/
main.go
212 lines (185 loc) · 4.96 KB
/
main.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
package tbf
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/golang/protobuf/proto"
"go.mercari.io/datastore"
"go.mercari.io/datastore/testsuite"
netcontext "golang.org/x/net/context"
"google.golang.org/appengine"
)
// TestSuite contains all the test cases that this package provides.
var TestSuite = map[string]testsuite.Test{
"RealWorld_TBF": tbf,
}
func init() {
testsuite.MergeTestSuite(TestSuite)
}
type contextClient struct{}
type contextBatch struct{}
func timeNow() time.Time {
l, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
panic(err)
}
return time.Date(2017, 11, 8, 10, 11, 12, 13, l)
}
func tbf(ctx context.Context, t *testing.T, client datastore.Client) {
defer func() {
err := client.Close()
if err != nil {
t.Fatal(err)
}
}()
// それぞれのCircleに4枚の画像を持たせて、Circleを10件Getしたい。RPCは Query 1回 + BatchGet 1回 の合計2回がいい
// clientは複数作ってぶん回すとどっかでブロックされて処理が返ってこなくなるので使いまわす
ctx = context.WithValue(ctx, contextClient{}, client)
// batchは再利用可能
batch := client.Batch()
ctx = context.WithValue(ctx, contextBatch{}, batch)
rpcCount := 0
inMemcacheTestSuite := false
if testsuite.IsAEDatastoreClient(ctx) {
// checking rpc count when testing in ae.
ctx = appengine.WithAPICallFunc(ctx, func(ctx netcontext.Context, service, method string, in, out proto.Message) error {
t.Log(service, method)
if service == "datastore_v3" {
rpcCount++
}
if service == "memcache" {
// if memcache service called, this test in the TestAEDatastoreWithAEMemcacheTestSuite.
inMemcacheTestSuite = true
}
return appengine.APICall(ctx, service, method, in, out)
})
}
const circleLimit = 10
const imageLimit = 4
// Prepare entities
for i := 0; i < circleLimit; i++ {
// NOTE Don't use client.AllocateIDs for JSON format consistency
circleID := CircleID(1000000 + 10000*i)
circleKey := circleID.ToKey(client)
circle := &Circle{
ID: circleID,
Name: fmt.Sprintf("サークル #%d", i+1),
}
for j := 0; j < imageLimit; j++ {
// NOTE Don't use client.AllocateIDs for JSON format consistency
imageID := imageID(circleKey.ID() + 1000 + int64(10*j))
imageKey := imageID.ToKey(client)
image := &Image{
ID: imageID,
OwnerCircleID: circleID,
GCSPath: fmt.Sprintf("%d/%d.jpg", circleKey.ID(), imageKey.ID()),
}
batch.Put(imageKey, image, nil)
circle.ImageIDs = append(circle.ImageIDs, image.ID)
circle.Images = append(circle.Images, image)
}
batch.Put(circleKey, circle, nil)
}
err := batch.Exec(ctx)
if err != nil {
t.Fatal(err)
}
if testsuite.IsAEDatastoreClient(ctx) && !inMemcacheTestSuite {
if rpcCount != 1 {
t.Errorf("unexpected: %v", rpcCount)
}
}
// fetch entities
rpcCount = 0
q := client.NewQuery(kindCircle)
var circleList []*Circle
_, err = client.GetAll(ctx, q, &circleList)
if err != nil {
t.Fatal(err)
}
err = batch.Exec(ctx)
if err != nil {
t.Fatal(err)
}
if testsuite.IsAEDatastoreClient(ctx) && !inMemcacheTestSuite {
if rpcCount != 2 {
t.Errorf("unexpected: %v", rpcCount)
}
}
if v := len(circleList); v != circleLimit {
t.Errorf("unexpected: %v", v)
}
for _, circle := range circleList {
if v := len(circle.Images); v != imageLimit {
t.Errorf("unexpected: %v", v)
}
for _, image := range circle.Images {
if v := image.GCSPath; !strings.HasPrefix(v, fmt.Sprintf("%d/", circle.ID)) {
t.Errorf("unexpected: %v", v)
}
}
}
{ // obj <-> JSON
b, err := json.MarshalIndent(circleList, "", " ")
if err != nil {
t.Fatal(err)
}
const filePath = "../testsuite/realworld/tbf/realworld-tbf.json"
expected, err := ioutil.ReadFile(filePath)
if err != nil {
err = ioutil.WriteFile(filePath, b, 0644)
if err != nil {
t.Fatal(err)
}
expected = b
}
if !bytes.Equal(b, expected) {
t.Fatalf("unexpected json format. hint: rm %s", filePath)
}
var newCircleList []*Circle
err = json.Unmarshal(b, &newCircleList)
if err != nil {
t.Fatal(err)
}
if v := len(newCircleList); v != circleLimit {
t.Errorf("unexpected: %v", v)
}
for _, circle := range newCircleList {
if v := len(circle.Images); v != imageLimit {
t.Errorf("unexpected: %v", v)
}
for _, image := range circle.Images {
if v := image.GCSPath; !strings.HasPrefix(v, fmt.Sprintf("%d/", circle.ID)) {
t.Errorf("unexpected: %v", v)
}
}
}
}
{ // Naked Get
q := client.NewQuery(kindImage)
var pss []datastore.PropertyList
_, err = client.GetAll(ctx, q, &pss)
if err != nil {
t.Fatal(err)
}
if v := len(pss); v != circleLimit*imageLimit {
t.Errorf("unexpected: %v", v)
}
for _, ps := range pss {
for _, p := range ps {
if !strings.HasSuffix(p.Name, "ID") {
continue
}
_, ok := p.Value.(datastore.Key)
if !ok {
t.Errorf("unexpected: %T", p.Value)
}
}
}
}
}