Skip to content

Commit

Permalink
WIP - Close db in index unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Nov 15, 2023
1 parent befbe83 commit 41f8fb7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
51 changes: 51 additions & 0 deletions db/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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}},
Expand All @@ -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{
Expand Down Expand Up @@ -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

Expand All @@ -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 = "!"
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"))
Expand All @@ -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")
Expand All @@ -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")

Expand All @@ -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()
Expand All @@ -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")

Expand All @@ -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()

Expand All @@ -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",
Expand Down Expand Up @@ -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")

Expand All @@ -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)
Expand All @@ -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")

Expand All @@ -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"))
Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -784,6 +814,7 @@ func TestCollectionGetIndexes_IfSystemStoreFails_ReturnError(t *testing.T) {

for _, testCase := range testCases {
f := newIndexTestFixture(t)
defer f.db.Close()

f.createUserCollectionIndexOnName()

Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -1003,19 +1040,23 @@ 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))
}

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

Expand All @@ -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()

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()

Expand All @@ -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},
Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 41f8fb7

Please sign in to comment.