-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
100 lines (84 loc) · 2.56 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package helpers
import (
"context"
"crypto/ecdsa"
"fmt"
"testing"
"time"
"github.com/DIMO-Network/shared/db"
"github.com/docker/go-connections/nat"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pressly/goose/v3"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
var DBSettings = db.Settings{
Name: "identity_api",
Host: "localhost",
User: "dimo",
Password: "dimo",
// MaxOpenConnections: 2,
// MaxIdleConnections: 2,
}
// StartContainerDatabase starts postgres container with default test settings, and migrates the db. Caller must terminate container.
func StartContainerDatabase(ctx context.Context, t *testing.T, migrationsDirRelPath string) (dbs db.Store, container *postgres.PostgresContainer) {
settings := DBSettings // Copy.
pgPort := "5432/tcp"
var err error
container, err = postgres.RunContainer(
ctx,
testcontainers.WithImage("docker.io/postgres:14.10-alpine"),
postgres.WithDatabase("identity_api"),
postgres.WithUsername("dimo"),
postgres.WithPassword("dimo"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(5*time.Second),
),
)
require.NoError(t, err)
defer func() {
if err != nil {
container.Terminate(ctx) //nolint:errcheck
t.Fatalf("Failed to set up Postgres container: %v", err)
}
}()
mappedPort, err := container.MappedPort(ctx, nat.Port(pgPort))
if err != nil {
err = fmt.Errorf("couldn't get Postgres port: %w", err)
return
}
settings.Port = mappedPort.Port()
dbs = db.NewDbConnectionForTest(ctx, &settings, false)
for !dbs.IsReady() {
time.Sleep(500 * time.Millisecond)
}
_, err = dbs.DBS().Writer.Exec("CREATE SCHEMA IF NOT EXISTS identity_api")
if err != nil {
err = fmt.Errorf("error creating schema: %w", err)
return
}
goose.SetTableName("identity_api.migrations")
err = goose.RunContext(ctx, "up", dbs.DBS().Writer.DB, migrationsDirRelPath)
if err != nil {
err = fmt.Errorf("error running migrations: %w", err)
return
}
err = container.Snapshot(ctx)
if err != nil {
err = fmt.Errorf("error taking blank snapshot: %w", err)
}
return
}
func GenerateWallet() (*ecdsa.PrivateKey, *common.Address, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return nil, nil, err
}
userAddr := crypto.PubkeyToAddress(privateKey.PublicKey)
return privateKey, &userAddr, nil
}