diff --git a/db/collection_test.go b/db/collection_test.go index b7dcd6dd9e..dd57cb285b 100644 --- a/db/collection_test.go +++ b/db/collection_test.go @@ -12,117 +12,11 @@ package db import ( "context" - "reflect" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/sourcenetwork/defradb/client" ) -func newTestCollectionWithSchema( - t *testing.T, - ctx context.Context, - db *implicitTxnDB, -) (client.Collection, error) { - desc := client.CollectionDescription{ - Name: "users", - Schema: client.SchemaDescription{ - Fields: []client.FieldDescription{ - { - Name: "_key", - Kind: client.FieldKind_DocKey, - }, - { - Name: "Name", - Kind: client.FieldKind_STRING, - Typ: client.LWW_REGISTER, - }, - { - Name: "Age", - Kind: client.FieldKind_INT, - Typ: client.LWW_REGISTER, - }, - { - Name: "Weight", - Kind: client.FieldKind_FLOAT, - Typ: client.LWW_REGISTER, - }, - }, - }, - } - - txn, err := db.NewTxn(ctx, false) - require.NoError(t, err) - - col, err := db.createCollection(ctx, txn, desc) - if err != nil { - return col, err - } - - return col, txn.Commit(ctx) -} - -func TestNewCollectionWithSchema(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - schema := col.Schema() - desc := col.Description() - - assert.True(t, reflect.DeepEqual(schema, desc.Schema)) - assert.Equal(t, "users", col.Name()) - assert.Equal(t, uint32(1), col.ID()) - assert.False(t, reflect.DeepEqual(schema, client.SchemaDescription{})) - assert.Equal(t, 4, len(schema.Fields)) - - for i := 0; i < 4; i++ { - assert.Equal(t, client.FieldID(i), schema.Fields[i].ID) - } -} - -func TestNewCollectionReturnsErrorGivenDuplicateSchema(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - - _, err = newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - _, err = newTestCollectionWithSchema(t, ctx, db) - assert.Errorf(t, err, "collection already exists") -} - -func TestGetCollectionByName(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - - _, err = newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - col, err := db.GetCollectionByName(ctx, "users") - assert.NoError(t, err) - - schema := col.Schema() - desc := col.Description() - - assert.True(t, reflect.DeepEqual(schema, desc.Schema)) - assert.Equal(t, "users", col.Name()) - assert.Equal(t, uint32(1), col.ID()) - assert.False(t, reflect.DeepEqual(schema, client.SchemaDescription{})) - assert.Equal(t, 4, len(schema.Fields)) - - for i := 0; i < 4; i++ { - assert.Equal(t, client.FieldID(i), schema.Fields[i].ID) - } -} - func TestGetCollectionByNameReturnsErrorGivenNonExistantCollection(t *testing.T) { ctx := context.Background() db, err := newMemoryDB(ctx) diff --git a/db/db_test.go b/db/db_test.go index c1a9648f36..9d681058c8 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -15,14 +15,8 @@ import ( "testing" badger "github.com/dgraph-io/badger/v4" - dag "github.com/ipfs/boxo/ipld/merkledag" - "github.com/stretchr/testify/assert" - "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/core" - corecrdt "github.com/sourcenetwork/defradb/core/crdt" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v4" - "github.com/sourcenetwork/defradb/merkle/clock" ) func newMemoryDB(ctx context.Context) (*implicitTxnDB, error) { @@ -48,369 +42,3 @@ func TestNewDB(t *testing.T) { t.Error(err) } } - -func TestDBSaveSimpleDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - if err != nil { - t.Error(err) - return - } - - err = col.Save(ctx, doc) - if err != nil { - t.Error(err) - } - - // value check - name, err := doc.Get("Name") - assert.NoError(t, err) - age, err := doc.Get("Age") - assert.NoError(t, err) - weight, err := doc.Get("Weight") - assert.NoError(t, err) - - assert.Equal(t, "John", name) - assert.Equal(t, int64(21), age) - assert.Equal(t, 154.1, weight) - - _, err = doc.Get("DoesntExist") - assert.Error(t, err) - - // db.printDebugDB() -} - -func TestDBUpdateDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - if err != nil { - t.Error(err) - return - } - - err = col.Save(ctx, doc) - if err != nil { - t.Error(err) - } - - // update fields - doc.Set("Name", "Pete") - doc.Delete("Weight") - - weightField := doc.Fields()["Weight"] - weightVal, _ := doc.GetValueWithField(weightField) - assert.True(t, weightVal.IsDelete()) - - err = col.Update(ctx, doc) - if err != nil { - t.Error(err) - } - - // value check - name, err := doc.Get("Name") - assert.NoError(t, err) - age, err := doc.Get("Age") - assert.NoError(t, err) - weight, err := doc.Get("Weight") - assert.NoError(t, err) - - assert.Equal(t, "Pete", name) - assert.Equal(t, int64(21), age) - assert.Nil(t, weight) -} - -func TestDBUpdateNonExistingDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - if err != nil { - t.Error(err) - return - } - - err = col.Update(ctx, doc) - assert.Error(t, err) -} - -func TestDBUpdateExistingDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - assert.NoError(t, err) - - err = col.Save(ctx, doc) - assert.NoError(t, err) - - testJSONObj = []byte(`{ - "_key": "bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d", - "Name": "Pete", - "Age": 31 - }`) - - doc, err = client.NewDocFromJSON(testJSONObj) - assert.NoError(t, err) - - err = col.Update(ctx, doc) - assert.NoError(t, err) - - // value check - name, err := doc.Get("Name") - assert.NoError(t, err) - age, err := doc.Get("Age") - assert.NoError(t, err) - // weight, err := doc.Get("Weight") - // assert.NoError(t, err) - - assert.Equal(t, "Pete", name) - assert.Equal(t, int64(31), age) -} - -func TestDBGetDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - assert.NoError(t, err) - - err = col.Save(ctx, doc) - assert.NoError(t, err) - - key, err := client.NewDocKeyFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d") - assert.NoError(t, err) - doc, err = col.Get(ctx, key, false) - assert.NoError(t, err) - - // value check - name, err := doc.Get("Name") - assert.NoError(t, err) - age, err := doc.Get("Age") - assert.NoError(t, err) - weight, err := doc.Get("Weight") - assert.NoError(t, err) - - assert.Equal(t, "John", name) - assert.Equal( - t, - uint64(21), - age, - ) // note: uint is used here, because the CBOR implementation converts all positive ints to uint64 - assert.Equal(t, 154.1, weight) -} - -func TestDBGetNotFoundDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - key, err := client.NewDocKeyFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d") - assert.NoError(t, err) - _, err = col.Get(ctx, key, false) - assert.EqualError(t, err, client.ErrDocumentNotFound.Error()) -} - -func TestDBDeleteDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - assert.NoError(t, err) - - err = col.Save(ctx, doc) - assert.NoError(t, err) - - key, err := client.NewDocKeyFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d") - assert.NoError(t, err) - deleted, err := col.Delete(ctx, key) - assert.NoError(t, err) - assert.True(t, deleted) -} - -func TestDBDeleteNotFoundDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - key, err := client.NewDocKeyFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d") - assert.NoError(t, err) - deleted, err := col.Delete(ctx, key) - assert.EqualError(t, err, client.ErrDocumentNotFound.Error()) - assert.False(t, deleted) -} - -func TestDocumentMerkleDAG(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21, - "Weight": 154.1 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - assert.NoError(t, err) - - err = col.Save(ctx, doc) - assert.NoError(t, err) - - clk := clock.NewMerkleClock( - db.multistore.Headstore(), - nil, - core.HeadStoreKey{}.WithDocKey( - "bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d", - ).WithFieldId( - "Name", - ), - nil, - ) - heads := clk.(*clock.MerkleClock).Heads() - cids, _, err := heads.List(ctx) - assert.NoError(t, err) - - reg := corecrdt.LWWRegister{} - for _, c := range cids { - b, errGet := db.Blockstore().Get(ctx, c) - assert.NoError(t, errGet) - - nd, errDecode := dag.DecodeProtobuf(b.RawData()) - assert.NoError(t, errDecode) - - _, errMarshal := nd.MarshalJSON() - assert.NoError(t, errMarshal) - - _, errDeltaDecode := reg.DeltaDecode(nd) - assert.NoError(t, errDeltaDecode) - } - - testJSONObj = []byte(`{ - "_key": "bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d", - "Name": "Pete", - "Age": 31 - }`) - - doc, err = client.NewDocFromJSON(testJSONObj) - assert.NoError(t, err) - - err = col.Update(ctx, doc) - assert.NoError(t, err) - - heads = clk.(*clock.MerkleClock).Heads() - cids, _, err = heads.List(ctx) - assert.NoError(t, err) - - for _, c := range cids { - b, err := db.Blockstore().Get(ctx, c) - assert.NoError(t, err) - - nd, err := dag.DecodeProtobuf(b.RawData()) - assert.NoError(t, err) - - _, err = nd.MarshalJSON() - assert.NoError(t, err) - - _, err = reg.DeltaDecode(nd) - assert.NoError(t, err) - } -} - -// collection with schema -func TestDBSchemaSaveSimpleDocument(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - testJSONObj := []byte(`{ - "Name": "John", - "Age": 21 - }`) - - doc, err := client.NewDocFromJSON(testJSONObj) - if err != nil { - t.Error(err) - return - } - - err = col.Save(ctx, doc) - assert.NoError(t, err) - - // value check - name, err := doc.Get("Name") - assert.NoError(t, err) - age, err := doc.Get("Age") - assert.NoError(t, err) - - assert.Equal(t, "John", name) - assert.Equal(t, int64(21), age) - - err = db.PrintDump(ctx) - assert.Nil(t, err) -} diff --git a/db/fetcher_test.go b/db/fetcher_test.go index 7c6cb20b71..f7de9bf036 100644 --- a/db/fetcher_test.go +++ b/db/fetcher_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/assert" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/fetcher" ) @@ -27,86 +26,3 @@ func TestFetcherStartWithoutInit(t *testing.T) { err := df.Start(ctx, core.Spans{}) assert.Error(t, err) } - -func TestFetcherGetAllPrimaryIndexEncodedDocSingle(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - doc, err := client.NewDocFromJSON([]byte(`{ - "Name": "John", - "Age": 21 - }`)) - assert.NoError(t, err) - err = col.Save(ctx, doc) - assert.NoError(t, err) - - txn, err := db.NewTxn(ctx, true) - if err != nil { - t.Error(err) - return - } - - // db.printDebugDB() - - df := new(fetcher.DocumentFetcher) - err = df.Init(ctx, txn, col, col.Schema().Fields, nil, nil, false, false) - assert.NoError(t, err) - - err = df.Start(ctx, core.Spans{}) - assert.NoError(t, err) - - encdoc, _, err := df.FetchNext(ctx) - assert.NoError(t, err) - assert.NotNil(t, encdoc) -} - -func TestFetcherGetAllPrimaryIndexEncodedDocMultiple(t *testing.T) { - ctx := context.Background() - db, err := newMemoryDB(ctx) - assert.NoError(t, err) - - col, err := newTestCollectionWithSchema(t, ctx, db) - assert.NoError(t, err) - - doc, err := client.NewDocFromJSON([]byte(`{ - "Name": "John", - "Age": 21 - }`)) - assert.NoError(t, err) - err = col.Save(ctx, doc) - assert.NoError(t, err) - - doc, err = client.NewDocFromJSON([]byte(`{ - "Name": "Alice", - "Age": 27 - }`)) - assert.NoError(t, err) - err = col.Save(ctx, doc) - assert.NoError(t, err) - - txn, err := db.NewTxn(ctx, true) - if err != nil { - t.Error(err) - return - } - - // db.printDebugDB() - - df := new(fetcher.DocumentFetcher) - err = df.Init(ctx, txn, col, col.Schema().Fields, nil, nil, false, false) - assert.NoError(t, err) - - err = df.Start(ctx, core.Spans{}) - assert.NoError(t, err) - - encdoc, _, err := df.FetchNext(ctx) - assert.NoError(t, err) - assert.NotNil(t, encdoc) - encdoc, _, err = df.FetchNext(ctx) - assert.NoError(t, err) - assert.NotNil(t, encdoc) -}