-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils.go
98 lines (88 loc) · 2.31 KB
/
testutils.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
package testutils
import (
"context"
"errors"
"fmt"
"os"
"google.golang.org/api/option"
"google.golang.org/grpc"
"github.com/castaneai/spadmin"
"github.com/castaneai/spankeys"
"cloud.google.com/go/spanner"
)
func SelectOne(ctx context.Context, sql string, c *spanner.Client, ptr interface{}) error {
stmt := spanner.NewStatement(sql)
iter := c.Single().Query(ctx, stmt)
defer iter.Stop()
r, err := iter.Next()
if err != nil {
return err
}
return r.Column(0, ptr)
}
func CountsRow(ctx context.Context, sql string, c *spanner.Client) (int64, error) {
stmt := spanner.NewStatement(sql)
iter := c.Single().Query(ctx, stmt)
defer iter.Stop()
r, err := iter.Next()
if err != nil {
return 0, err
}
var cnt int64
if err := r.Column(0, &cnt); err != nil {
return 0, err
}
return cnt, nil
}
func PrepareDatabase(ctx context.Context, ddls []string, opts ...option.ClientOption) error {
dsn, err := makeDSNFromEnv()
if err != nil {
return err
}
if emulatorAddr := os.Getenv("SPANNER_EMULATOR_HOST"); emulatorAddr != "" {
conn, err := grpc.DialContext(ctx, emulatorAddr, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return err
}
opts = append(opts, option.WithGRPCConn(conn))
}
admin, err := spadmin.NewClient(ctx, dsn.Parent(), opts...)
if err != nil {
return err
}
exists, err := admin.DatabaseExists(ctx, dsn.DatabaseID())
if err != nil {
return err
}
if exists {
if err := admin.DropDatabase(ctx, dsn.DatabaseID()); err != nil {
return err
}
}
if err := admin.CreateDatabase(ctx, dsn.DatabaseID(), ddls); err != nil {
return err
}
return nil
}
func makeDSNFromEnv() (spankeys.DSN, error) {
projectID := os.Getenv("SPANNER_PROJECT_ID")
if projectID == "" {
return "", errors.New("env: SPANNER_PROJECT_ID not set")
}
instance := os.Getenv("SPANNER_INSTANCE_ID")
if instance == "" {
return "", errors.New("env: SPANNER_INSTANCE_ID not set")
}
database := os.Getenv("SPANNER_DATABASE_ID")
if database == "" {
return "", errors.New("env: SPANNER_DATABASE_ID not set")
}
return spankeys.DSN(fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectID, instance, database)), nil
}
func NewSpannerClient(ctx context.Context) (*spanner.Client, error) {
dsn, err := makeDSNFromEnv()
if err != nil {
return nil, err
}
return spanner.NewClient(ctx, string(dsn))
}