-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_test.go
144 lines (113 loc) · 2.88 KB
/
delete_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
package nds_test
import (
"strconv"
"testing"
"github.com/qedus/nds"
"errors"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/memcache"
)
func TestDeleteMulti(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type TestEntity struct {
Value int
}
for _, count := range []int{499, 500, 501} {
keys := make([]*datastore.Key, count)
entities := make([]TestEntity, count)
for i := range keys {
keys[i] = datastore.NewKey(c, "TestEntity", strconv.Itoa(i), 0, nil)
entities[i] = TestEntity{i}
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Prime cache.
entities = make([]TestEntity, count)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
if err := nds.DeleteMulti(c, keys); err != nil {
t.Fatal(err)
}
err := nds.GetMulti(c, keys, make([]TestEntity, count))
if err == nil {
t.Fatal("expect error")
}
me, ok := err.(appengine.MultiError)
if !ok {
t.Fatal("should be MultiError")
}
for _, e := range me {
if e != datastore.ErrNoSuchEntity {
t.Fatal("expected ErrNoSuchEntity")
}
}
}
}
func TestDeleteNilKey(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
if err := nds.Delete(c, nil); err != datastore.ErrInvalidKey {
t.Fatal("expected nil key error")
}
}
func TestDeleteIncompleteKey(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
if err := nds.Delete(c, nil); err != datastore.ErrInvalidKey {
t.Fatal("expected invalid key error")
}
}
func TestDeleteMemcacheFail(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
keys := []*datastore.Key{key}
entities := make([]testEntity, 1)
entities[0].Val = 43
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
nds.SetMemcacheSetMulti(func(c context.Context,
items []*memcache.Item) error {
return errors.New("expected error")
})
defer func() {
nds.SetMemcacheSetMulti(memcache.SetMulti)
}()
if err := nds.DeleteMulti(c, keys); err == nil {
t.Fatal("expected DeleteMulti error")
}
}
func TestDeleteInTransaction(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "TestEntity", "", 1, nil)
if _, err := nds.Put(c, key, &testEntity{2}); err != nil {
t.Fatal(err)
}
// Prime cache.
if err := nds.Get(c, key, &testEntity{}); err != nil {
t.Fatal(err)
}
if err := nds.RunInTransaction(c, func(tc context.Context) error {
return nds.DeleteMulti(tc, []*datastore.Key{key})
}, nil); err != nil {
t.Fatal(err)
}
if err := nds.Get(c, key, &testEntity{}); err == nil {
t.Fatal("expected no entity")
} else if err != datastore.ErrNoSuchEntity {
t.Fatal(err)
}
}