Skip to content

Commit 2e229a2

Browse files
authored
test: remove private api usages in tests (#221)
refactor tests, so private interfaces are not used: * split batches in tests * remove batchSplitter interface, which is not used after refactor * replace quoteKeyword with simple substitution. it is not needed as table names in tests are not problematic We need this for a lib/tests module split
1 parent ef93f5c commit 2e229a2

12 files changed

+75
-120
lines changed

clickhouse.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ func (h *clickhouse) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction
8282
return tx.Commit()
8383
}
8484

85-
// splitter is a batchSplitter interface implementation. We need it for
86-
// ClickHouseDB because clickhouse doesn't support multi-statements.
87-
func (*clickhouse) splitter() []byte {
88-
return []byte(";\n")
89-
}
90-
9185
func (h *clickhouse) cleanTableQuery(tableName string) string {
9286
if h.cleanTableFn == nil {
9387
return h.baseHelper.cleanTableQuery(tableName)

clickhouse_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build clickhouse
2-
// +build clickhouse
32

43
package testfixtures
54

@@ -11,10 +10,7 @@ import (
1110
)
1211

1312
func TestClickhouse(t *testing.T) {
14-
testLoader(
15-
t,
16-
"clickhouse",
17-
os.Getenv("CLICKHOUSE_CONN_STRING"),
18-
"testdata/schema/clickhouse.sql",
19-
)
13+
db := openDB(t, "clickhouse", os.Getenv("CLICKHOUSE_CONN_STRING"))
14+
loadSchemaInBatchesBySplitter(t, db, "testdata/schema/clickhouse.sql", []byte(";\n"))
15+
testLoader(t, db, "clickhouse")
2016
}

cockroachdb_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build cockroachdb
2-
// +build cockroachdb
32

43
package testfixtures
54

@@ -13,11 +12,12 @@ import (
1312

1413
func TestCockroachDB(t *testing.T) {
1514
for _, dialect := range []string{"postgres", "pgx"} {
15+
db := openDB(t, dialect, os.Getenv("CRDB_CONN_STRING"))
16+
loadSchemaInOneQuery(t, db, "testdata/schema/cockroachdb.sql")
1617
testLoader(
1718
t,
19+
db,
1820
dialect,
19-
os.Getenv("CRDB_CONN_STRING"),
20-
"testdata/schema/cockroachdb.sql",
2121
DangerousSkipTestDatabaseCheck(),
2222
UseDropConstraint(),
2323
)

helper.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ type queryable interface {
3434
QueryRow(string, ...interface{}) *sql.Row
3535
}
3636

37-
// batchSplitter is an interface with method which returns byte slice for
38-
// splitting SQL batches. This need to split sql statements and run its
39-
// separately.
40-
//
41-
// For Microsoft SQL Server batch splitter is "GO". For details see
42-
// https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go
43-
type batchSplitter interface { //nolint
44-
splitter() []byte
45-
}
46-
4737
var (
4838
_ helper = &clickhouse{}
4939
_ helper = &spanner{}

mysql_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build mysql
1+
//go:build mysql
22

33
package testfixtures
44

@@ -10,10 +10,7 @@ import (
1010
)
1111

1212
func TestMySQL(t *testing.T) {
13-
testLoader(
14-
t,
15-
"mysql",
16-
os.Getenv("MYSQL_CONN_STRING"),
17-
"testdata/schema/mysql.sql",
18-
)
13+
db := openDB(t, "mysql", os.Getenv("MYSQL_CONN_STRING"))
14+
loadSchemaInOneQuery(t, db, "testdata/schema/mysql.sql")
15+
testLoader(t, db, "mysql")
1916
}

postgresql_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build postgresql
1+
//go:build postgresql
22

33
package testfixtures
44

@@ -11,36 +11,28 @@ import (
1111
)
1212

1313
func TestPostgreSQL(t *testing.T) {
14-
for _, dialect := range []string{"postgres", "pgx"} {
15-
testLoader(
16-
t,
17-
dialect,
18-
os.Getenv("PG_CONN_STRING"),
19-
"testdata/schema/postgresql.sql",
20-
)
21-
}
14+
testPostgreSQL(t)
2215
}
2316

2417
func TestPostgreSQLWithAlterConstraint(t *testing.T) {
25-
for _, dialect := range []string{"postgres", "pgx"} {
26-
testLoader(
27-
t,
28-
dialect,
29-
os.Getenv("PG_CONN_STRING"),
30-
"testdata/schema/postgresql.sql",
31-
UseAlterConstraint(),
32-
)
33-
}
18+
testPostgreSQL(t, UseAlterConstraint())
3419
}
3520

3621
func TestPostgreSQLWithDropConstraint(t *testing.T) {
22+
testPostgreSQL(t, UseDropConstraint())
23+
}
24+
25+
func testPostgreSQL(t *testing.T, additionalOptions ...func(*Loader) error) {
26+
t.Helper()
3727
for _, dialect := range []string{"postgres", "pgx"} {
28+
db := openDB(t, dialect, os.Getenv("PG_CONN_STRING"))
29+
loadSchemaInOneQuery(t, db, "testdata/schema/postgresql.sql")
3830
testLoader(
3931
t,
32+
db,
4033
dialect,
41-
os.Getenv("PG_CONN_STRING"),
42-
"testdata/schema/postgresql.sql",
43-
UseDropConstraint(),
34+
additionalOptions...,
4435
)
4536
}
37+
4638
}

spanner.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ func (h *spanner) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (
8484
return h.dropAndRecreateConstraints(db, loadFn)
8585
}
8686

87-
// splitter is a batchSplitter interface implementation. We need it for
88-
// spanner because spanner doesn't support multi-statements.
89-
func (*spanner) splitter() []byte {
90-
return []byte(";\n")
91-
}
92-
9387
func (h *spanner) cleanTableQuery(tableName string) string {
9488
if h.cleanTableFn == nil {
9589
return h.baseHelper.cleanTableQuery(tableName)

spanner_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ import (
1818
func TestSpanner(t *testing.T) {
1919
prepareSpannerDB(t)
2020

21-
testLoader(
22-
t,
23-
"spanner",
24-
os.Getenv("SPANNER_CONN_STRING"),
25-
"testdata/schema/spanner.sql",
26-
DangerousSkipTestDatabaseCheck(),
27-
)
21+
db := openDB(t, "spanner", os.Getenv("SPANNER_CONN_STRING"))
22+
loadSchemaInBatchesBySplitter(t, db, "testdata/schema/spanner.sql", []byte(";\n"))
23+
testLoader(t, db, "spanner", DangerousSkipTestDatabaseCheck())
2824
}
2925

3026
func prepareSpannerDB(t *testing.T) {

sqlite_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build sqlite
1+
//go:build sqlite
22

33
package testfixtures
44

@@ -10,10 +10,7 @@ import (
1010
)
1111

1212
func TestSQLite(t *testing.T) {
13-
testLoader(
14-
t,
15-
"sqlite3",
16-
os.Getenv("SQLITE_CONN_STRING"),
17-
"testdata/schema/sqlite.sql",
18-
)
13+
db := openDB(t, "sqlite3", os.Getenv("SQLITE_CONN_STRING"))
14+
loadSchemaInOneQuery(t, db, "testdata/schema/sqlite.sql")
15+
testLoader(t, db, "sqlite3")
1916
}

sqlserver.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,3 @@ func (h *sqlserver) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction)
143143

144144
return tx.Commit()
145145
}
146-
147-
// splitter is a batchSplitter interface implementation. We need it for
148-
// SQL Server because commands like a `CREATE SCHEMA...` and a `CREATE TABLE...`
149-
// could not be executed in the same batch.
150-
// See https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175502(v=sql.105)#rules-for-using-batches
151-
func (*sqlserver) splitter() []byte {
152-
return []byte("GO\n")
153-
}

0 commit comments

Comments
 (0)