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 313deb4 commit 912424d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
47 changes: 47 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,6 +444,7 @@ func TestCreateIndex_WithMultipleCollectionsAndIndexes_AssignIncrementedIDPerCol

func TestCreateIndex_IfFailsToCreateTxn_ReturnError(t *testing.T) {
f := newIndexTestFixture(t)
defer f.db.Close()

testErr := errors.New("test error")

Expand All @@ -446,6 +458,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 +468,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 +514,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 +546,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 +558,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 +578,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 +593,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 +606,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 +624,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 +640,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 +678,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 +694,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 +709,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 +726,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 +738,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 +751,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 +812,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 +829,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 +891,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 +908,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 +926,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 +1015,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 +1028,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,13 +1038,15 @@ 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()

Expand All @@ -1025,6 +1062,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 +1081,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 +1103,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 +1114,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 +1132,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 +1157,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 +1208,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 +1224,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 +1243,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 +1252,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 912424d

Please sign in to comment.