libs5-go/metadata/directory_test.go

145 lines
3.3 KiB
Go
Raw Normal View History

2024-01-04 13:20:37 +00:00
package metadata
import (
"encoding/json"
"github.com/emirpasic/gods/maps/linkedhashmap"
cmp "github.com/google/go-cmp/cmp"
2024-01-04 13:20:37 +00:00
"github.com/vmihailenco/msgpack/v5"
"os"
"path/filepath"
"testing"
)
func isEqual(sizeFunc1, sizeFunc2 func() int, iteratorFunc1, iteratorFunc2 func() linkedhashmap.Iterator) bool {
if sizeFunc1() != sizeFunc2() {
return false
}
iter1 := iteratorFunc1()
iter2 := iteratorFunc2()
for iter1.Next() {
iter2.Next()
if iter1.Key() != iter2.Key() {
return false
}
if !cmp.Equal(iter1.Value(), iter2.Value()) {
return false
}
}
return true
}
func (frm fileReferenceMap) Equal(other fileReferenceMap) bool {
return isEqual(frm.Size, other.Size, frm.Iterator, other.Iterator)
}
func (frm fileHistoryMap) Equal(other fileHistoryMap) bool {
return isEqual(frm.Size, other.Size, frm.Iterator, other.Iterator)
}
func (drm directoryReferenceMap) Equal(other directoryReferenceMap) bool {
return isEqual(drm.Size, other.Size, drm.Iterator, other.Iterator)
}
func (ext extMap) Equal(other extMap) bool {
return isEqual(ext.Size, other.Size, ext.Iterator, other.Iterator)
}
2024-01-04 13:20:37 +00:00
func readFile(filename string) []byte {
filePath := filepath.Join("testdata", filename)
data, err := os.ReadFile(filePath)
if err != nil {
panic(err)
}
return data
}
func getDirectoryMeta() *DirectoryMetadata {
data := readFile("directory.json")
var dir DirectoryMetadata
err := json.Unmarshal(data, &dir)
if err != nil {
panic(err)
}
return &dir
}
func TestDirectoryMetadata_DecodeMsgpack(t *testing.T) {
type fields struct {
Details DirectoryMetadataDetails
Directories map[string]DirectoryReference
Files map[string]FileReference
ExtraMetadata ExtraMetadata
}
tests := []struct {
name string
wantErr bool
}{
{
name: "Decode",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonDm := getDirectoryMeta()
dm := &DirectoryMetadata{}
if err := msgpack.Unmarshal(readFile("directory.bin"), dm); (err != nil) != tt.wantErr {
t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(jsonDm, dm) {
t.Errorf("DecodeMsgpack() error = %v, wantErr %v", "msgpack does not match json", tt.wantErr)
}
})
}
}
func TestDirectoryMetadata_EncodeMsgpack(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{
name: "Encode",
wantErr: false,
},
2024-01-04 13:20:37 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dm := &DirectoryMetadata{}
2024-01-04 13:20:37 +00:00
good := readFile("directory.bin")
2024-01-04 13:20:37 +00:00
if err := msgpack.Unmarshal(good, dm); (err != nil) != tt.wantErr {
2024-01-04 13:20:37 +00:00
t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr)
}
out, err := msgpack.Marshal(dm)
2024-01-04 13:20:37 +00:00
if (err != nil) != tt.wantErr {
2024-01-04 13:20:37 +00:00
t.Errorf("EncodeMsgpack() error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(good, out) {
t.Errorf("EncodeMsgpack() error = %v, wantErr %v", "msgpack does not match sample", tt.wantErr)
2024-01-04 13:20:37 +00:00
}
dm2 := &DirectoryMetadata{}
if err := msgpack.Unmarshal(out, dm2); (err != nil) != tt.wantErr {
2024-01-04 13:20:37 +00:00
t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(dm, dm2) {
t.Errorf("EncodeMsgpack() error = %v, wantErr %v", "msgpack deser verification does not match", tt.wantErr)
2024-01-04 13:20:37 +00:00
}
})
}
}