Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests for indexes #3531

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions internal/backends/postgresql/metadata/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,46 @@ func TestRenameCollection(t *testing.T) {
})
}

func TestMetadataIndexes(t *testing.T) {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
if testing.Short() {
t.Skip("skipping in -short mode")
}

t.Parallel()

connInfo := conninfo.New()
ctx := conninfo.Ctx(testutil.Ctx(t), connInfo)

_, db, dbName := createDatabase(t, ctx)

var sql string
err := db.QueryRow(
ctx,
"SELECT indexdef FROM pg_indexes WHERE schemaname = $1 AND tablename = $2 AND indexname = $3",
dbName, metadataTableName, metadataTableName+"_id_idx",
).Scan(&sql)
require.NoError(t, err)

expected := fmt.Sprintf(
`CREATE UNIQUE INDEX %s ON %q.%s USING btree (((_jsonb -> '_id'::text)))`,
metadataTableName+"_id_idx", dbName, metadataTableName,
)
assert.Equal(t, expected, sql)

err = db.QueryRow(
ctx,
"SELECT indexdef FROM pg_indexes WHERE schemaname = $1 AND tablename = $2 AND indexname = $3",
dbName, metadataTableName, metadataTableName+"_table_idx",
).Scan(&sql)
assert.NoError(t, err)

expected = fmt.Sprintf(
`CREATE UNIQUE INDEX %s ON %q.%s USING btree (((_jsonb -> 'table'::text)))`,
metadataTableName+"_table_idx", dbName, metadataTableName,
)
assert.Equal(t, expected, sql)
}

func TestIndexesCreateDrop(t *testing.T) {
if testing.Short() {
t.Skip("skipping in -short mode")
Expand Down Expand Up @@ -552,7 +592,8 @@ func TestIndexesCreateDrop(t *testing.T) {
})

t.Run("CheckSettingsAfterCreation", func(t *testing.T) {
err := r.initCollections(ctx, dbName, db)
// Force DBs and collection initialization to check that indexes metadata is stored correctly in the database.
_, err = r.getPool(ctx)
require.NoError(t, err)

var refreshedCollection *Collection
Expand Down Expand Up @@ -589,8 +630,8 @@ func TestIndexesCreateDrop(t *testing.T) {
require.NoError(t, row.Scan(&count))
require.Equal(t, 2, count) // only default index and index_unique should be left

// check settings after dropping indexes
err = r.initCollections(ctx, dbName, db)
// Force DBs and collection initialization to check index metadata after deletion.
_, err = r.getPool(ctx)
require.NoError(t, err)

collection, err = r.CollectionGet(ctx, dbName, collectionName)
Expand All @@ -608,37 +649,6 @@ func TestIndexesCreateDrop(t *testing.T) {
}
}
})

t.Run("MetadataIndexes", func(t *testing.T) {
t.Parallel()

var sql string
err := db.QueryRow(
ctx,
"SELECT indexdef FROM pg_indexes WHERE schemaname = $1 AND tablename = $2 AND indexname = $3",
dbName, metadataTableName, metadataTableName+"_id_idx",
).Scan(&sql)
require.NoError(t, err)

expected := fmt.Sprintf(
`CREATE UNIQUE INDEX %s ON %q.%s USING btree (((_jsonb -> '_id'::text)))`,
metadataTableName+"_id_idx", dbName, metadataTableName,
)
require.Equal(t, expected, sql)

err = db.QueryRow(
ctx,
"SELECT indexdef FROM pg_indexes WHERE schemaname = $1 AND tablename = $2 AND indexname = $3",
dbName, metadataTableName, metadataTableName+"_table_idx",
).Scan(&sql)
assert.NoError(t, err)

expected = fmt.Sprintf(
`CREATE UNIQUE INDEX %s ON %q.%s USING btree (((_jsonb -> 'table'::text)))`,
metadataTableName+"_table_idx", dbName, metadataTableName,
)
assert.Equal(t, expected, sql)
})
}

func TestLongIndexNames(t *testing.T) {
Expand All @@ -651,7 +661,7 @@ func TestLongIndexNames(t *testing.T) {
connInfo := conninfo.New()
ctx := conninfo.Ctx(testutil.Ctx(t), connInfo)

r, db, dbName := createDatabase(t, ctx)
r, _, dbName := createDatabase(t, ctx)

batch1 := []IndexInfo{{
Name: strings.Repeat("aB", 75),
Expand Down Expand Up @@ -719,7 +729,8 @@ func TestLongIndexNames(t *testing.T) {
err = r.IndexesCreate(ctx, dbName, tc.collectionName, batch2)
require.NoError(t, err)

err = r.initCollections(ctx, dbName, db)
// Force DBs and collection initialization to check that indexes metadata is stored correctly in the database.
_, err = r.getPool(ctx)
require.NoError(t, err)

collection, err = r.CollectionGet(ctx, dbName, tc.collectionName)
Expand Down
6 changes: 6 additions & 0 deletions internal/backends/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
}

// collectionsStats returns statistics about tables and indexes for the given collections.
//
// If the list of collections is empty, then stats filled with zero values is returned.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
func collectionsStats(ctx context.Context, p *pgxpool.Pool, dbName string, list []*metadata.Collection) (*stats, error) {
if len(list) == 0 {
return new(stats), nil
}

Check warning on line 47 in internal/backends/postgresql/postgresql.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/postgresql.go#L46-L47

Added lines #L46 - L47 were not covered by tests

var err error

// TODO https://github.com/FerretDB/FerretDB/issues/3518
Expand Down
Loading