-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathspechelper_sharedResource_test.go
47 lines (41 loc) · 1.07 KB
/
spechelper_sharedResource_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// package spechelper
package examples_test
import (
"context"
"os"
"sync"
"testing"
"go.llib.dev/testcase"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal/example/mydomain"
"go.llib.dev/testcase/internal/example/someextres"
)
var (
sharedGlobalStorageInstanceInit sync.Once
sharedGlobalStorageInstance mydomain.Storage // role interface type
)
func getSharedGlobalStorageInstance(tb testing.TB) mydomain.Storage {
sharedGlobalStorageInstanceInit.Do(func() {
storage, err := someextres.NewStorage(os.Getenv(`TEST_DATABASE_URL`))
assert.Must(tb).Nil(err)
sharedGlobalStorageInstance = storage
})
return sharedGlobalStorageInstance
}
var Context = testcase.Var[context.Context]{
ID: `context`,
Init: func(t *testcase.T) context.Context {
return context.Background()
},
}
var Storage = testcase.Var[mydomain.Storage]{
ID: `Storage`,
Init: func(t *testcase.T) mydomain.Storage {
s := getSharedGlobalStorageInstance(t)
tx, err := s.BeginTx(Context.Get(t))
t.Must.Nil(err)
Context.Set(t, tx)
t.Defer(s.RollbackTx, tx) // teardown
return s
},
}