Refactor TestQuery() to decouple it from the DBAdapter

This commit is contained in:
Vinícius Garcia 2022-02-15 23:51:58 -03:00
parent 3e275804d0
commit 488f5ba71d

View File

@ -85,6 +85,21 @@ var supportedConfigs = []testConfig{
func TestQuery(t *testing.T) {
for _, config := range supportedConfigs {
QueryTest(t,
config,
func(t *testing.T) (DBAdapter, io.Closer) {
db, close := connectDB(t, config)
return db, close
},
)
}
}
func QueryTest(
t *testing.T,
config testConfig,
newDBAdapter func(t *testing.T) (DBAdapter, io.Closer),
) {
t.Run(config.driver, func(t *testing.T) {
variations := []struct {
desc string
@ -108,7 +123,7 @@ func TestQuery(t *testing.T) {
}
t.Run("should return 0 results correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -125,7 +140,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should return a user correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
_, err := db.ExecContext(context.TODO(), `INSERT INTO users (name, age, address) VALUES ('Bia', 0, '{"country":"BR"}')`)
@ -144,7 +159,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should return multiple users correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
_, err := db.ExecContext(context.TODO(), `INSERT INTO users (name, age, address) VALUES ('João Garcia', 0, '{"country":"US"}')`)
@ -171,7 +186,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should query joined tables correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
// This test only makes sense with no query prefix
@ -238,7 +253,7 @@ func TestQuery(t *testing.T) {
}
t.Run("should return 0 results correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -255,7 +270,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should return a user correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -275,7 +290,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should return multiple users correctly", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -308,7 +323,7 @@ func TestQuery(t *testing.T) {
return
}
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -367,7 +382,7 @@ func TestQuery(t *testing.T) {
}
t.Run("should report error if input is not a pointer to a slice of structs", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -394,7 +409,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should report error if the query is not valid", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -405,7 +420,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should report error if using nested struct and the query starts with SELECT", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -422,7 +437,7 @@ func TestQuery(t *testing.T) {
t.Run("should report error for nested structs with invalid types", func(t *testing.T) {
t.Run("int", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -444,7 +459,7 @@ func TestQuery(t *testing.T) {
})
t.Run("*struct", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -467,7 +482,7 @@ func TestQuery(t *testing.T) {
})
t.Run("should report error if nested struct is invalid", func(t *testing.T) {
db, closer := connectDB(t, config)
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
@ -484,8 +499,6 @@ func TestQuery(t *testing.T) {
})
})
})
}
}
func TestQueryOne(t *testing.T) {