-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyo_test.go
260 lines (235 loc) · 6.06 KB
/
yo_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package yowrap
import (
"context"
"errors"
"os"
"sort"
"testing"
"cloud.google.com/go/spanner"
database "cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"cloud.google.com/go/spanner/spannertest"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/jferrl/yowrap/internal/user"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestModel_Insert(t *testing.T) {
ctx := context.Background()
type fields struct {
model *user.User
}
tests := []struct {
name string
fields fields
}{
{
name: "yowrap model is able to expose the Insert method of the embedded model",
fields: fields{model: &user.User{
ID: uuid.NewString(),
Name: "John Doe",
Email: "[email protected]",
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewModel(tt.fields.model)
_, ok := m.Model().(*user.User)
if !ok {
t.Fatal("unable to type assert to *user.User")
}
if got := m.Insert(ctx) != nil; !got {
t.Errorf("Model.Insert() = %v, want non-nil", got)
}
})
}
}
func TestModel_Apply(t *testing.T) {
ctx := context.Background()
model := &user.User{
ID: uuid.NewString(),
Name: "John Doe",
Email: "[email protected]",
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
type fields struct {
model *user.User
}
tests := []struct {
name string
fields fields
mutType Mutation
before HookFunc[*user.User]
after HookFunc[*user.User]
wantErr bool
want []*user.User
}{
{
name: "unknown mutation returns an error",
fields: fields{model: model},
wantErr: true,
},
{
name: "insert a new user into the database",
fields: fields{model: model},
mutType: Insert,
want: []*user.User{
{
Name: "John Doe",
Email: "[email protected]",
},
},
},
{
name: "insert or update user into the database",
fields: fields{model: model},
mutType: InsertOrUpdate,
want: []*user.User{
{
Name: "John Doe",
Email: "[email protected]",
},
},
},
{
name: "update a non-existent user in the database",
fields: fields{model: model},
mutType: Update,
wantErr: true,
},
{
name: "before insert hook returns an error",
fields: fields{model: model},
mutType: Insert,
before: func(_ context.Context, m *Model[*user.User], _ *spanner.ReadWriteTransaction) error {
return errors.New("before insert hook error")
},
wantErr: true,
},
{
name: "after insert hook returns an error",
fields: fields{model: model},
mutType: Insert,
after: func(_ context.Context, m *Model[*user.User], _ *spanner.ReadWriteTransaction) error {
return errors.New("after insert hook error")
},
wantErr: true,
},
{
name: "insert a new user into the database with hooks",
fields: fields{model: model},
mutType: Insert,
before: func(_ context.Context, m *Model[*user.User], _ *spanner.ReadWriteTransaction) error {
// type assertion to access the embedded model
u, ok := m.Yoable.(*user.User)
if !ok {
return errors.New("unable to type assert to *user.User")
}
u.Name = "Jane Doe"
return nil
},
after: func(ctx context.Context, m *Model[*user.User], rwt *spanner.ReadWriteTransaction) error {
// type assertion to access the embedded model
u, ok := m.Yoable.(*user.User)
if !ok {
return errors.New("unable to type assert to *user.User")
}
u.Email = "[email protected]"
return rwt.BufferWrite([]*spanner.Mutation{
m.Update(ctx),
})
},
want: []*user.User{
{
Name: "Jane Doe",
Email: "[email protected]",
},
},
},
{
name: "delete a user from the database when is created with before delete hook",
fields: fields{model: model},
mutType: Delete,
before: func(cxt context.Context, m *Model[*user.User], txn *spanner.ReadWriteTransaction) error {
return txn.BufferWrite([]*spanner.Mutation{
m.Insert(cxt),
})
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spannerClient, cleanup := setupSpanner(ctx, t)
defer cleanup()
m := NewModel(tt.fields.model,
WithSpannerClientOption[*user.User](spannerClient),
)
before, after := tt.mutType.Hooks()
if tt.before != nil {
m.On(before, tt.before)
}
if tt.after != nil {
m.On(after, tt.after)
}
if _, err := m.Apply(ctx, tt.mutType); (err != nil) != tt.wantErr {
t.Errorf("Model.ApplyInsert() error = %v, wantErr %v", err, tt.wantErr)
}
got, err := user.ReadUser(ctx, spannerClient.Single(), spanner.AllKeys())
if err != nil {
t.Fatal(err)
}
sort.Slice(got, func(i, j int) bool {
return got[i].Name < got[j].Name
})
if diff := cmp.Diff(tt.want, got,
cmpopts.IgnoreFields(user.User{}, "ID", "CreatedAt", "UpdatedAt")); diff != "" {
t.Errorf("ApplyInsert() mismatch (-want +got):\n%s", diff)
}
})
}
}
func setupSpanner(ctx context.Context, t *testing.T) (*spanner.Client, func()) {
server, err := spannertest.NewServer(":0")
if err != nil {
t.Fatal(err)
}
conn, err := grpc.Dial(server.Addr, grpc.WithTransportCredentials(
insecure.NewCredentials(),
))
if err != nil {
t.Fatal(err)
}
spannerDatabaseClient, err := database.NewDatabaseAdminClient(ctx, option.WithGRPCConn(conn))
if err != nil {
t.Fatal(err)
}
ddl, err := os.ReadFile("internal/sql/ddl.sql")
if err != nil {
t.Fatal(err)
}
op, err := spannerDatabaseClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{
Database: "my-db",
Statements: []string{string(ddl)},
})
if err != nil {
t.Fatal(err)
}
if err := op.Wait(ctx); err != nil {
t.Fatal(err)
}
db := "projects/my-project/instances/my-instance/databases/my-db"
spannerClient, err := spanner.NewClient(ctx, db, option.WithGRPCConn(conn))
if err != nil {
t.Fatal(err)
}
return spannerClient, func() {
spannerClient.Close()
spannerDatabaseClient.Close()
server.Close()
}
}