From c039ced75ea02146d6ecb46ce3d8b2bc332fb3db Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 5 Jan 2024 06:23:04 -0500 Subject: [PATCH] test: implement directory encoding test, clean up all other unn-needed test dummies --- metadata/directory_test.go | 442 ++----------------------------------- 1 file changed, 19 insertions(+), 423 deletions(-) diff --git a/metadata/directory_test.go b/metadata/directory_test.go index f87c18e..cbb76b0 100644 --- a/metadata/directory_test.go +++ b/metadata/directory_test.go @@ -2,7 +2,6 @@ package metadata import ( "encoding/json" - "fmt" "github.com/emirpasic/gods/maps/linkedhashmap" cmp "github.com/google/go-cmp/cmp" "github.com/vmihailenco/msgpack/v5" @@ -102,446 +101,43 @@ func TestDirectoryMetadata_DecodeMsgpack(t *testing.T) { } func TestDirectoryMetadata_EncodeMsgpack(t *testing.T) { - type fields struct { - Details DirectoryMetadataDetails - Directories map[string]DirectoryReference - Files map[string]FileReference - ExtraMetadata ExtraMetadata - } - type args struct { - enc *msgpack.Encoder - } tests := []struct { name string - fields fields - args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Encode", + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - dm := &DirectoryMetadata{ - Details: tt.fields.Details, - Directories: tt.fields.Directories, - Files: tt.fields.Files, - ExtraMetadata: tt.fields.ExtraMetadata, - } - if err := dm.EncodeMsgpack(tt.args.enc); (err != nil) != tt.wantErr { - t.Errorf("EncodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} + dm := &DirectoryMetadata{} -func TestDirectoryReference_DecodeMsgpack(t *testing.T) { - type fields struct { - Created uint64 - Name string - EncryptedWriteKey []byte - PublicKey []byte - EncryptionKey []byte - Ext map[string]interface{} - URI string - Key string - Size int64 - } - type args struct { - dec *msgpack.Decoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dr := &DirectoryReference{ - Created: tt.fields.Created, - Name: tt.fields.Name, - EncryptedWriteKey: tt.fields.EncryptedWriteKey, - PublicKey: tt.fields.PublicKey, - EncryptionKey: tt.fields.EncryptionKey, - Ext: tt.fields.Ext, - URI: tt.fields.URI, - Key: tt.fields.Key, - Size: tt.fields.Size, - } - if err := dr.DecodeMsgpack(tt.args.dec); (err != nil) != tt.wantErr { + good := readFile("directory.bin") + + if err := msgpack.Unmarshal(good, dm); (err != nil) != tt.wantErr { t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) } - }) - } -} -func TestDirectoryReference_EncodeMsgpack(t *testing.T) { - type fields struct { - Created uint64 - Name string - EncryptedWriteKey []byte - PublicKey []byte - EncryptionKey []byte - Ext map[string]interface{} - URI string - Key string - Size int64 - } - type args struct { - enc *msgpack.Encoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dr := &DirectoryReference{ - Created: tt.fields.Created, - Name: tt.fields.Name, - EncryptedWriteKey: tt.fields.EncryptedWriteKey, - PublicKey: tt.fields.PublicKey, - EncryptionKey: tt.fields.EncryptionKey, - Ext: tt.fields.Ext, - URI: tt.fields.URI, - Key: tt.fields.Key, - Size: tt.fields.Size, - } - if err := dr.EncodeMsgpack(tt.args.enc); (err != nil) != tt.wantErr { + out, err := msgpack.Marshal(dm) + + if (err != nil) != tt.wantErr { t.Errorf("EncodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) } - }) - } -} -func TestFileReference_DecodeMsgpack(t *testing.T) { - type fields struct { - Name string - Created int - Version int - File *FileVersion - Ext map[string]interface{} - History map[int]*FileVersion - MimeType string - URI string - Key string - } - type args struct { - dec *msgpack.Decoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fr := &FileReference{ - Name: tt.fields.Name, - Created: tt.fields.Created, - Version: tt.fields.Version, - File: tt.fields.File, - Ext: tt.fields.Ext, - History: tt.fields.History, - MimeType: tt.fields.MimeType, - URI: tt.fields.URI, - Key: tt.fields.Key, + if !cmp.Equal(good, out) { + t.Errorf("EncodeMsgpack() error = %v, wantErr %v", "msgpack does not match sample", tt.wantErr) } - if err := fr.DecodeMsgpack(tt.args.dec); (err != nil) != tt.wantErr { + + dm2 := &DirectoryMetadata{} + + if err := msgpack.Unmarshal(out, dm2); (err != nil) != tt.wantErr { t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) } - }) - } -} -func TestFileReference_EncodeMsgpack(t *testing.T) { - type fields struct { - Name string - Created int - Version int - File *FileVersion - Ext map[string]interface{} - History map[int]*FileVersion - MimeType string - URI string - Key string - } - type args struct { - enc *msgpack.Encoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fr := &FileReference{ - Name: tt.fields.Name, - Created: tt.fields.Created, - Version: tt.fields.Version, - File: tt.fields.File, - Ext: tt.fields.Ext, - History: tt.fields.History, - MimeType: tt.fields.MimeType, - URI: tt.fields.URI, - Key: tt.fields.Key, - } - if err := fr.EncodeMsgpack(tt.args.enc); (err != nil) != tt.wantErr { - t.Errorf("EncodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestFileReference_Modified(t *testing.T) { - type fields struct { - Name string - Created int - Version int - File *FileVersion - Ext map[string]interface{} - History map[int]*FileVersion - MimeType string - URI string - Key string - } - tests := []struct { - name string - fields fields - want int - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fr := &FileReference{ - Name: tt.fields.Name, - Created: tt.fields.Created, - Version: tt.fields.Version, - File: tt.fields.File, - Ext: tt.fields.Ext, - History: tt.fields.History, - MimeType: tt.fields.MimeType, - URI: tt.fields.URI, - Key: tt.fields.Key, - } - if got := fr.Modified(); got != tt.want { - t.Errorf("Modified() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestFileVersionThumbnail_DecodeMsgpack(t *testing.T) { - type fields struct { - ImageType string - AspectRatio float64 - CID *encoding.EncryptedCID - Thumbhash []byte - } - type args struct { - dec *msgpack.Decoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fvt := &FileVersionThumbnail{ - ImageType: tt.fields.ImageType, - AspectRatio: tt.fields.AspectRatio, - CID: tt.fields.CID, - Thumbhash: tt.fields.Thumbhash, - } - if err := fvt.DecodeMsgpack(tt.args.dec); (err != nil) != tt.wantErr { - t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestFileVersionThumbnail_Encode(t *testing.T) { - type fields struct { - ImageType string - AspectRatio float64 - CID *encoding.EncryptedCID - Thumbhash []byte - } - tests := []struct { - name string - fields fields - want map[int]interface{} - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fvt := &FileVersionThumbnail{ - ImageType: tt.fields.ImageType, - AspectRatio: tt.fields.AspectRatio, - CID: tt.fields.CID, - Thumbhash: tt.fields.Thumbhash, - } - if got := fvt.Encode(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("Encode() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestFileVersionThumbnail_EncodeMsgpack(t *testing.T) { - type fields struct { - ImageType string - AspectRatio float64 - CID *encoding.EncryptedCID - Thumbhash []byte - } - type args struct { - enc *msgpack.Encoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fvt := &FileVersionThumbnail{ - ImageType: tt.fields.ImageType, - AspectRatio: tt.fields.AspectRatio, - CID: tt.fields.CID, - Thumbhash: tt.fields.Thumbhash, - } - if err := fvt.EncodeMsgpack(tt.args.enc); (err != nil) != tt.wantErr { - t.Errorf("EncodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestFileVersion_CID(t *testing.T) { - type fields struct { - Ts int - EncryptedCID *encoding.EncryptedCID - PlaintextCID *encoding.CID - Thumbnail *FileVersionThumbnail - Hashes []*encoding.Multihash - Ext map[string]interface{} - } - tests := []struct { - name string - fields fields - want *encoding.CID - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fv := &FileVersion{ - Ts: tt.fields.Ts, - EncryptedCID: tt.fields.EncryptedCID, - PlaintextCID: tt.fields.PlaintextCID, - Thumbnail: tt.fields.Thumbnail, - Hashes: tt.fields.Hashes, - Ext: tt.fields.Ext, - } - if got := fv.CID(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("CID() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestFileVersion_DecodeMsgpack(t *testing.T) { - type fields struct { - Ts int - EncryptedCID *encoding.EncryptedCID - PlaintextCID *encoding.CID - Thumbnail *FileVersionThumbnail - Hashes []*encoding.Multihash - Ext map[string]interface{} - } - type args struct { - dec *msgpack.Decoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fv := &FileVersion{ - Ts: tt.fields.Ts, - EncryptedCID: tt.fields.EncryptedCID, - PlaintextCID: tt.fields.PlaintextCID, - Thumbnail: tt.fields.Thumbnail, - Hashes: tt.fields.Hashes, - Ext: tt.fields.Ext, - } - if err := fv.DecodeMsgpack(tt.args.dec); (err != nil) != tt.wantErr { - t.Errorf("DecodeMsgpack() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestFileVersion_EncodeMsgpack(t *testing.T) { - type fields struct { - Ts int - EncryptedCID *encoding.EncryptedCID - PlaintextCID *encoding.CID - Thumbnail *FileVersionThumbnail - Hashes []*encoding.Multihash - Ext map[string]interface{} - } - type args struct { - enc *msgpack.Encoder - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fv := &FileVersion{ - Ts: tt.fields.Ts, - EncryptedCID: tt.fields.EncryptedCID, - PlaintextCID: tt.fields.PlaintextCID, - Thumbnail: tt.fields.Thumbnail, - Hashes: tt.fields.Hashes, - Ext: tt.fields.Ext, - } - if err := fv.EncodeMsgpack(tt.args.enc); (err != nil) != tt.wantErr { - t.Errorf("EncodeMsgpack() 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) } }) }