-
Notifications
You must be signed in to change notification settings - Fork 3
/
cached_datastore_test.go
107 lines (79 loc) · 2.34 KB
/
cached_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
package db_test
import (
"appengine/aetest"
"appengine/datastore"
"appengine/memcache"
"github.com/drborges/datastore-model"
"github.com/drborges/goexpect"
"testing"
)
type MembershipCard struct {
db.Model
Number int `db:"id"`
Owner string
}
func TestLoadsModelFromCache(t *testing.T) {
c, _ := aetest.NewContext(nil)
defer c.Close()
card := &MembershipCard{Number: 123, Owner: "Borges"}
cds := db.CachedDatastore{db.NewDatastore(c)}
cds.Create(card)
card.Owner = "Diego"
cds.Datastore.Update(card)
cardFromDB := &MembershipCard{Number: 123}
cds.Datastore.Load(cardFromDB)
cardFromCache := &MembershipCard{Number: 123}
err := cds.Load(cardFromCache)
expect := goexpect.New(t)
expect(err).ToBe(nil)
expect(cardFromDB.Owner).ToBe("Diego")
expect(cardFromCache.Owner).ToBe("Borges")
}
func TestUpdatesModelInCacheAndDatastore(t *testing.T) {
c, _ := aetest.NewContext(nil)
defer c.Close()
card := &MembershipCard{Number: 123, Owner: "Borges"}
cds := db.CachedDatastore{db.NewDatastore(c)}
cds.Create(card)
card.Owner = "Diego"
err := cds.Update(card)
cardFromDB := &MembershipCard{Number: 123}
cds.Datastore.Load(cardFromDB)
cardFromCache := &MembershipCard{Number: 123}
cds.Load(cardFromCache)
expect := goexpect.New(t)
expect(err).ToBe(nil)
expect(cardFromDB.Owner).ToBe("Diego")
expect(cardFromCache.Owner).ToBe("Diego")
}
func TestDeletesModelFromCacheAndDatastore(t *testing.T) {
c, _ := aetest.NewContext(nil)
defer c.Close()
card := &MembershipCard{Number: 123, Owner: "Borges"}
cds := db.CachedDatastore{db.NewDatastore(c)}
cds.Create(card)
err := cds.Delete(card)
cardFromCache := &MembershipCard{Number: 123}
errNoSuchEntity := cds.Load(cardFromCache)
expect := goexpect.New(t)
expect(err).ToBe(nil)
expect(errNoSuchEntity).ToBe(datastore.ErrNoSuchEntity)
}
func TestCachedDatastoreUsesTaggedFieldAsCacheKey(t *testing.T) {
c, _ := aetest.NewContext(nil)
defer c.Close()
type MembershipCard struct {
db.Model
Number int `db:"id"`
Owner string `cache:"id"`
}
card := &MembershipCard{Number: 123, Owner: "Diego"}
cds := db.CachedDatastore{db.NewDatastore(c)}
cds.Create(card)
cachedCard := &MembershipCard{}
_, err := memcache.JSON.Get(c, "Diego", cachedCard)
expect := goexpect.New(t)
expect(err).ToBe(nil)
expect(cachedCard.Number).ToBe(card.Number)
expect(cachedCard.Owner).ToBe(card.Owner)
}