mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-27 10:35:36 -06:00
added integration tests for migration, schema
This commit is contained in:
148
go/libraries/doltcore/migrate/integration_test.go
Normal file
148
go/libraries/doltcore/migrate/integration_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
// Copyright 2022 Dolthub, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package migrate_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/commands"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/dtestutils"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/env"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/migrate"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
|
||||
"github.com/dolthub/dolt/go/store/chunks"
|
||||
"github.com/dolthub/dolt/go/store/datas"
|
||||
"github.com/dolthub/dolt/go/store/prolly/tree"
|
||||
"github.com/dolthub/dolt/go/store/types"
|
||||
)
|
||||
|
||||
type migrationTest struct {
|
||||
name string
|
||||
setup []string
|
||||
asserts []assertion
|
||||
err string
|
||||
}
|
||||
|
||||
type assertion struct {
|
||||
query string
|
||||
expected []sql.Row
|
||||
}
|
||||
|
||||
func TestMigration(t *testing.T) {
|
||||
tests := []migrationTest{
|
||||
{
|
||||
name: "smoke test",
|
||||
setup: []string{
|
||||
"CREATE TABLE test (pk int primary key)",
|
||||
"INSERT INTO test VALUES (1),(2),(3)",
|
||||
"CALL dolt_add('.')",
|
||||
"CALL dolt_commit('-am', 'new table')",
|
||||
},
|
||||
asserts: []assertion{
|
||||
{
|
||||
query: "SELECT * FROM test",
|
||||
expected: []sql.Row{{int32(1)}, {int32(2)}, {int32(3)}},
|
||||
},
|
||||
{
|
||||
query: "SELECT count(*) FROM dolt_log",
|
||||
expected: []sql.Row{{int64(2)}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
preEnv := runSetupSql(t, ctx, test.setup)
|
||||
|
||||
postEnv := runMigration(t, ctx, preEnv)
|
||||
|
||||
ddb := postEnv.DoltDB
|
||||
root, err := postEnv.WorkingRoot(ctx)
|
||||
require.NoError(t, err)
|
||||
for _, a := range test.asserts {
|
||||
actual, err := sqle.ExecuteSelect(t, postEnv, ddb, root, a.query)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, a.expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func runSetupSql(t *testing.T, ctx context.Context, setup []string) *env.DoltEnv {
|
||||
dEnv := dtestutils.CreateTestEnv()
|
||||
cmd := commands.SqlCmd{}
|
||||
for _, query := range setup {
|
||||
code := cmd.Exec(ctx, cmd.Name(), []string{"-q", query}, dEnv)
|
||||
require.Equal(t, 0, code)
|
||||
}
|
||||
return dEnv
|
||||
}
|
||||
|
||||
func runMigration(t *testing.T, ctx context.Context, preEnv *env.DoltEnv) (postEnv *env.DoltEnv) {
|
||||
ddb, err := initTestMigrationDB(ctx)
|
||||
require.NoError(t, err)
|
||||
postEnv = &env.DoltEnv{
|
||||
Version: preEnv.Version,
|
||||
Config: preEnv.Config,
|
||||
RepoState: preEnv.RepoState,
|
||||
FS: preEnv.FS,
|
||||
DoltDB: ddb,
|
||||
}
|
||||
|
||||
err = migrate.TraverseDAG(ctx, preEnv.DoltDB, postEnv.DoltDB)
|
||||
assert.NoError(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
func initTestMigrationDB(ctx context.Context) (*doltdb.DoltDB, error) {
|
||||
var db datas.Database
|
||||
storage := &chunks.MemoryStorage{}
|
||||
cs := storage.NewViewWithFormat("__DOLT__")
|
||||
vrw := types.NewValueStore(cs)
|
||||
ns := tree.NewNodeStore(cs)
|
||||
db = datas.NewTypesDatabase(vrw, ns)
|
||||
|
||||
name, email := "user", "user@fake.horse"
|
||||
meta, err := datas.NewCommitMeta(name, email, "test migration")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rv, err := doltdb.EmptyRootValue(ctx, vrw, ns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv := doltdb.HackNomsValuesFromRootValues(rv)
|
||||
|
||||
ds, err := db.GetDataset(ctx, ref.NewInternalRef("migration").String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = db.Commit(ctx, ds, nv, datas.CommitOptions{Meta: meta})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return doltdb.DoltDBFromCS(cs), nil
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
|
||||
"github.com/dolthub/dolt/go/store/chunks"
|
||||
"github.com/dolthub/dolt/go/store/datas"
|
||||
"github.com/dolthub/dolt/go/store/hash"
|
||||
"github.com/dolthub/dolt/go/store/prolly"
|
||||
@@ -170,7 +171,8 @@ func migrateCommit(ctx context.Context, oldCm *doltdb.Commit, new *doltdb.DoltDB
|
||||
if err = new.SetHead(ctx, flushRef, newHash); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = new.ShallowGC(ctx); err != nil {
|
||||
err = new.ShallowGC(ctx)
|
||||
if err != nil && err != chunks.ErrUnsupportedOperation {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -16,18 +16,19 @@ package schema_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/commands"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/dtestutils"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
|
||||
)
|
||||
|
||||
func TestSqlIntegration(t *testing.T) {
|
||||
|
||||
const tblName = "test"
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -49,16 +50,8 @@ func TestSqlIntegration(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dEnv := dtestutils.CreateTestEnv()
|
||||
cmd := commands.SqlCmd{}
|
||||
root := runTestSql(t, ctx, test.setup)
|
||||
|
||||
for _, query := range test.setup {
|
||||
code := cmd.Exec(ctx, cmd.Name(), []string{"-q", query}, dEnv)
|
||||
require.Equal(t, 0, code)
|
||||
}
|
||||
|
||||
root, err := dEnv.WorkingRoot(ctx)
|
||||
require.NoError(t, err)
|
||||
tbl, ok, err := root.GetTable(ctx, tblName)
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
@@ -70,3 +63,75 @@ func TestSqlIntegration(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetKeyTags(t *testing.T) {
|
||||
const tblName = "test"
|
||||
tests := []struct {
|
||||
name string
|
||||
setup []string
|
||||
keyCols []string
|
||||
}{
|
||||
{
|
||||
name: "primary key",
|
||||
setup: []string{"CREATE TABLE test (pk int PRIMARY KEY, c0 int);"},
|
||||
keyCols: []string{"pk"},
|
||||
},
|
||||
{
|
||||
name: "keyless",
|
||||
setup: []string{"CREATE TABLE test (c0 int, c1 int);"},
|
||||
keyCols: nil,
|
||||
},
|
||||
{
|
||||
name: "secondary index",
|
||||
setup: []string{"CREATE TABLE test (pk int PRIMARY KEY, c0 int, c1 int, INDEX(c0));"},
|
||||
keyCols: []string{"pk", "c0"},
|
||||
},
|
||||
{
|
||||
name: "compound index",
|
||||
setup: []string{"CREATE TABLE test (pk int PRIMARY KEY, c0 int, c1 int, INDEX(c1, c0));"},
|
||||
keyCols: []string{"pk", "c0", "c1"},
|
||||
},
|
||||
{
|
||||
name: "keyless secondary index",
|
||||
setup: []string{"CREATE TABLE test (c0 int, c1 int, INDEX(c1));"},
|
||||
keyCols: []string{"c1"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
root := runTestSql(t, ctx, test.setup)
|
||||
|
||||
tbl, ok, err := root.GetTable(ctx, tblName)
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
sch, err := tbl.GetSchema(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
all := sch.GetAllCols()
|
||||
expected := make([]uint64, len(test.keyCols))
|
||||
for i, name := range test.keyCols {
|
||||
expected[i] = all.LowerNameToCol[name].Tag
|
||||
}
|
||||
sort.Slice(expected, func(i, j int) bool {
|
||||
return expected[i] < expected[j]
|
||||
})
|
||||
|
||||
actual := schema.GetKeyColumnTags(sch)
|
||||
assert.Equal(t, expected, actual.AsSlice())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func runTestSql(t *testing.T, ctx context.Context, setup []string) *doltdb.RootValue {
|
||||
dEnv := dtestutils.CreateTestEnv()
|
||||
cmd := commands.SqlCmd{}
|
||||
for _, query := range setup {
|
||||
code := cmd.Exec(ctx, cmd.Name(), []string{"-q", query}, dEnv)
|
||||
require.Equal(t, 0, code)
|
||||
}
|
||||
root, err := dEnv.WorkingRoot(ctx)
|
||||
require.NoError(t, err)
|
||||
return root
|
||||
}
|
||||
|
||||
@@ -333,10 +333,6 @@ func TestArePrimaryKeySetsDiffableTypeChanges(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetKeyColumnTags(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func testSchema(method string, sch Schema, t *testing.T) {
|
||||
validateCols(t, allCols, sch.GetAllCols(), method+"GetAllCols")
|
||||
validateCols(t, pkCols, sch.GetPKCols(), method+"GetPKCols")
|
||||
|
||||
Reference in New Issue
Block a user