Skip to content

Commit

Permalink
Using ExecuteSqls() to setup schema/objects specific to each test ins…
Browse files Browse the repository at this point in the history
…tead of depending on a common global init sql script
  • Loading branch information
sanyamsinghal committed Dec 21, 2024
1 parent 788ea74 commit 5e6292c
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 178 deletions.
2 changes: 1 addition & 1 deletion yb-voyager/src/srcdb/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestMain(m *testing.M) {

// setting source db type, version and defaults
postgresContainer := testcontainers.NewTestContainer("postgresql", nil)
// TODO: handle error
err := postgresContainer.Start(ctx)
if err != nil {
utils.ErrExit("Failed to start postgres container: %v", err)
Expand Down Expand Up @@ -82,6 +81,7 @@ func TestMain(m *testing.M) {
if err != nil {
utils.ErrExit("%v", err)
}

testOracleSource = &TestDB{
TestContainer: oracleContainer,
Source: &Source{
Expand Down
94 changes: 75 additions & 19 deletions yb-voyager/src/srcdb/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,79 @@ import (
)

func TestPostgresGetAllTableNames(t *testing.T) {
testPostgresSource.TestContainer.ExecuteSqls(
`CREATE SCHEMA test_schema;`,
`CREATE TABLE test_schema.foo (
id INT PRIMARY KEY,
name VARCHAR
);`,
`INSERT into test_schema.foo values (1, 'abc'), (2, 'xyz');`,
`CREATE TABLE test_schema.bar (
id INT PRIMARY KEY,
name VARCHAR
);`,
`INSERT into test_schema.bar values (1, 'abc'), (2, 'xyz');`,
`CREATE TABLE test_schema.non_pk1(
id INT,
name VARCHAR(255)
);`)
defer testPostgresSource.TestContainer.ExecuteSqls(`DROP SCHEMA test_schema CASCADE;`)

sqlname.SourceDBType = "postgresql"
testPostgresSource.Source.Schema = "test_schema"

// Test GetAllTableNames
actualTables := testPostgresSource.DB().GetAllTableNames()
expectedTables := []*sqlname.SourceName{
sqlname.NewSourceName("public", "foo"),
sqlname.NewSourceName("public", "bar"),
sqlname.NewSourceName("public", "table1"),
sqlname.NewSourceName("public", "table2"),
sqlname.NewSourceName("public", "unique_table"),
sqlname.NewSourceName("public", "non_pk1"),
sqlname.NewSourceName("public", "non_pk2"),
sqlname.NewSourceName("test_schema", "foo"),
sqlname.NewSourceName("test_schema", "bar"),
sqlname.NewSourceName("test_schema", "non_pk1"),
}
assert.Equal(t, len(expectedTables), len(actualTables), "Expected number of tables to match")

testutils.AssertEqualSourceNameSlices(t, expectedTables, actualTables)
}

func TestPostgresGetTableToUniqueKeyColumnsMap(t *testing.T) {
objectName := sqlname.NewObjectName("postgresql", "public", "public", "unique_table")

// Test GetTableToUniqueKeyColumnsMap
tableList := []sqlname.NameTuple{
{CurrentName: objectName},
testPostgresSource.TestContainer.ExecuteSqls(
`CREATE SCHEMA test_schema;`,
`CREATE TABLE test_schema.unique_table (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE,
phone VARCHAR(20) UNIQUE,
address VARCHAR(255) UNIQUE
);`,
`INSERT INTO test_schema.unique_table (email, phone, address) VALUES
('[email protected]', '1234567890', '123 Elm Street'),
('[email protected]', '0987654321', '456 Oak Avenue');`,
`CREATE TABLE test_schema.another_unique_table (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE,
age INT
);`,
`CREATE UNIQUE INDEX idx_age ON test_schema.another_unique_table(age);`,
`INSERT INTO test_schema.another_unique_table (username, age) VALUES
('user1', 30),
('user2', 40);`)
defer testPostgresSource.TestContainer.ExecuteSqls(`DROP SCHEMA test_schema CASCADE;`)

uniqueTablesList := []sqlname.NameTuple{
{CurrentName: sqlname.NewObjectName("postgresql", "test_schema", "test_schema", "unique_table")},
{CurrentName: sqlname.NewObjectName("postgresql", "test_schema", "test_schema", "another_unique_table")},
}
uniqueKeys, err := testPostgresSource.DB().GetTableToUniqueKeyColumnsMap(tableList)

actualUniqKeys, err := testPostgresSource.DB().GetTableToUniqueKeyColumnsMap(uniqueTablesList)
if err != nil {
t.Fatalf("Error retrieving unique keys: %v", err)
}

expectedKeys := map[string][]string{
"unique_table": {"email", "phone", "address"},
expectedUniqKeys := map[string][]string{
"test_schema.unique_table": {"email", "phone", "address"},
"test_schema.another_unique_table": {"username", "age"},
}

// Compare the maps by iterating over each table and asserting the columns list
for table, expectedColumns := range expectedKeys {
actualColumns, exists := uniqueKeys[table]
for table, expectedColumns := range expectedUniqKeys {
actualColumns, exists := actualUniqKeys[table]
if !exists {
t.Errorf("Expected table %s not found in uniqueKeys", table)
}
Expand All @@ -72,9 +108,29 @@ func TestPostgresGetTableToUniqueKeyColumnsMap(t *testing.T) {
}

func TestPostgresGetNonPKTables(t *testing.T) {
testPostgresSource.TestContainer.ExecuteSqls(
`CREATE SCHEMA test_schema;`,
`CREATE TABLE test_schema.table1 (
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);`,
`CREATE TABLE test_schema.table2 (
id SERIAL PRIMARY KEY,
email VARCHAR(100)
);`,
`CREATE TABLE test_schema.non_pk1(
id INT,
name VARCHAR(255)
);`,
`CREATE TABLE test_schema.non_pk2(
id INT,
name VARCHAR(255)
);`)
defer testPostgresSource.TestContainer.ExecuteSqls(`DROP SCHEMA test_schema CASCADE;`)

actualTables, err := testPostgresSource.DB().GetNonPKTables()
assert.NilError(t, err, "Expected nil but non nil error: %v", err)

expectedTables := []string{`public."non_pk2"`, `public."non_pk1"`} // func returns table.Qualified.Quoted
expectedTables := []string{`test_schema."non_pk2"`, `test_schema."non_pk1"`} // func returns table.Qualified.Quoted
testutils.AssertEqualStringSlices(t, expectedTables, actualTables)
}
95 changes: 76 additions & 19 deletions yb-voyager/src/srcdb/yugbaytedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,79 @@ import (
)

func TestYugabyteGetAllTableNames(t *testing.T) {
sqlname.SourceDBType = "yugabytedb"
testYugabyteDBSource.TestContainer.ExecuteSqls(
`CREATE SCHEMA test_schema;`,
`CREATE TABLE test_schema.foo (
id INT PRIMARY KEY,
name VARCHAR
);`,
`INSERT into test_schema.foo values (1, 'abc'), (2, 'xyz');`,
`CREATE TABLE test_schema.bar (
id INT PRIMARY KEY,
name VARCHAR
);`,
`INSERT into test_schema.bar values (1, 'abc'), (2, 'xyz');`,
`CREATE TABLE test_schema.non_pk1(
id INT,
name VARCHAR(255)
);`)
defer testYugabyteDBSource.TestContainer.ExecuteSqls(`DROP SCHEMA test_schema CASCADE;`)

sqlname.SourceDBType = "postgresql"
testYugabyteDBSource.Source.Schema = "test_schema"

// Test GetAllTableNames
actualTables := testYugabyteDBSource.DB().GetAllTableNames()
expectedTables := []*sqlname.SourceName{
sqlname.NewSourceName("public", "foo"),
sqlname.NewSourceName("public", "bar"),
sqlname.NewSourceName("public", "table1"),
sqlname.NewSourceName("public", "table2"),
sqlname.NewSourceName("public", "unique_table"),
sqlname.NewSourceName("public", "non_pk1"),
sqlname.NewSourceName("public", "non_pk2"),
sqlname.NewSourceName("test_schema", "foo"),
sqlname.NewSourceName("test_schema", "bar"),
sqlname.NewSourceName("test_schema", "non_pk1"),
}
assert.Equal(t, len(expectedTables), len(actualTables), "Expected number of tables to match")

testutils.AssertEqualSourceNameSlices(t, expectedTables, actualTables)
}

func TestYugabyteGetTableToUniqueKeyColumnsMap(t *testing.T) {
objectName := sqlname.NewObjectName("yugabytedb", "public", "public", "unique_table")
testYugabyteDBSource.TestContainer.ExecuteSqls(
`CREATE SCHEMA test_schema;`,
`CREATE TABLE test_schema.unique_table (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE,
phone VARCHAR(20) UNIQUE,
address VARCHAR(255) UNIQUE
);`,
`INSERT INTO test_schema.unique_table (email, phone, address) VALUES
('[email protected]', '1234567890', '123 Elm Street'),
('[email protected]', '0987654321', '456 Oak Avenue');`,
`CREATE TABLE test_schema.another_unique_table (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE,
age INT
);`,
`CREATE UNIQUE INDEX idx_age ON test_schema.another_unique_table(age);`,
`INSERT INTO test_schema.another_unique_table (username, age) VALUES
('user1', 30),
('user2', 40);`)
defer testYugabyteDBSource.TestContainer.ExecuteSqls(`DROP SCHEMA test_schema CASCADE;`)

// Test GetTableToUniqueKeyColumnsMap
tableList := []sqlname.NameTuple{
{CurrentName: objectName},
uniqueTablesList := []sqlname.NameTuple{
{CurrentName: sqlname.NewObjectName("postgresql", "test_schema", "test_schema", "unique_table")},
{CurrentName: sqlname.NewObjectName("postgresql", "test_schema", "test_schema", "another_unique_table")},
}
uniqueKeys, err := testYugabyteDBSource.DB().GetTableToUniqueKeyColumnsMap(tableList)

actualUniqKeys, err := testYugabyteDBSource.DB().GetTableToUniqueKeyColumnsMap(uniqueTablesList)
if err != nil {
t.Fatalf("Error retrieving unique keys: %v", err)
}

expectedKeys := map[string][]string{
"unique_table": {"email", "phone", "address"},
expectedUniqKeys := map[string][]string{
"test_schema.unique_table": {"email", "phone", "address"},
"test_schema.another_unique_table": {"username", "age"},
}

// Compare the maps by iterating over each table and asserting the columns list
for table, expectedColumns := range expectedKeys {
actualColumns, exists := uniqueKeys[table]
for table, expectedColumns := range expectedUniqKeys {
actualColumns, exists := actualUniqKeys[table]
if !exists {
t.Errorf("Expected table %s not found in uniqueKeys", table)
}
Expand All @@ -71,9 +108,29 @@ func TestYugabyteGetTableToUniqueKeyColumnsMap(t *testing.T) {
}

func TestYugabyteGetNonPKTables(t *testing.T) {
testYugabyteDBSource.TestContainer.ExecuteSqls(
`CREATE SCHEMA test_schema;`,
`CREATE TABLE test_schema.table1 (
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);`,
`CREATE TABLE test_schema.table2 (
id SERIAL PRIMARY KEY,
email VARCHAR(100)
);`,
`CREATE TABLE test_schema.non_pk1(
id INT,
name VARCHAR(255)
);`,
`CREATE TABLE test_schema.non_pk2(
id INT,
name VARCHAR(255)
);`)
defer testYugabyteDBSource.TestContainer.ExecuteSqls(`DROP SCHEMA test_schema CASCADE;`)

actualTables, err := testYugabyteDBSource.DB().GetNonPKTables()
assert.NilError(t, err, "Expected nil but non nil error: %v", err)

expectedTables := []string{`public."non_pk2"`, `public."non_pk1"`} // func returns table.Qualified.Quoted
expectedTables := []string{`test_schema."non_pk2"`, `test_schema."non_pk1"`} // func returns table.Qualified.Quoted
testutils.AssertEqualStringSlices(t, expectedTables, actualTables)
}
8 changes: 4 additions & 4 deletions yb-voyager/src/tgtdb/conn_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBasic(t *testing.T) {
connParams := &ConnectionParams{
NumConnections: size,
NumMaxConnections: size,
ConnUriList: []string{testYugabyteDBTarget.Container.GetConnectionString()},
ConnUriList: []string{testYugabyteDBTarget.GetConnectionString()},
SessionInitScript: []string{},
}
pool := NewConnectionPool(connParams)
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestIncreaseConnectionsUptoMax(t *testing.T) {
connParams := &ConnectionParams{
NumConnections: size,
NumMaxConnections: maxSize,
ConnUriList: []string{testYugabyteDBTarget.Container.GetConnectionString()},
ConnUriList: []string{testYugabyteDBTarget.GetConnectionString()},
SessionInitScript: []string{},
}
pool := NewConnectionPool(connParams)
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestDecreaseConnectionsUptoMin(t *testing.T) {
connParams := &ConnectionParams{
NumConnections: size,
NumMaxConnections: maxSize,
ConnUriList: []string{testYugabyteDBTarget.Container.GetConnectionString()},
ConnUriList: []string{testYugabyteDBTarget.GetConnectionString()},
SessionInitScript: []string{},
}
pool := NewConnectionPool(connParams)
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestUpdateConnectionsRandom(t *testing.T) {
connParams := &ConnectionParams{
NumConnections: size,
NumMaxConnections: maxSize,
ConnUriList: []string{testYugabyteDBTarget.Container.GetConnectionString()},
ConnUriList: []string{testYugabyteDBTarget.GetConnectionString()},
SessionInitScript: []string{},
}
pool := NewConnectionPool(connParams)
Expand Down
7 changes: 3 additions & 4 deletions yb-voyager/src/tgtdb/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

type TestDB struct {
Container testcontainers.TestContainer
testcontainers.TestContainer
TargetDB
}

Expand All @@ -42,7 +42,6 @@ func TestMain(m *testing.M) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// setting source db type, version and defaults
postgresContainer := testcontainers.NewTestContainer("postgresql", nil)
err := postgresContainer.Start(ctx)
if err != nil {
Expand All @@ -53,7 +52,7 @@ func TestMain(m *testing.M) {
utils.ErrExit("%v", err)
}
testPostgresTarget = &TestDB{
Container: postgresContainer,
TestContainer: postgresContainer,
TargetDB: NewTargetDB(&TargetConf{
TargetDBType: "postgresql",
DBVersion: postgresContainer.GetConfig().DBVersion,
Expand Down Expand Up @@ -109,7 +108,7 @@ func TestMain(m *testing.M) {
utils.ErrExit("%v", err)
}
testYugabyteDBTarget = &TestDB{
Container: yugabytedbContainer,
TestContainer: yugabytedbContainer,
TargetDB: NewTargetDB(&TargetConf{
TargetDBType: "yugabytedb",
DBVersion: yugabytedbContainer.GetConfig().DBVersion,
Expand Down
Loading

0 comments on commit 5e6292c

Please sign in to comment.