-
Notifications
You must be signed in to change notification settings - Fork 5
/
admin_test.go
91 lines (84 loc) · 2.41 KB
/
admin_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
package spool
import (
"context"
"testing"
"cloud.google.com/go/spanner"
"github.com/cloudspannerecosystem/spool/model"
)
func connect(ctx context.Context, t *testing.T, conf *Config) (*spanner.Client, func()) {
t.Helper()
client, err := spanner.NewClient(ctx, conf.Database(), conf.ClientOptions()...)
if err != nil {
t.Fatal(err)
}
return client, func() {
if err := clean(ctx, client, conf,
func(ctx context.Context, txn *spanner.ReadWriteTransaction) ([]*model.SpoolDatabase, error) {
return model.FindAllSpoolDatabases(ctx, txn)
},
); err != nil {
t.Fatal(err)
}
}
}
func TestListAll(t *testing.T) {
ctx := context.Background()
client, truncate := connect(ctx, t, testConf.Config())
defer truncate()
sdb1 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-1",
Checksum: "checksum-1",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
sdb2 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-2",
Checksum: "checksum-2",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
if _, err := client.Apply(ctx, []*spanner.Mutation{sdb1.Insert(ctx), sdb2.Insert(ctx)}); err != nil {
t.Fatalf("failed to setup fixture: %s", err)
}
sdbs, err := ListAll(ctx, testConf.Config())
if err != nil {
t.Fatal(err)
}
if len(sdbs) != 2 {
t.Errorf("failed to find all: found %d", len(sdbs))
}
}
func TestCleanAll(t *testing.T) {
ctx := context.Background()
client, truncate := connect(ctx, t, testConf.Config())
defer truncate()
sdb1 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-1",
Checksum: "checksum-1",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
sdb2 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-2",
Checksum: "checksum-2",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
if _, err := client.Apply(ctx, []*spanner.Mutation{sdb1.Insert(ctx), sdb2.Insert(ctx)}); err != nil {
t.Fatalf("failed to setup fixture: %s", err)
}
if err := CleanAll(ctx, testConf.Config()); err != nil {
t.Fatal(err)
}
sdbs, err := model.FindAllSpoolDatabases(ctx, client.Single())
if err != nil {
t.Error(err)
}
if len(sdbs) != 0 {
t.Error("failed to clean all")
}
}