diff --git a/tests/integration/state.go b/tests/integration/state.go index 9e65458531..4948a01c54 100644 --- a/tests/integration/state.go +++ b/tests/integration/state.go @@ -175,6 +175,9 @@ type state struct { // nodes. docIDs [][]client.DocID + // CIDs by index, by field name, by doc index, by collection index. + cids [][]map[string][]cid.Cid + // Indexes, by index, by collection index, by node index. indexes [][][]client.IndexDescription diff --git a/tests/integration/test_case.go b/tests/integration/test_case.go index 3103d674ca..1fddebb3b9 100644 --- a/tests/integration/test_case.go +++ b/tests/integration/test_case.go @@ -17,6 +17,7 @@ import ( "github.com/sourcenetwork/immutable" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/internal/core" "github.com/sourcenetwork/defradb/net" "github.com/sourcenetwork/defradb/tests/gen" "github.com/sourcenetwork/defradb/tests/predefined" @@ -348,6 +349,31 @@ func NewDocIndex(collectionIndex int, index int) DocIndex { } } +type CidIndex struct { + CollectionIndex int + DocIndex int + FieldName string + Index int +} + +func NewCompCidIndex(collectionIndex int, docIndex int, index int) CidIndex { + return CidIndex{ + CollectionIndex: collectionIndex, + DocIndex: docIndex, + FieldName: core.COMPOSITE_NAMESPACE, + Index: index, + } +} + +func NewCidIndex(collectionIndex int, docIndex int, fieldName string, index int) CidIndex { + return CidIndex{ + CollectionIndex: collectionIndex, + DocIndex: docIndex, + FieldName: fieldName, + Index: index, + } +} + // DeleteDoc will attempt to delete the given document in the given collection // using the collection api. type DeleteDoc struct { diff --git a/tests/integration/utils.go b/tests/integration/utils.go index 05698e9a39..9cbc5a26f7 100644 --- a/tests/integration/utils.go +++ b/tests/integration/utils.go @@ -24,6 +24,8 @@ import ( "time" "github.com/fxamacker/cbor/v2" + "github.com/ipfs/go-cid" + "github.com/ipfs/go-datastore/query" "github.com/sourcenetwork/corelog" "github.com/sourcenetwork/immutable" "github.com/stretchr/testify/assert" @@ -34,6 +36,7 @@ import ( "github.com/sourcenetwork/defradb/crypto" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/errors" + "github.com/sourcenetwork/defradb/internal/core" "github.com/sourcenetwork/defradb/internal/db" "github.com/sourcenetwork/defradb/internal/encryption" "github.com/sourcenetwork/defradb/internal/request/graphql" @@ -1271,8 +1274,56 @@ func createDoc( // Expand the slice if required, so that the document can be accessed by collection index s.docIDs = append(s.docIDs, make([][]client.DocID, action.CollectionID-len(s.docIDs)+1)...) } + startDocIndex := len(s.docIDs[action.CollectionID]) s.docIDs[action.CollectionID] = append(s.docIDs[action.CollectionID], docIDs...) + for relativeDocIndex, docID := range docIDs { + results, err := nodes[0].Headstore().Query( + s.ctx, + query.Query{ + Prefix: core.HeadStoreKey{ + DocID: docID.String(), + }.ToString(), + KeysOnly: true, + }, + ) + if err != nil { + s.t.Fatalf("failed to fetch cids: %s", err.Error()) + } + + for res := range results.Next() { + if res.Error != nil { + s.t.Fatalf("failed to fetch cids: %s", res.Error.Error()) + } + + key, err := core.NewHeadStoreKey(res.Key) + if err != nil { + s.t.Fatalf("failed to fetch cids: %s", err.Error()) + } + + if action.CollectionID >= len(s.cids) { + // Expand the slice if required, so that the cids can be accessed by collection index + s.cids = append(s.cids, make([][]map[string][]cid.Cid, action.CollectionID+1)...) + } + + colCids := s.cids[action.CollectionID] + docIndex := startDocIndex + relativeDocIndex + if docIndex >= len(colCids) { + colCids = append(colCids, make([]map[string][]cid.Cid, docIndex-len(colCids)+1)...) // todo - this can be moved outside of loop + } + + if colCids[docIndex] == nil { + colCids[docIndex] = map[string][]cid.Cid{} + } + + // This is a new doc, so we can blindly overwrite the array of cids + colCids[docIndex][key.FieldID] = []cid.Cid{ + key.Cid, + } + s.cids[action.CollectionID] = colCids + } + } + if action.ExpectedError == "" { waitForUpdateEvents(s, action.NodeID, getEventsForCreateDoc(s, action)) } @@ -1504,6 +1555,32 @@ func updateDoc( if action.ExpectedError == "" && !action.SkipLocalUpdateEvent { waitForUpdateEvents(s, action.NodeID, getEventsForUpdateDoc(s, action)) } + + results, err := nodes[0].Headstore().Query( + s.ctx, + query.Query{ + Prefix: core.HeadStoreKey{ + DocID: s.docIDs[action.CollectionID][action.DocID].String(), + }.ToString(), + KeysOnly: true, + }, + ) + if err != nil { + s.t.Fatalf("failed to fetch cids: %s", err.Error()) + } + + for res := range results.Next() { + if res.Error != nil { + s.t.Fatalf("failed to fetch cids: %s", res.Error.Error()) + } + + key, err := core.NewHeadStoreKey(res.Key) + if err != nil { + s.t.Fatalf("failed to fetch cids: %s", err.Error()) + } + + s.cids[action.CollectionID][action.DocID][key.FieldID] = append(s.cids[action.CollectionID][action.DocID][key.FieldID], key.Cid) + } } func updateDocViaColSave( @@ -2079,6 +2156,17 @@ func assertRequestResultDocs( actualValue, fmt.Sprintf("node: %v, path: %s", nodeID, stack), ) + + case CidIndex: + expectedCid := s.cids[expectedValue.CollectionIndex][expectedValue.DocIndex][expectedValue.FieldName][expectedValue.Index].String() + assertResultsEqual( + s.t, + s.clientType, + expectedCid, + actualValue, + fmt.Sprintf("node: %v, path: %s", nodeID, stack), + ) + case []map[string]any: actualValueMap := ConvertToArrayOfMaps(s.t, actualValue)