|
1 | 1 | package datastore
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/Masterminds/semver/v3"
|
@@ -579,3 +580,102 @@ func TestMemoryAddressRefStore_Filter(t *testing.T) {
|
579 | 580 | })
|
580 | 581 | }
|
581 | 582 | }
|
| 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 | +} |
0 commit comments