-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
179 lines (154 loc) · 3.3 KB
/
store_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
package ae
import (
"os"
"testing"
"github.com/chrisolsen/ae/testutils"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/memcache"
)
var T = testutils.T{}
func TestMain(m *testing.M) {
os.Exit(func() int {
code := m.Run()
T.Close()
return code
}())
}
func TestDeleteNonExistingRecord(t *testing.T) {
c := T.GetContext()
b := Store{}
key := datastore.NewIncompleteKey(c, "users", nil)
err := b.Delete(c, key)
if err == nil {
t.Errorf("ErrNoSuchEntity expsted")
return
}
}
func TestDeleteFromDatastore(t *testing.T) {
c := T.GetContext()
b := Store{TableName: "users"}
type person struct {
Name string
}
// create person
p := person{Name: "Jim"}
key, err := b.Create(c, &p, nil)
if err != nil {
t.Errorf("failed to create person: %v", err)
return
}
// verify creatio
var pverify person
_, err = b.Get(c, key, &pverify)
if err != nil {
t.Errorf("can't get person: %v", err)
return
}
// test exists in memcache
var cperson person
_, err = memcache.Gob.Get(c, key.Encode(), &cperson)
if err != nil {
t.Errorf("failed to fetch from memcache: %v", err)
return
}
if cperson.Name != p.Name {
t.Errorf("invalid cache match: %v", err)
return
}
// delete user
err = b.Delete(c, key)
if err != nil {
t.Errorf("failed to delete person: %v", err)
return
}
// test cleared from db
err = datastore.Get(c, key, &pverify)
if err != datastore.ErrNoSuchEntity {
t.Errorf("person still exists in database: %v", err)
return
}
// test cleared from memcache
_, err = memcache.Get(c, key.Encode())
if err != memcache.ErrCacheMiss {
t.Errorf("should no longer exist in memcache: %v", err)
return
}
}
func TestUpdate(t *testing.T) {
c := T.GetContext()
b := Store{TableName: "users"}
type person struct {
Name string
}
// create person
p1 := person{Name: "Jim"}
key, err := b.Create(c, &p1, nil)
if err != nil {
t.Errorf("failed to create person: %v", err)
return
}
p1.Name = "Sam"
err = b.Update(c, key, &p1)
if err != nil {
t.Errorf("failed to update person: %v", err)
return
}
// test deleted from memcache
var p2 person
_, err = memcache.Gob.Get(c, key.Encode(), &p2)
if err != memcache.ErrCacheMiss {
t.Errorf("failed to delete from memcache: %v", err)
return
}
// test updated in db
var p3 person
err = datastore.Get(c, key, &p3)
if err != nil {
t.Errorf("person should still exist in db: %v", err)
return
}
if p3.Name != "Sam" {
t.Errorf("person not updated in db")
return
}
}
func TestGetNilVals(t *testing.T) {
c := T.GetContext()
s := Store{TableName: "users"}
key := datastore.NewIncompleteKey(c, "users", nil)
type user struct {
Name string
}
var u user
_, err := s.Get(c, key, &u)
if err == nil {
t.Error("ErrNoSuchEntity expected")
return
}
}
func TestGetFromMemcache(t *testing.T) {
c := T.GetContext()
s := Store{TableName: "users"}
key := datastore.NewIncompleteKey(c, "users", nil)
type user struct {
Name string
}
var u user
key, err := s.Create(c, &u, nil)
if err != nil {
t.Errorf("error on create: %v", err)
return
}
// prime memcache
_, err = s.Get(c, key, &u)
if err != nil {
t.Errorf("error on priming get: %v", err)
return
}
// should now retrieve from memcache
_, err = s.Get(c, key, &u)
if err != nil {
t.Errorf("error on memcache get: %v", err)
return
}
}