Skip to content

Commit ed9a433

Browse files
committed
feat(datastore) adds more serialization/deserialization tests
1 parent 93946c5 commit ed9a433

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

datastore/memory_address_ref_store_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datastore
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/Masterminds/semver/v3"
@@ -579,3 +580,102 @@ func TestMemoryAddressRefStore_Filter(t *testing.T) {
579580
})
580581
}
581582
}
583+
584+
func TestMemoryAddressRefStore_RecordsOrdering(t *testing.T) {
585+
t.Parallel()
586+
587+
var (
588+
recordOne = AddressRef{
589+
Address: "0x1111111",
590+
ChainSelector: 1,
591+
Type: "contract1",
592+
Version: semver.MustParse("1.0.0"),
593+
Qualifier: "qual1",
594+
Labels: NewLabelSet(
595+
"label1", "label2",
596+
),
597+
}
598+
599+
recordTwo = AddressRef{
600+
Address: "0x2222222",
601+
ChainSelector: 2,
602+
Type: "contract2",
603+
Version: semver.MustParse("2.0.0"),
604+
Qualifier: "qual2",
605+
Labels: NewLabelSet(
606+
"label3", "label4",
607+
),
608+
}
609+
610+
recordThree = AddressRef{
611+
Address: "0x3333333",
612+
ChainSelector: 3,
613+
Type: "contract3",
614+
Version: semver.MustParse("3.0.0"),
615+
Qualifier: "qual3",
616+
Labels: NewLabelSet(
617+
"label5", "label6",
618+
),
619+
}
620+
621+
recordFour = AddressRef{
622+
Address: "0x4444444",
623+
ChainSelector: 4,
624+
Type: "contract4",
625+
Version: semver.MustParse("4.0.0"),
626+
Qualifier: "qual4",
627+
Labels: NewLabelSet(
628+
"label7", "label8",
629+
),
630+
}
631+
)
632+
633+
// Create a store with records in specific order
634+
originalStore := MemoryAddressRefStore{
635+
Records: []AddressRef{
636+
recordOne,
637+
recordTwo,
638+
recordThree,
639+
recordFour,
640+
},
641+
}
642+
643+
// marshal the store to JSON
644+
data, err := json.Marshal(&originalStore)
645+
require.NoError(t, err, "Failed to marshal store")
646+
647+
// unmarshal back to a new store instance
648+
var unmarshaledStore MemoryAddressRefStore
649+
err = json.Unmarshal(data, &unmarshaledStore)
650+
require.NoError(t, err, "Failed to unmarshal store")
651+
652+
// verify the records are in the same order
653+
require.Equal(t, len(originalStore.Records), len(unmarshaledStore.Records),
654+
"number of records should match after unmarshaling")
655+
656+
// compare each record to ensure order is maintained
657+
for i, originalRecord := range originalStore.Records {
658+
require.Equal(t, originalRecord.Address, unmarshaledStore.Records[i].Address,
659+
"address at position %d should match", i)
660+
require.Equal(t, originalRecord.ChainSelector, unmarshaledStore.Records[i].ChainSelector,
661+
"chainSelector at position %d should match", i)
662+
require.Equal(t, originalRecord.Type, unmarshaledStore.Records[i].Type,
663+
"type at position %d should match", i)
664+
require.Equal(t, originalRecord.Qualifier, unmarshaledStore.Records[i].Qualifier,
665+
"qualifier at position %d should match", i)
666+
667+
// Version is a pointer, so we need to compare the actual version string
668+
require.NotNil(t, unmarshaledStore.Records[i].Version, "Version at position %d should not be nil", i)
669+
require.Equal(t, originalRecord.Version.String(), unmarshaledStore.Records[i].Version.String(),
670+
"version at position %d should match", i)
671+
672+
require.Equal(t, originalRecord.Labels, unmarshaledStore.Records[i].Labels,
673+
"labels at position %d should match", i)
674+
}
675+
676+
// additionally, verify the keys and lookups still work
677+
for i, originalRecord := range originalStore.Records {
678+
idx := unmarshaledStore.indexOf(originalRecord.Key())
679+
require.Equal(t, i, idx, "Index lookup for record %d should match original position", i)
680+
}
681+
}

datastore/memory_contract_metadata_store_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datastore
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -519,3 +520,76 @@ func TestMemoryContractMetadataStore_Filter(t *testing.T) {
519520
})
520521
}
521522
}
523+
524+
func TestMemoryContractMetadataStore_RecordsOrdering(t *testing.T) {
525+
t.Parallel()
526+
527+
var (
528+
recordOne = ContractMetadata{
529+
ChainSelector: 1,
530+
Address: "0x1111111",
531+
Metadata: testMetadata{Field: "metadata1", ChainSelector: 1},
532+
}
533+
534+
recordTwo = ContractMetadata{
535+
ChainSelector: 2,
536+
Address: "0x2222222",
537+
Metadata: testMetadata{Field: "metadata2", ChainSelector: 2},
538+
}
539+
540+
recordThree = ContractMetadata{
541+
ChainSelector: 3,
542+
Address: "0x3333333",
543+
Metadata: testMetadata{Field: "metadata3", ChainSelector: 3},
544+
}
545+
546+
recordFour = ContractMetadata{
547+
ChainSelector: 4,
548+
Address: "0x4444444",
549+
Metadata: testMetadata{Field: "metadata4", ChainSelector: 4},
550+
}
551+
)
552+
553+
// Create a store with records in specific order
554+
originalStore := MemoryContractMetadataStore{
555+
Records: []ContractMetadata{
556+
recordOne,
557+
recordTwo,
558+
recordThree,
559+
recordFour,
560+
},
561+
}
562+
563+
// Marshal the store to JSON
564+
data, err := json.Marshal(&originalStore)
565+
require.NoError(t, err, "Failed to marshal store")
566+
567+
// Unmarshal back to a new store instance
568+
var unmarshaledStore MemoryContractMetadataStore
569+
err = json.Unmarshal(data, &unmarshaledStore)
570+
require.NoError(t, err, "Failed to unmarshal store")
571+
572+
// Verify the records are in the same order
573+
require.Equal(t, len(originalStore.Records), len(unmarshaledStore.Records),
574+
"number of records should match after unmarshaling")
575+
576+
// Compare each record to ensure order is maintained
577+
for i, originalRecord := range originalStore.Records {
578+
require.Equal(t, originalRecord.Address, unmarshaledStore.Records[i].Address,
579+
"address at position %d should match", i)
580+
require.Equal(t, originalRecord.ChainSelector, unmarshaledStore.Records[i].ChainSelector,
581+
"chainSelector at position %d should match", i)
582+
583+
// Compare metadata fields
584+
originalMetadata, err := As[testMetadata](originalRecord.Metadata)
585+
require.NoError(t, err, "failed to convert original metadata to testMetadata at position %d", i)
586+
587+
unmarshaledMetadata, err := As[testMetadata](unmarshaledStore.Records[i].Metadata)
588+
require.NoError(t, err, "failed to convert unmarshaled metadata to testMetadata at position %d", i)
589+
590+
require.Equal(t, originalMetadata.Field, unmarshaledMetadata.Field,
591+
"metadata field at position %d should match", i)
592+
require.Equal(t, originalMetadata.ChainSelector, unmarshaledMetadata.ChainSelector,
593+
"metadata chain selector at position %d should match", i)
594+
}
595+
}

0 commit comments

Comments
 (0)