-
Notifications
You must be signed in to change notification settings - Fork 0
/
thing_test.go
48 lines (39 loc) · 1.12 KB
/
thing_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
package main
import (
"context"
"math"
"os"
"testing"
"time"
"github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
)
func TestReadThingByID(t *testing.T) {
connString := os.Getenv("POSTGRES_URL")
require.NotEmpty(t, connString, "POSTGRES_URL is required")
ctx := context.Background()
conn, err := pgx.Connect(ctx, connString)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, conn.Close(ctx)) })
db := &db{conn: conn}
schemaMigrations, err := os.ReadFile("./testdata/initial_schema.sql")
require.NoError(t, err)
m := squirrel.Expr(string(schemaMigrations))
_, err = squirrel.ExecContextWith(ctx, db, m)
require.NoError(t, err)
th := thing{
ID: id(uuid.New().String()),
Name: "Foo",
Labels: []string{"cat", "dog"},
N: 7,
X: 1.283,
CreatedAt: time.Now().Truncate(time.Millisecond),
Stuff: map[string]any{"a": math.Pi, "b": "yes"},
}
require.NoError(t, db.insertThing(ctx, th))
gotThing, err := db.readThingByID(ctx, th.ID)
require.NoError(t, err)
require.Equal(t, th, gotThing)
}