Skip to content

Commit 99218f9

Browse files
committed
Move backend tests to table driven tests instead of suite
1 parent 61ef88c commit 99218f9

File tree

6 files changed

+243
-239
lines changed

6 files changed

+243
-239
lines changed

backend/mysql/mysql_test.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,33 @@ func Test_MysqlBackend(t *testing.T) {
2525

2626
dbName := "test_" + strings.Replace(uuid.NewString(), "-", "", -1)
2727

28-
test.TestBackend(t, test.Tester{
29-
New: func() backend.Backend {
30-
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
31-
if err != nil {
32-
panic(err)
33-
}
34-
35-
if _, err := db.Exec("CREATE DATABASE " + dbName); err != nil {
36-
panic(errors.Wrap(err, "could not create database"))
37-
}
38-
39-
if err := db.Close(); err != nil {
40-
panic(err)
41-
}
42-
43-
return NewMysqlBackend("localhost", 3306, testUser, testPassword, dbName, backend.WithStickyTimeout(0))
44-
},
45-
46-
Teardown: func() {
47-
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
48-
if err != nil {
49-
panic(err)
50-
}
51-
52-
if _, err := db.Exec("DROP DATABASE IF EXISTS " + dbName); err != nil {
53-
panic(errors.Wrap(err, "could not drop database"))
54-
}
55-
56-
if err := db.Close(); err != nil {
57-
panic(err)
58-
}
59-
},
28+
test.BackendTest(t, func() backend.Backend {
29+
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
if _, err := db.Exec("CREATE DATABASE " + dbName); err != nil {
35+
panic(errors.Wrap(err, "could not create database"))
36+
}
37+
38+
if err := db.Close(); err != nil {
39+
panic(err)
40+
}
41+
42+
return NewMysqlBackend("localhost", 3306, testUser, testPassword, dbName, backend.WithStickyTimeout(0))
43+
}, func(b backend.Backend) {
44+
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
if _, err := db.Exec("DROP DATABASE IF EXISTS " + dbName); err != nil {
50+
panic(errors.Wrap(err, "could not drop database"))
51+
}
52+
53+
if err := db.Close(); err != nil {
54+
panic(err)
55+
}
6056
})
6157
}

backend/redis/redis_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
package redis
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

78
"github.com/cschleiden/go-workflows/backend"
89
"github.com/cschleiden/go-workflows/backend/test"
10+
"github.com/go-redis/redis/v8"
911
"github.com/stretchr/testify/require"
1012
)
1113

1214
func Test_RedisBackend(t *testing.T) {
13-
test.TestBackend(t, test.Tester{
14-
New: func() backend.Backend {
15-
// Disable sticky workflow behavior for the test execution
16-
b, err := NewRedisBackend("localhost:6379", "", "RedisPassw0rd", 0,
17-
WithBlockTimeout(time.Millisecond*2))
18-
require.NoError(t, err)
19-
return b
20-
},
21-
})
15+
test.BackendTest(t, func() backend.Backend {
16+
address := "localhost:6379"
17+
user := ""
18+
password := "RedisPassw0rd"
19+
20+
// Flush database
21+
client := redis.NewUniversalClient(&redis.UniversalOptions{
22+
Addrs: []string{address},
23+
Username: user,
24+
Password: password,
25+
DB: 0,
26+
})
27+
28+
if err := client.FlushDB(context.Background()).Err(); err != nil {
29+
panic(err)
30+
}
31+
32+
// Disable sticky workflow behavior for the test execution
33+
b, err := NewRedisBackend(address, user, password, 0, WithBlockTimeout(time.Millisecond*2))
34+
require.NoError(t, err)
35+
36+
return b
37+
}, nil)
2238
}

backend/sqlite/sqlite_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import (
88
)
99

1010
func Test_SqliteBackend(t *testing.T) {
11-
test.TestBackend(t, test.Tester{
12-
New: func() backend.Backend {
13-
// Disable sticky workflow behavior for the test execution
14-
return NewInMemoryBackend(backend.WithStickyTimeout(0))
15-
},
16-
})
11+
test.BackendTest(t, func() backend.Backend {
12+
// Disable sticky workflow behavior for the test execution
13+
return NewInMemoryBackend(backend.WithStickyTimeout(0))
14+
}, nil)
1715
}

0 commit comments

Comments
 (0)