/
datastore_test.go
258 lines (209 loc) · 6.87 KB
/
datastore_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
package appx_test
import (
"appengine/aetest"
"appengine/datastore"
"appengine/memcache"
"encoding/json"
"github.com/drborges/appx"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestDatastoreLoad(t *testing.T) {
context, _ := aetest.NewContext(nil)
defer context.Close()
Convey("Given I have a cached entity", t, func() {
user := NewUser(User{
Name: "Borges",
Email: "drborges.cic@gmail.com",
SSN: "123123123",
})
appx.NewKeyResolver(context).Resolve(user)
cached := appx.CachedEntity{
Entity: user,
Key: user.Key(),
ParentKey: user.ParentKey(),
}
memcache.JSON.Set(context, &memcache.Item{
Key: user.CacheID(),
Object: cached,
})
Convey("When I load it with appx datastore", func() {
userFromCache := NewUser(User{
SSN: user.SSN,
})
err := appx.NewDatastore(context).Load(userFromCache)
Convey("Then the entity data is properly loaded", func() {
So(err, ShouldBeNil)
So(userFromCache.SSN, ShouldEqual, user.SSN)
So(userFromCache.Name, ShouldEqual, user.Name)
So(userFromCache.Email, ShouldEqual, user.Email)
So(userFromCache.Key(), ShouldResemble, user.Key())
})
})
})
Convey("Given I have a queriable entity", t, func() {
user := NewUser(User{
Name: "Borges",
Email: "drborges.cic@gmail.com",
SSN: "321321",
})
appx.NewKeyResolver(context).Resolve(user)
datastore.Put(context, user.Key(), user)
// Give datastore some time to index the data before querying
time.Sleep(200 * time.Millisecond)
Convey("When I load it with appx datastore", func() {
userFromDatastore := NewUser(User{
Email: user.Email,
SSN: "321321",
})
err := appx.NewDatastore(context).Load(userFromDatastore)
Convey("Then the entity data is properly loaded", func() {
So(err, ShouldBeNil)
So(userFromDatastore.SSN, ShouldEqual, user.SSN)
So(userFromDatastore.Name, ShouldEqual, user.Name)
So(userFromDatastore.Email, ShouldEqual, user.Email)
So(userFromDatastore.Key(), ShouldResemble, user.Key())
Convey("And the entity is cached in case it is cacheable", func() {
userFromCache := NewUser(User{Name: "Borges"})
cached := appx.CachedEntity{
Entity: userFromCache,
}
item, err := memcache.Get(context, user.CacheID())
json.Unmarshal(item.Value, &cached)
appx.NewKeyResolver(context).Resolve(userFromCache)
So(err, ShouldBeNil)
So(userFromCache, ShouldResemble, user)
})
})
})
})
Convey("Given I have a lookupable entity", t, func() {
user := NewUser(User{
Name: "Borges",
Email: "drborges.cic@gmail.com",
SSN: "987987",
})
appx.NewKeyResolver(context).Resolve(user)
datastore.Put(context, user.Key(), user)
Convey("When I load it with appx datastore", func() {
userFromDatastore := NewUser(User{
Name: "Borges",
})
err := appx.NewDatastore(context).Load(userFromDatastore)
Convey("Then the entity data is properly loaded", func() {
So(err, ShouldBeNil)
So(userFromDatastore.SSN, ShouldEqual, user.SSN)
So(userFromDatastore.Name, ShouldEqual, user.Name)
So(userFromDatastore.Email, ShouldEqual, user.Email)
So(userFromDatastore.Key(), ShouldResemble, user.Key())
Convey("And the entity is not cached if its cache id is empty", func() {
_, err := memcache.Get(context, user.CacheID())
So(err, ShouldEqual, memcache.ErrCacheMiss)
})
})
})
})
}
func TestDatastoreSave(t *testing.T) {
context, _ := aetest.NewContext(nil)
defer context.Close()
Convey("Given I have an entity in datastore", t, func() {
user1 := NewUser(User{
Name: "Borges",
Email: "borges@email.com",
SSN: "123123123",
})
appx.NewKeyResolver(context).Resolve(user1)
datastore.Put(context, user1.Key(), user1)
Convey("And I have an entity not yet saved to datastore", func() {
user2 := NewUser(User{
Name: "Diego",
Email: "diego@email.com",
SSN: "321321321",
})
Convey("When I update the entities in datastore", func() {
err := appx.NewDatastore(context).Save(user1, user2)
Convey("Then the entities are updated in the datastore", func() {
So(err, ShouldBeNil)
user1FromDatastore := &User{}
user2FromDatastore := &User{}
user1FromDatastore.SetKey(user1.Key())
user2FromDatastore.SetKey(user2.Key())
datastore.Get(context, user1.Key(), user1FromDatastore)
datastore.Get(context, user2.Key(), user2FromDatastore)
So(user1FromDatastore.SSN, ShouldEqual, user1.SSN)
So(user1FromDatastore.Name, ShouldEqual, user1.Name)
So(user1FromDatastore.Email, ShouldEqual, user1.Email)
So(user1FromDatastore.Key(), ShouldResemble, user1.Key())
So(user2FromDatastore.SSN, ShouldEqual, user2.SSN)
So(user2FromDatastore.Name, ShouldEqual, user2.Name)
So(user2FromDatastore.Email, ShouldEqual, user2.Email)
So(user2FromDatastore.Key(), ShouldResemble, user2.Key())
})
})
})
})
Convey("Given I have an entity with incomplete key in datastore", t, func() {
user := &User{
Name: "Borges",
keySpec: &appx.KeySpec{
Kind: "Users",
Incomplete: true,
},
}
err := appx.NewDatastore(context).Save(user)
So(err, ShouldBeNil)
userKey := user.Key()
Convey("When I update the entity", func() {
user.Email = "user@email.com"
err := appx.NewDatastore(context).Save(user)
So(err, ShouldBeNil)
Convey("Then the user key is not overriden", func() {
So(userKey, ShouldResemble, user.Key())
})
})
})
}
func TestDatastoreDelete(t *testing.T) {
context, _ := aetest.NewContext(nil)
defer context.Close()
Convey("Given I have an entity in datastore", t, func() {
userInDatastore := NewUser(User{
Name: "Borges",
Email: "borges@email.com",
SSN: "123123123",
})
appx.NewKeyResolver(context).Resolve(userInDatastore)
datastore.Put(context, userInDatastore.Key(), userInDatastore)
Convey("And I have a cached entity though not yet saved to datastore", func() {
cachedUser := NewUser(User{
Name: "Diego",
Email: "diego@email.com",
SSN: "321321321",
})
cached := appx.CachedEntity{
Entity: cachedUser,
Key: cachedUser.Key(),
ParentKey: cachedUser.ParentKey(),
}
memcache.JSON.Set(context, &memcache.Item{
Key: cachedUser.CacheID(),
Object: cached,
})
Convey("And I have a non existent user", func() {
nonExistentUser := NewUser(User{Name: "not existent"})
Convey("When I delete the all", func() {
err := appx.NewDatastore(context).Delete(userInDatastore, cachedUser, nonExistentUser)
Convey("Then the entities are deleted from cache and datastore", func() {
So(err, ShouldBeNil)
err := datastore.Get(context, userInDatastore.Key(), userInDatastore)
So(err, ShouldEqual, datastore.ErrNoSuchEntity)
_, err = memcache.Get(context, cachedUser.CacheID())
So(err, ShouldEqual, memcache.ErrCacheMiss)
})
})
})
})
})
}