Skip to content

Commit be1aa5f

Browse files
committed
Rework test suite to load test data before starting backend
1 parent 5d55cc1 commit be1aa5f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

Diff for: backends/rapidpro/backend_test.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/aws/aws-sdk-go-v2/service/s3"
2222
"github.com/buger/jsonparser"
2323
"github.com/gomodule/redigo/redis"
24+
"github.com/jmoiron/sqlx"
2425
"github.com/lib/pq"
2526
"github.com/nyaruka/courier"
2627
"github.com/nyaruka/courier/queue"
@@ -61,27 +62,31 @@ func testConfig() *courier.Config {
6162
return config
6263
}
6364

65+
func (ts *BackendTestSuite) loadSQL(path string) {
66+
db, err := sqlx.Open("postgres", ts.b.config.DB)
67+
noError(err)
68+
69+
sql, err := os.ReadFile(path)
70+
noError(err)
71+
db.MustExec(string(sql))
72+
}
73+
6474
func (ts *BackendTestSuite) SetupSuite() {
6575
ctx := context.Background()
76+
cfg := testConfig()
6677

6778
// turn off logging
6879
log.SetOutput(io.Discard)
6980

70-
b, err := courier.NewBackend(testConfig())
81+
b, err := courier.NewBackend(cfg)
7182
noError(err)
72-
7383
ts.b = b.(*backend)
74-
must(ts.b.Start())
7584

76-
// read our schema sql
77-
sqlSchema, err := os.ReadFile("schema.sql")
78-
noError(err)
79-
ts.b.db.MustExec(string(sqlSchema))
85+
// load our test schema and data
86+
ts.loadSQL("schema.sql")
87+
ts.loadSQL("testdata.sql")
8088

81-
// read our testdata sql
82-
sql, err := os.ReadFile("testdata.sql")
83-
noError(err)
84-
ts.b.db.MustExec(string(sql))
89+
must(ts.b.Start())
8590

8691
ts.b.s3.Client.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: aws.String("test-attachments")})
8792
ts.b.s3.Client.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: aws.String("test-logs")})

0 commit comments

Comments
 (0)