-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodels_test.go
33 lines (27 loc) · 945 Bytes
/
models_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
package models_test
import (
"context"
"fmt"
"testing"
"github.com/GitDataAI/jiaozifs/config"
"github.com/GitDataAI/jiaozifs/models"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/phayes/freeport"
"github.com/stretchr/testify/require"
"go.uber.org/fx/fxtest"
)
var testConnTmpl = "postgres://postgres:postgres@localhost:%d/jiaozifs?sslmode=disable"
func TestSetupDatabase(t *testing.T) {
ctx := context.Background()
port, err := freeport.GetFreePort()
require.NoError(t, err)
postgres := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().Port(uint32(port)).Database("jiaozifs"))
err = postgres.Start()
require.NoError(t, err)
defer postgres.Stop() //nolint
lc := fxtest.NewLifecycle(t)
db, err := models.SetupDatabase(ctx, lc, &config.DatabaseConfig{Connection: fmt.Sprintf(testConnTmpl, port)})
require.NoError(t, err)
lc.RequireStop()
require.NoError(t, db.PingContext(ctx))
}