From 41f8fb73c56ccbb505b12e2d42cf73232ad3d790 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Wed, 15 Nov 2023 13:29:42 -0500 Subject: [PATCH] WIP - Close db in index unit tests --- db/index_test.go | 51 +++++++++++++++++++++++++++++++++++++++++ db/indexed_docs_test.go | 18 +++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/db/index_test.go b/db/index_test.go index e5682b551c..e85fd9bfb4 100644 --- a/db/index_test.go +++ b/db/index_test.go @@ -239,6 +239,7 @@ func (f *indexTestFixture) getCollectionIndexes(colName string) ([]client.IndexD func TestCreateIndex_IfFieldsIsEmpty_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() _, err := f.createCollectionIndex(client.IndexDescription{ Name: "some_index_name", @@ -248,6 +249,7 @@ func TestCreateIndex_IfFieldsIsEmpty_ReturnError(t *testing.T) { func TestCreateIndex_IfIndexDescriptionIDIsNotZero_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() for _, id := range []uint32{1, 20, 999} { desc := client.IndexDescription{ @@ -264,6 +266,7 @@ func TestCreateIndex_IfIndexDescriptionIDIsNotZero_ReturnError(t *testing.T) { func TestCreateIndex_IfValidInput_CreateIndex(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := client.IndexDescription{ Name: "some_index_name", @@ -279,6 +282,7 @@ func TestCreateIndex_IfValidInput_CreateIndex(t *testing.T) { func TestCreateIndex_IfFieldNameIsEmpty_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := client.IndexDescription{ Name: "some_index_name", @@ -292,6 +296,7 @@ func TestCreateIndex_IfFieldNameIsEmpty_ReturnError(t *testing.T) { func TestCreateIndex_IfFieldHasNoDirection_DefaultToAsc(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := client.IndexDescription{ Name: "some_index_name", @@ -304,6 +309,7 @@ func TestCreateIndex_IfFieldHasNoDirection_DefaultToAsc(t *testing.T) { func TestCreateIndex_IfSingleFieldInDescOrder_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := client.IndexDescription{ Fields: []client.IndexedFieldDescription{ @@ -316,6 +322,7 @@ func TestCreateIndex_IfSingleFieldInDescOrder_ReturnError(t *testing.T) { func TestCreateIndex_IfIndexWithNameAlreadyExists_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() name := "some_index_name" desc1 := client.IndexDescription{ @@ -334,6 +341,7 @@ func TestCreateIndex_IfIndexWithNameAlreadyExists_ReturnError(t *testing.T) { func TestCreateIndex_IfGeneratedNameMatchesExisting_AddIncrement(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() name := usersColName + "_" + usersAgeFieldName + "_ASC" desc1 := client.IndexDescription{ @@ -359,6 +367,7 @@ func TestCreateIndex_IfGeneratedNameMatchesExisting_AddIncrement(t *testing.T) { func TestCreateIndex_ShouldSaveToSystemStorage(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() name := "users_age_ASC" desc := client.IndexDescription{ @@ -380,6 +389,7 @@ func TestCreateIndex_ShouldSaveToSystemStorage(t *testing.T) { func TestCreateIndex_IfCollectionDoesntExist_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := client.IndexDescription{ Fields: []client.IndexedFieldDescription{{Name: productsPriceFieldName}}, @@ -391,6 +401,7 @@ func TestCreateIndex_IfCollectionDoesntExist_ReturnError(t *testing.T) { func TestCreateIndex_IfPropertyDoesntExist_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() const field = "non_existing_field" desc := client.IndexDescription{ @@ -433,10 +444,13 @@ func TestCreateIndex_WithMultipleCollectionsAndIndexes_AssignIncrementedIDPerCol func TestCreateIndex_IfFailsToCreateTxn_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() testErr := errors.New("test error") mockedRootStore := mocks.NewRootStore(t) + mockedRootStore.On("Close").Return(nil) + mockedRootStore.EXPECT().NewTransaction(mock.Anything, mock.Anything).Return(nil, testErr) f.db.rootstore = mockedRootStore @@ -446,6 +460,7 @@ func TestCreateIndex_IfFailsToCreateTxn_ReturnError(t *testing.T) { func TestCreateIndex_IfProvideInvalidIndexName_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() indexDesc := getUsersIndexDescOnName() indexDesc.Name = "!" @@ -455,6 +470,7 @@ func TestCreateIndex_IfProvideInvalidIndexName_ReturnError(t *testing.T) { func TestCreateIndex_ShouldUpdateCollectionsDescription(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() indOnName, err := f.users.CreateIndex(f.ctx, getUsersIndexDescOnName()) require.NoError(t, err) @@ -500,6 +516,7 @@ func TestCreateIndex_IfAttemptToIndexOnUnsupportedType_ReturnError(t *testing.T) func TestGetIndexes_ShouldReturnListOfAllExistingIndexes(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() usersIndexDesc := client.IndexDescription{ Name: "users_name_index", @@ -531,6 +548,7 @@ func TestGetIndexes_ShouldReturnListOfAllExistingIndexes(t *testing.T) { func TestGetIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() indexKey := core.NewCollectionIndexKey(usersColName, "users_name_index") err := f.txn.Systemstore().Put(f.ctx, indexKey.ToDS(), []byte("invalid")) @@ -542,6 +560,7 @@ func TestGetIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) { func TestGetIndexes_IfInvalidIndexKeyIsStored_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() indexKey := core.NewCollectionIndexKey(usersColName, "users_name_index") key := ds.NewKey(indexKey.ToString() + "/invalid") @@ -561,6 +580,7 @@ func TestGetIndexes_IfInvalidIndexKeyIsStored_ReturnError(t *testing.T) { func TestGetIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() testErr := errors.New("test error") @@ -575,6 +595,7 @@ func TestGetIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { func TestGetIndexes_IfSystemStoreFails_ShouldCloseIterator(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() mockedTxn := f.mockTxn() mockedTxn.MockSystemstore.EXPECT().Query(mock.Anything, mock.Anything).Unset() @@ -587,6 +608,7 @@ func TestGetIndexes_IfSystemStoreFails_ShouldCloseIterator(t *testing.T) { func TestGetIndexes_IfSystemStoreQueryIteratorFails_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() testErr := errors.New("test error") @@ -604,6 +626,7 @@ func TestGetIndexes_IfSystemStoreQueryIteratorFails_ReturnError(t *testing.T) { func TestGetIndexes_IfSystemStoreHasInvalidData_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() mockedTxn := f.mockTxn() @@ -619,6 +642,7 @@ func TestGetIndexes_IfSystemStoreHasInvalidData_ReturnError(t *testing.T) { func TestGetCollectionIndexes_ShouldReturnListOfCollectionIndexes(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() usersIndexDesc := client.IndexDescription{ Name: "users_name_index", @@ -656,6 +680,7 @@ func TestGetCollectionIndexes_ShouldReturnListOfCollectionIndexes(t *testing.T) func TestGetCollectionIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() testErr := errors.New("test error") @@ -671,6 +696,7 @@ func TestGetCollectionIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { func TestGetCollectionIndexes_IfSystemStoreFails_ShouldCloseIterator(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() mockedTxn := f.mockTxn() mockedTxn.MockSystemstore = mocks.NewDSReaderWriter(t) @@ -685,6 +711,7 @@ func TestGetCollectionIndexes_IfSystemStoreFails_ShouldCloseIterator(t *testing. func TestGetCollectionIndexes_IfSystemStoreQueryIteratorFails_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() testErr := errors.New("test error") @@ -701,6 +728,7 @@ func TestGetCollectionIndexes_IfSystemStoreQueryIteratorFails_ReturnError(t *tes func TestGetCollectionIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() indexKey := core.NewCollectionIndexKey(usersColName, "users_name_index") err := f.txn.Systemstore().Put(f.ctx, indexKey.ToDS(), []byte("invalid")) @@ -712,6 +740,7 @@ func TestGetCollectionIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) { func TestCollectionGetIndexes_ShouldReturnIndexes(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -724,6 +753,7 @@ func TestCollectionGetIndexes_ShouldReturnIndexes(t *testing.T) { func TestCollectionGetIndexes_ShouldCloseQueryIterator(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -784,6 +814,7 @@ func TestCollectionGetIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { for _, testCase := range testCases { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -800,6 +831,7 @@ func TestCollectionGetIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { func TestCollectionGetIndexes_IfFailsToCreateTxn_ShouldNotCache(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -861,6 +893,7 @@ func TestCollectionGetIndexes_IfStoredIndexWithUnsupportedType_ReturnError(t *te func TestCollectionGetIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() f.createUserCollectionIndexOnAge() @@ -877,6 +910,7 @@ func TestCollectionGetIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) { func TestCollectionGetIndexes_IfIndexIsCreated_ReturnUpdateIndexes(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -894,6 +928,7 @@ func TestCollectionGetIndexes_IfIndexIsCreated_ReturnUpdateIndexes(t *testing.T) func TestCollectionGetIndexes_IfIndexIsDropped_ReturnUpdateIndexes(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() f.createUserCollectionIndexOnAge() @@ -982,6 +1017,7 @@ func TestCollectionGetIndexes_ShouldReturnIndexesInOrderedByName(t *testing.T) { func TestDropIndex_ShouldDeleteIndex(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := f.createUserCollectionIndexOnName() err := f.dropIndex(usersColName, desc.Name) @@ -994,6 +1030,7 @@ func TestDropIndex_ShouldDeleteIndex(t *testing.T) { func TestDropIndex_IfStorageFails_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := f.createUserCollectionIndexOnName() f.db.Close() @@ -1003,6 +1040,7 @@ func TestDropIndex_IfStorageFails_ReturnError(t *testing.T) { func TestDropIndex_IfCollectionDoesntExist_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() err := f.dropIndex(productsColName, "any_name") assert.ErrorIs(t, err, NewErrCanNotReadCollection(usersColName, nil)) @@ -1010,12 +1048,15 @@ func TestDropIndex_IfCollectionDoesntExist_ReturnError(t *testing.T) { func TestDropIndex_IfFailsToCreateTxn_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() testErr := errors.New("test error") mockedRootStore := mocks.NewRootStore(t) + mockedRootStore.On("Close").Return(nil) + mockedRootStore.EXPECT().NewTransaction(mock.Anything, mock.Anything).Return(nil, testErr) f.db.rootstore = mockedRootStore @@ -1025,6 +1066,7 @@ func TestDropIndex_IfFailsToCreateTxn_ReturnError(t *testing.T) { func TestDropIndex_IfFailsToDeleteFromStorage_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -1043,6 +1085,7 @@ func TestDropIndex_IfFailsToDeleteFromStorage_ReturnError(t *testing.T) { func TestDropIndex_ShouldUpdateCollectionsDescription(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() col := f.users.WithTxn(f.txn) _, err := col.CreateIndex(f.ctx, getUsersIndexDescOnName()) require.NoError(t, err) @@ -1064,6 +1107,7 @@ func TestDropIndex_ShouldUpdateCollectionsDescription(t *testing.T) { func TestDropIndex_IfIndexWithNameDoesNotExist_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() const name = "not_existing_index" err := f.users.DropIndex(f.ctx, name) @@ -1074,6 +1118,7 @@ func TestDropIndex_IfSystemStoreFails_ReturnError(t *testing.T) { testErr := errors.New("test error") f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() @@ -1091,6 +1136,7 @@ func TestDropIndex_IfSystemStoreFails_ReturnError(t *testing.T) { func TestDropAllIndexes_ShouldDeleteAllIndexes(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() _, err := f.createCollectionIndexFor(usersColName, client.IndexDescription{ Fields: []client.IndexedFieldDescription{ {Name: usersNameFieldName, Direction: client.Ascending}, @@ -1115,6 +1161,7 @@ func TestDropAllIndexes_ShouldDeleteAllIndexes(t *testing.T) { func TestDropAllIndexes_IfStorageFails_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() f.db.Close() @@ -1165,6 +1212,7 @@ func TestDropAllIndexes_IfSystemStorageFails_ReturnError(t *testing.T) { for _, testCase := range testCases { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() mockedTxn := f.mockTxn() @@ -1180,6 +1228,7 @@ func TestDropAllIndexes_IfSystemStorageFails_ReturnError(t *testing.T) { func TestDropAllIndexes_ShouldCloseQueryIterator(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() mockedTxn := f.mockTxn() @@ -1198,6 +1247,7 @@ func TestDropAllIndexes_ShouldCloseQueryIterator(t *testing.T) { func TestNewCollectionIndex_IfDescriptionHasNoFields_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := getUsersIndexDescOnName() desc.Fields = nil _, err := NewCollectionIndex(f.users, desc) @@ -1206,6 +1256,7 @@ func TestNewCollectionIndex_IfDescriptionHasNoFields_ReturnError(t *testing.T) { func TestNewCollectionIndex_IfDescriptionHasNonExistingField_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() desc := getUsersIndexDescOnName() desc.Fields[0].Name = "non_existing_field" _, err := NewCollectionIndex(f.users, desc) diff --git a/db/indexed_docs_test.go b/db/indexed_docs_test.go index 6503429c96..f1f8d6270f 100644 --- a/db/indexed_docs_test.go +++ b/db/indexed_docs_test.go @@ -249,6 +249,7 @@ func (f *indexTestFixture) stubSystemStore(systemStoreOn *mocks.DSReaderWriter_E func TestNonUnique_IfDocIsAdded_ShouldBeIndexed(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -263,6 +264,7 @@ func TestNonUnique_IfDocIsAdded_ShouldBeIndexed(t *testing.T) { func TestNonUnique_IfFailsToStoredIndexedDoc_Error(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -281,6 +283,7 @@ func TestNonUnique_IfFailsToStoredIndexedDoc_Error(t *testing.T) { func TestNonUnique_IfDocDoesNotHaveIndexedField_SkipIndex(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() data, err := json.Marshal(struct { @@ -302,6 +305,7 @@ func TestNonUnique_IfDocDoesNotHaveIndexedField_SkipIndex(t *testing.T) { func TestNonUnique_IfSystemStorageHasInvalidIndexDescription_Error(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -317,6 +321,7 @@ func TestNonUnique_IfSystemStorageHasInvalidIndexDescription_Error(t *testing.T) func TestNonUnique_IfSystemStorageFailsToReadIndexDesc_Error(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -334,6 +339,7 @@ func TestNonUnique_IfSystemStorageFailsToReadIndexDesc_Error(t *testing.T) { func TestNonUnique_IfIndexIntField_StoreIt(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnAge() doc := f.newUserDoc("John", 21) @@ -379,6 +385,7 @@ func TestNonUnique_IfMultipleCollectionsWithIndexes_StoreIndexWithCollectionID(t func TestNonUnique_IfMultipleIndexes_StoreIndexWithIndexID(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() f.createUserCollectionIndexOnAge() @@ -482,6 +489,7 @@ func TestNonUnique_StoringIndexedFieldValueOfDifferentTypes(t *testing.T) { func TestNonUnique_IfIndexedFieldIsNil_StoreItAsNil(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() docJSON, err := json.Marshal(struct { @@ -504,6 +512,7 @@ func TestNonUnique_IfIndexedFieldIsNil_StoreItAsNil(t *testing.T) { func TestNonUniqueCreate_ShouldIndexExistingDocs(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() doc1 := f.newUserDoc("John", 21) f.saveDocToCollection(doc1, f.users) @@ -578,6 +587,7 @@ func TestNonUniqueCreate_IfUponIndexingExistingDocsFetcherFails_ReturnError(t *t for _, tc := range cases { f := newIndexTestFixture(t) + defer f.db.Close() doc := f.newUserDoc("John", 21) f.saveDocToCollection(doc, f.users) @@ -595,6 +605,7 @@ func TestNonUniqueCreate_IfUponIndexingExistingDocsFetcherFails_ReturnError(t *t func TestNonUniqueCreate_IfDatastoreFailsToStoreIndex_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() doc := f.newUserDoc("John", 21) f.saveDocToCollection(doc, f.users) @@ -652,6 +663,7 @@ func TestNonUniqueDrop_ShouldDeleteStoredIndexedFields(t *testing.T) { func TestNonUniqueUpdate_ShouldDeleteOldValueAndStoreNewOne(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() cases := []struct { @@ -698,6 +710,7 @@ func TestNonUniqueUpdate_ShouldDeleteOldValueAndStoreNewOne(t *testing.T) { func TestNonUniqueUpdate_IfFailsToReadIndexDescription_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -786,6 +799,7 @@ func TestNonUniqueUpdate_IfFetcherFails_ReturnError(t *testing.T) { t.Log(tc.Name) f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -810,6 +824,7 @@ func TestNonUniqueUpdate_IfFetcherFails_ReturnError(t *testing.T) { func TestNonUniqueUpdate_IfFailsToUpdateIndex_ReturnError(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnAge() doc := f.newUserDoc("John", 21) @@ -829,6 +844,7 @@ func TestNonUniqueUpdate_IfFailsToUpdateIndex_ReturnError(t *testing.T) { func TestNonUniqueUpdate_ShouldPassToFetcherOnlyRelevantFields(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() f.createUserCollectionIndexOnAge() @@ -891,6 +907,7 @@ func TestNonUniqueUpdate_IfDatastoreFails_ReturnError(t *testing.T) { t.Log(tc.Name) f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() doc := f.newUserDoc("John", 21) @@ -922,6 +939,7 @@ func TestNonUniqueUpdate_IfDatastoreFails_ReturnError(t *testing.T) { func TestNonUpdate_IfIndexedFieldWasNil_ShouldDeleteIt(t *testing.T) { f := newIndexTestFixture(t) + defer f.db.Close() f.createUserCollectionIndexOnName() docJSON, err := json.Marshal(struct {