Skip to content

Commit 2c7873c

Browse files
committed
chore(tests): faster and more stable tests by moving db setup to TestMain
1 parent bbd199d commit 2c7873c

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

tests/client_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ package tests
33
import (
44
"context"
55
"fmt"
6-
"github.com/oolio-group/dynago"
76
"log"
7+
"os"
88
"testing"
99

10+
"github.com/oolio-group/dynago"
11+
"github.com/ory/dockertest/v3"
12+
1013
"github.com/aws/aws-sdk-go-v2/aws"
1114
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
1215
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
13-
"github.com/ory/dockertest/v3"
1416
)
1517

16-
func startLocalDatabase(t *testing.T) (addr string, purge func()) {
17-
t.Helper()
18+
func startLocalDatabase() (addr string, purge func()) {
1819
pool, err := dockertest.NewPool("")
1920
if err != nil {
20-
t.Fatalf("could not connect to docker: %s", err)
21+
log.Fatalln("could not connect to docker", err)
2122
}
2223

2324
resource, err := pool.Run("amazon/dynamodb-local", "latest", []string{})
@@ -27,11 +28,23 @@ func startLocalDatabase(t *testing.T) (addr string, purge func()) {
2728
addr = fmt.Sprintf("http://localhost:%s", resource.GetPort("8000/tcp"))
2829
return addr, func() {
2930
if err := pool.Purge(resource); err != nil {
30-
t.Fatalf("could not purge container: %s", err)
31+
log.Println(err)
3132
}
3233
}
3334
}
3435

36+
var dynamoEndpoint string
37+
38+
func TestMain(m *testing.M) {
39+
address, cleanup := startLocalDatabase()
40+
dynamoEndpoint = address
41+
42+
code := m.Run()
43+
// os.Exit does not respect defer
44+
cleanup()
45+
os.Exit(code)
46+
}
47+
3548
func createTestTable(t *dynago.Client) error {
3649
_, err := t.GetDynamoDBClient().CreateTable(context.TODO(), &dynamodb.CreateTableInput{
3750
AttributeDefinitions: []types.AttributeDefinition{{
@@ -73,13 +86,10 @@ func TestNewClient(t *testing.T) {
7386
}
7487

7588
func TestNewClientLocalEndpoint(t *testing.T) {
76-
endpoint, purge := startLocalDatabase(t)
77-
defer purge()
78-
7989
table, err := dynago.NewClient(context.TODO(), dynago.ClientOptions{
8090
TableName: "test",
8191
Endpoint: &dynago.EndpointResolver{
82-
EndpointURL: endpoint,
92+
EndpointURL: dynamoEndpoint,
8393
AccessKeyID: "dummy",
8494
SecretAccessKey: "dummy",
8595
},

tests/query_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ type User struct {
4646
}
4747

4848
func TestQuery(t *testing.T) {
49-
endoint, purge := startLocalDatabase(t)
50-
defer purge()
51-
52-
table := prepareTable(t, endoint, "query_test")
49+
table := prepareTable(t, dynamoEndpoint, "query_test")
5350
testCases := []struct {
5451
title string
5552
condition string
@@ -245,10 +242,7 @@ func genRandomBytes(t *testing.T, size int) (blk []byte) {
245242
}
246243

247244
func TestQueryPagination(t *testing.T) {
248-
endoint, purge := startLocalDatabase(t)
249-
defer purge()
250-
251-
table := prepareTable(t, endoint, "query_pagination_test")
245+
table := prepareTable(t, dynamoEndpoint, "query_pagination_test")
252246
// write 3MB worth of sample user records to the database for testing
253247
// dynamodb paginates query result by pages of 1MB recors by default.
254248
const batchSize = 100

tests/transact_items_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ type Terminal struct {
1717
}
1818

1919
func TestTransactItems(t *testing.T) {
20-
endoint, purge := startLocalDatabase(t)
21-
defer purge()
22-
23-
table := prepareTable(t, endoint, "transcact_item_test")
20+
table := prepareTable(t, dynamoEndpoint, "transcact_item_test")
2421

2522
testCases := []struct {
2623
title string

0 commit comments

Comments
 (0)