testing: add TestDirectoryMetadata_EncodeJSON

This commit is contained in:
Derrick Hammer 2024-01-18 10:03:52 -05:00
parent a3af7485a6
commit dfcfd80d93
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 45 additions and 1 deletions

View File

@ -1,9 +1,11 @@
package metadata package metadata
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/emirpasic/gods/maps/linkedhashmap" "github.com/emirpasic/gods/maps/linkedhashmap"
cmp "github.com/google/go-cmp/cmp" cmp "github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"os" "os"
"path/filepath" "path/filepath"
@ -59,7 +61,7 @@ func readFile(filename string) []byte {
} }
func getDirectoryMeta() *DirectoryMetadata { func getDirectoryMeta() *DirectoryMetadata {
data := readFile("directory.json") data := getDirectoryMetaContent()
var dir DirectoryMetadata var dir DirectoryMetadata
@ -71,6 +73,12 @@ func getDirectoryMeta() *DirectoryMetadata {
return &dir return &dir
} }
func getDirectoryMetaContent() []byte {
data := readFile("directory.json")
return data
}
func TestDirectoryMetadata_DecodeJSON(t *testing.T) { func TestDirectoryMetadata_DecodeJSON(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -165,3 +173,39 @@ func TestDirectoryMetadata_EncodeMsgpack(t *testing.T) {
}) })
} }
} }
func TestDirectoryMetadata_EncodeJSON(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{
name: "Encode",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonDm := getDirectoryMetaContent()
dm := &DirectoryMetadata{}
if err := json.Unmarshal(jsonDm, dm); (err != nil) != tt.wantErr {
t.Errorf("EncodeJSON() error = %v, wantErr %v", err, tt.wantErr)
}
jsonData, err := json.MarshalIndent(dm, "", "\t")
if (err != nil) != tt.wantErr {
t.Errorf("EncodeJSON() error = %v, wantErr %v", err, tt.wantErr)
}
buf := bytes.NewBuffer(nil)
err = json.Indent(buf, jsonData, "", "\t")
if err != nil {
t.Errorf("EncodeJSON() error = %v, wantErr %v", err, tt.wantErr)
}
assert.Equal(t, buf.Bytes(), jsonData)
})
}
}