Skip to content

Commit

Permalink
Move IndexTester out of test file (again) so that it is accessible fr…
Browse files Browse the repository at this point in the history
…om other projects
  • Loading branch information
mAdkins committed Jun 7, 2023
1 parent c41e6df commit 0ff2631
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
70 changes: 7 additions & 63 deletions mdb/index_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
package mdb

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -35,31 +31,31 @@ func (suite *indexTestSuite) TestIndexNone() {

func (suite *indexTestSuite) TestIndexOne() {
index1 := NewIndexDescription(true, "alpha")
suite.Require().NoError(suite.access.Index(suite.collection, index1))
suite.Require().NoError(suite.Access().Index(suite.collection, index1))
NewIndexTester().TestIndexes(suite.T(), suite.collection, index1)
}

func (suite *indexTestSuite) TestIndexTwo() {
index1 := NewIndexDescription(true, "alpha")
index2 := NewIndexDescription(true, "bravo")
suite.Require().NoError(suite.access.Index(suite.collection, index1))
suite.Require().NoError(suite.access.Index(suite.collection, index2))
suite.Require().NoError(suite.Access().Index(suite.collection, index1))
suite.Require().NoError(suite.Access().Index(suite.collection, index2))
NewIndexTester().TestIndexes(suite.T(), suite.collection, index1, index2)
}

func (suite *indexTestSuite) TestIndexThree() {
index1 := NewIndexDescription(true, "alpha")
index2 := NewIndexDescription(true, "bravo")
index3 := NewIndexDescription(true, "alpha", "bravo")
suite.Require().NoError(suite.access.Index(suite.collection, index1))
suite.Require().NoError(suite.access.Index(suite.collection, index2))
suite.Require().NoError(suite.access.Index(suite.collection, index3))
suite.Require().NoError(suite.Access().Index(suite.collection, index1))
suite.Require().NoError(suite.Access().Index(suite.collection, index2))
suite.Require().NoError(suite.Access().Index(suite.collection, index3))
NewIndexTester().TestIndexes(suite.T(), suite.collection, index1, index2, index3)
}

func (suite *indexTestSuite) TestIndexFinisher() {
index := NewIndexDescription(true, "alpha", "bravo")
collection, err := ConnectCollection(suite.access,
collection, err := ConnectCollection(suite.Access(),
&CollectionDefinition{
Name: "test-collection-index-finisher",
ValidationJSON: SimpleValidatorJSON,
Expand All @@ -71,55 +67,3 @@ func (suite *indexTestSuite) TestIndexFinisher() {
suite.NotNil(collection)
NewIndexTester().TestIndexes(suite.T(), collection, index)
}

// =============================================================================

// IndexTester provides a utility for verifying index creation.
type IndexTester []indexDatum

type indexDatum struct {
Name string
Key map[string]int32
Unique bool
}

func NewIndexTester() IndexTester {
return make(IndexTester, 0, 2)
}

func (it IndexTester) TestIndexes(t *testing.T, collection *Collection, descriptions ...*IndexDescription) {
ctx := context.Background()
cursor, err := collection.Indexes().List(ctx)
require.NoError(t, err)
err = cursor.All(ctx, &it)
require.NoError(t, err)
assert.Len(t, it, len(descriptions)+1)
it.hasIndexNamed(t, "_id_", NewIndexDescription(false, "_id"))
for _, description := range descriptions {
nameMap := make([]string, 0, len(description.keys))
for _, key := range description.keys {
nameMap = append(nameMap, key+"_1")
}
it.hasIndexNamed(t, strings.Join(nameMap, "_"), description)
}
}

func (it IndexTester) hasIndexNamed(t *testing.T, name string, description *IndexDescription) {
for _, data := range it {
if data.Name == name {
assert.Equal(t, description.unique, data.Unique, "check unique for index %s", name)
keyMap := make(map[string]int32, len(description.keys))
for _, key := range description.keys {
keyMap[key] = 1
}
assert.Equal(t, keyMap, data.Key, "check keys for index %s", name)
return
}
}

names := make([]string, 0, len(it))
for _, data := range it {
names = append(names, data.Name)
}
assert.Fail(t, "missing index", "no index %s (%s)", name, strings.Join(names, ", "))
}
60 changes: 60 additions & 0 deletions mdb/index_tester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package mdb

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// IndexTester provides a utility for verifying index creation.
type IndexTester []indexDatum

type indexDatum struct {
Name string
Key map[string]int32
Unique bool
}

func NewIndexTester() IndexTester {
return make(IndexTester, 0, 2)
}

func (it IndexTester) TestIndexes(t *testing.T, collection *Collection, descriptions ...*IndexDescription) {
ctx := context.Background()
cursor, err := collection.Indexes().List(ctx)
require.NoError(t, err)
err = cursor.All(ctx, &it)
require.NoError(t, err)
assert.Len(t, it, len(descriptions)+1)
it.hasIndexNamed(t, "_id_", NewIndexDescription(false, "_id"))
for _, description := range descriptions {
nameMap := make([]string, 0, len(description.keys))
for _, key := range description.keys {
nameMap = append(nameMap, key+"_1")
}
it.hasIndexNamed(t, strings.Join(nameMap, "_"), description)
}
}

func (it IndexTester) hasIndexNamed(t *testing.T, name string, description *IndexDescription) {
for _, data := range it {
if data.Name == name {
assert.Equal(t, description.unique, data.Unique, "check unique for index %s", name)
keyMap := make(map[string]int32, len(description.keys))
for _, key := range description.keys {
keyMap[key] = 1
}
assert.Equal(t, keyMap, data.Key, "check keys for index %s", name)
return
}
}

names := make([]string, 0, len(it))
for _, data := range it {
names = append(names, data.Name)
}
assert.Fail(t, "missing index", "no index %s (%s)", name, strings.Join(names, ", "))
}

0 comments on commit 0ff2631

Please sign in to comment.