Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 22, 2024
1 parent b268c0b commit eb78413
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/integration/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions tests/integration/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
114 changes: 114 additions & 0 deletions tests/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -1453,6 +1504,32 @@ func deleteDoc(

assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)

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)
}

if action.ExpectedError == "" {
docIDs := map[string]struct{}{
docID.String(): {},
Expand Down Expand Up @@ -1504,6 +1581,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(
Expand Down Expand Up @@ -2079,6 +2182,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)

Expand Down

0 comments on commit eb78413

Please sign in to comment.