@@ -3,21 +3,22 @@ package tests
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "github.com/oolio-group/dynago"
7
6
"log"
7
+ "os"
8
8
"testing"
9
9
10
+ "github.com/oolio-group/dynago"
11
+ "github.com/ory/dockertest/v3"
12
+
10
13
"github.com/aws/aws-sdk-go-v2/aws"
11
14
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
12
15
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
13
- "github.com/ory/dockertest/v3"
14
16
)
15
17
16
- func startLocalDatabase (t * testing.T ) (addr string , purge func ()) {
17
- t .Helper ()
18
+ func startLocalDatabase () (addr string , purge func ()) {
18
19
pool , err := dockertest .NewPool ("" )
19
20
if err != nil {
20
- t . Fatalf ("could not connect to docker: %s " , err )
21
+ log . Fatalln ("could not connect to docker" , err )
21
22
}
22
23
23
24
resource , err := pool .Run ("amazon/dynamodb-local" , "latest" , []string {})
@@ -27,11 +28,23 @@ func startLocalDatabase(t *testing.T) (addr string, purge func()) {
27
28
addr = fmt .Sprintf ("http://localhost:%s" , resource .GetPort ("8000/tcp" ))
28
29
return addr , func () {
29
30
if err := pool .Purge (resource ); err != nil {
30
- t . Fatalf ( "could not purge container: %s" , err )
31
+ log . Println ( err )
31
32
}
32
33
}
33
34
}
34
35
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
+
35
48
func createTestTable (t * dynago.Client ) error {
36
49
_ , err := t .GetDynamoDBClient ().CreateTable (context .TODO (), & dynamodb.CreateTableInput {
37
50
AttributeDefinitions : []types.AttributeDefinition {{
@@ -73,13 +86,10 @@ func TestNewClient(t *testing.T) {
73
86
}
74
87
75
88
func TestNewClientLocalEndpoint (t * testing.T ) {
76
- endpoint , purge := startLocalDatabase (t )
77
- defer purge ()
78
-
79
89
table , err := dynago .NewClient (context .TODO (), dynago.ClientOptions {
80
90
TableName : "test" ,
81
91
Endpoint : & dynago.EndpointResolver {
82
- EndpointURL : endpoint ,
92
+ EndpointURL : dynamoEndpoint ,
83
93
AccessKeyID : "dummy" ,
84
94
SecretAccessKey : "dummy" ,
85
95
},
0 commit comments