Skip to content

Commit 3365b75

Browse files
committed
testing: simplify test setup
Switch to an options-struct pattern to de-noise the test. This was prompted by the need to add an option that will only be used by a few tests, in a future commit.
1 parent a8a607d commit 3365b75

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

testing/main_test.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,22 @@ var minRequiredVersionsByORMName = map[string]struct {
192192
// tenants.
193193
var minTenantVersion = version.MustParse("v20.2.0-alpha")
194194

195-
func testORM(
196-
t *testing.T, language, orm string, tableNames testTableNames, columnNames testColumnNames,
197-
) {
195+
type testInfo struct {
196+
language, orm string
197+
tableNames testTableNames // defaults to defaultTestTableNames
198+
columnNames testColumnNames // defaults to defaultTestColumnNames
199+
}
200+
201+
func testORM(t *testing.T, info testInfo) {
202+
if info.tableNames == (testTableNames{}) {
203+
info.tableNames = defaultTestTableNames
204+
}
205+
if info.columnNames.IsEmpty() {
206+
info.columnNames = defaultTestColumnNames
207+
}
198208
app := application{
199-
language: language,
200-
orm: orm,
209+
language: info.language,
210+
orm: info.orm,
201211
}
202212

203213
type testCase struct {
@@ -213,7 +223,7 @@ func testORM(
213223

214224
crdbVersion := getVersionFromDB(t, db)
215225
// Check that this ORM can be run with the given cockroach version.
216-
if info, ok := minRequiredVersionsByORMName[orm]; ok {
226+
if info, ok := minRequiredVersionsByORMName[info.orm]; ok {
217227
if !crdbVersion.AtLeast(info.v) {
218228
t.Skip(info.skipMsg)
219229
}
@@ -248,8 +258,8 @@ func testORM(
248258
td := testDriver{
249259
db: tc.db,
250260
dbName: app.dbName(),
251-
tableNames: tableNames,
252-
columnNames: columnNames,
261+
tableNames: info.tableNames,
262+
columnNames: info.columnNames,
253263
}
254264

255265
t.Run("FirstRun", func(t *testing.T) {
@@ -331,33 +341,38 @@ func testORM(
331341
}
332342

333343
func TestGORM(t *testing.T) {
334-
testORM(t, "go", "gorm", defaultTestTableNames, defaultTestColumnNames)
344+
testORM(t, testInfo{language: "go", orm: "gorm"})
335345
}
336346

337347
func TestGOPG(t *testing.T) {
338-
testORM(t, "go", "gopg", defaultTestTableNames, defaultTestColumnNames)
348+
testORM(t, testInfo{language: "go", orm: "gopg"})
339349
}
340350

341351
func TestHibernate(t *testing.T) {
342-
testORM(t, "java", "hibernate", defaultTestTableNames, defaultTestColumnNames)
352+
testORM(t, testInfo{language: "java", orm: "hibernate"})
343353
}
344354

345355
func TestSequelize(t *testing.T) {
346-
testORM(t, "node", "sequelize", defaultTestTableNames, defaultTestColumnNames)
356+
testORM(t, testInfo{language: "node", orm: "sequelize"})
347357
}
348358

349359
func TestSQLAlchemy(t *testing.T) {
350-
testORM(t, "python", "sqlalchemy", defaultTestTableNames, defaultTestColumnNames)
360+
testORM(t, testInfo{language: "python", orm: "sqlalchemy"})
351361
}
352362

353363
func TestDjango(t *testing.T) {
354-
testORM(t, "python", "django", djangoTestTableNames, djangoTestColumnNames)
364+
testORM(t, testInfo{
365+
language: "python",
366+
orm: "django",
367+
tableNames: djangoTestTableNames,
368+
columnNames: djangoTestColumnNames,
369+
})
355370
}
356371

357372
func TestActiveRecord(t *testing.T) {
358-
testORM(t, "ruby", "activerecord", defaultTestTableNames, defaultTestColumnNames)
373+
testORM(t, testInfo{language: "ruby", orm: "activerecord"})
359374
}
360375

361376
func TestActiveRecord4(t *testing.T) {
362-
testORM(t, "ruby", "ar4", defaultTestTableNames, defaultTestColumnNames)
377+
testORM(t, testInfo{language: "ruby", orm: "ar4"})
363378
}

testing/test_driver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type testColumnNames struct {
2525
ordersProductsColumns []string
2626
}
2727

28+
func (tcn testColumnNames) IsEmpty() bool {
29+
return len(tcn.customersColumns)+len(tcn.ordersColumns)+len(tcn.productsColumns)+len(tcn.ordersProductsColumns) == 0
30+
}
31+
2832
// These need to be variables so that their address can be taken.
2933
var (
3034
customerName1 = "Billy"

0 commit comments

Comments
 (0)