Skip to content

feat(datastore) adds more serialization/deserialization tests #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions datastore/memory_address_ref_store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datastore

import (
"encoding/json"
"testing"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -579,3 +580,102 @@ func TestMemoryAddressRefStore_Filter(t *testing.T) {
})
}
}

func TestMemoryAddressRefStore_RecordsOrdering(t *testing.T) {
t.Parallel()

var (
recordOne = AddressRef{
Address: "0x1111111",
ChainSelector: 1,
Type: "contract1",
Version: semver.MustParse("1.0.0"),
Qualifier: "qual1",
Labels: NewLabelSet(
"label1", "label2",
),
}

recordTwo = AddressRef{
Address: "0x2222222",
ChainSelector: 2,
Type: "contract2",
Version: semver.MustParse("2.0.0"),
Qualifier: "qual2",
Labels: NewLabelSet(
"label3", "label4",
),
}

recordThree = AddressRef{
Address: "0x3333333",
ChainSelector: 3,
Type: "contract3",
Version: semver.MustParse("3.0.0"),
Qualifier: "qual3",
Labels: NewLabelSet(
"label5", "label6",
),
}

recordFour = AddressRef{
Address: "0x4444444",
ChainSelector: 4,
Type: "contract4",
Version: semver.MustParse("4.0.0"),
Qualifier: "qual4",
Labels: NewLabelSet(
"label7", "label8",
),
}
)

// Create a store with records in specific order
originalStore := MemoryAddressRefStore{
Records: []AddressRef{
recordOne,
recordTwo,
recordThree,
recordFour,
},
}

// marshal the store to JSON
data, err := json.Marshal(&originalStore)
require.NoError(t, err, "Failed to marshal store")

// unmarshal back to a new store instance
var unmarshaledStore MemoryAddressRefStore
err = json.Unmarshal(data, &unmarshaledStore)
require.NoError(t, err, "Failed to unmarshal store")

// verify the records are in the same order
require.Len(t, originalStore.Records, len(unmarshaledStore.Records),
"number of records should match after unmarshaling")

// compare each record to ensure order is maintained
for i, originalRecord := range originalStore.Records {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, i prefer the comparison to be done with JSON string, this is more fool proof as that is essentially what we see in CLD

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine some one implement MarshalJSON and ruin the ordering, this tests still pass but we dont get what we want anymore.

require.Equal(t, originalRecord.Address, unmarshaledStore.Records[i].Address,
"address at position %d should match", i)
require.Equal(t, originalRecord.ChainSelector, unmarshaledStore.Records[i].ChainSelector,
"chainSelector at position %d should match", i)
require.Equal(t, originalRecord.Type, unmarshaledStore.Records[i].Type,
"type at position %d should match", i)
require.Equal(t, originalRecord.Qualifier, unmarshaledStore.Records[i].Qualifier,
"qualifier at position %d should match", i)

// Version is a pointer, so we need to compare the actual version string
require.NotNil(t, unmarshaledStore.Records[i].Version, "Version at position %d should not be nil", i)
require.Equal(t, originalRecord.Version.String(), unmarshaledStore.Records[i].Version.String(),
"version at position %d should match", i)

require.Equal(t, originalRecord.Labels, unmarshaledStore.Records[i].Labels,
"labels at position %d should match", i)
}

// additionally, verify the keys and lookups still work
for i, originalRecord := range originalStore.Records {
idx := unmarshaledStore.indexOf(originalRecord.Key())
require.Equal(t, i, idx, "Index lookup for record %d should match original position", i)
}
}
74 changes: 74 additions & 0 deletions datastore/memory_contract_metadata_store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datastore

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -519,3 +520,76 @@ func TestMemoryContractMetadataStore_Filter(t *testing.T) {
})
}
}

func TestMemoryContractMetadataStore_RecordsOrdering(t *testing.T) {
t.Parallel()

var (
recordOne = ContractMetadata{
ChainSelector: 1,
Address: "0x1111111",
Metadata: testMetadata{Field: "metadata1", ChainSelector: 1},
}

recordTwo = ContractMetadata{
ChainSelector: 2,
Address: "0x2222222",
Metadata: testMetadata{Field: "metadata2", ChainSelector: 2},
}

recordThree = ContractMetadata{
ChainSelector: 3,
Address: "0x3333333",
Metadata: testMetadata{Field: "metadata3", ChainSelector: 3},
}

recordFour = ContractMetadata{
ChainSelector: 4,
Address: "0x4444444",
Metadata: testMetadata{Field: "metadata4", ChainSelector: 4},
}
)

// Create a store with records in specific order
originalStore := MemoryContractMetadataStore{
Records: []ContractMetadata{
recordOne,
recordTwo,
recordThree,
recordFour,
},
}

// Marshal the store to JSON
data, err := json.Marshal(&originalStore)
require.NoError(t, err, "Failed to marshal store")

// Unmarshal back to a new store instance
var unmarshaledStore MemoryContractMetadataStore
err = json.Unmarshal(data, &unmarshaledStore)
require.NoError(t, err, "Failed to unmarshal store")

// Verify the records are in the same order
require.Len(t, originalStore.Records, len(unmarshaledStore.Records),
"number of records should match after unmarshaling")

// Compare each record to ensure order is maintained
for i, originalRecord := range originalStore.Records {
require.Equal(t, originalRecord.Address, unmarshaledStore.Records[i].Address,
"address at position %d should match", i)
require.Equal(t, originalRecord.ChainSelector, unmarshaledStore.Records[i].ChainSelector,
"chainSelector at position %d should match", i)

// Compare metadata fields
originalMetadata, err := As[testMetadata](originalRecord.Metadata)
require.NoError(t, err, "failed to convert original metadata to testMetadata at position %d", i)

unmarshaledMetadata, err := As[testMetadata](unmarshaledStore.Records[i].Metadata)
require.NoError(t, err, "failed to convert unmarshaled metadata to testMetadata at position %d", i)

require.Equal(t, originalMetadata.Field, unmarshaledMetadata.Field,
"metadata field at position %d should match", i)
require.Equal(t, originalMetadata.ChainSelector, unmarshaledMetadata.ChainSelector,
"metadata chain selector at position %d should match", i)
}
}