/
key_resolver_test.go
185 lines (152 loc) · 4.72 KB
/
key_resolver_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
package appx_test
import (
"appengine/aetest"
"appengine/datastore"
"github.com/drborges/appx"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestKeyResolver(t *testing.T) {
context, _ := aetest.NewContext(nil)
defer context.Close()
Convey("Given I have a key manager", t, func() {
manager := appx.NewKeyResolver(context)
Convey("When I resolve a key of an entity with the key already set", func() {
entity := &User{
keySpec: &appx.KeySpec{
Kind: "Entity",
IntID: 123,
},
}
key := datastore.NewKey(context, entity.keySpec.Kind, "", entity.keySpec.IntID, nil)
entity.SetKey(key)
err := manager.Resolve(entity)
Convey("Then it succeeds", func() {
So(err, ShouldBeNil)
Convey("And the entity key is not overriden", func() {
So(entity.Key(), ShouldEqual, key)
})
})
})
Convey("When I resolve a key of an entity with no parent", func() {
entity := &User{
keySpec: &appx.KeySpec{
Kind: "Entity",
IntID: 123,
},
}
err := manager.Resolve(entity)
Convey("Then it succeeds", func() {
So(err, ShouldBeNil)
Convey("And the resolved key is set back to the entity", func() {
So(entity.Key().StringID(), ShouldEqual, "")
So(entity.Key().IntID(), ShouldEqual, entity.keySpec.IntID)
So(entity.Key().Kind(), ShouldEqual, entity.keySpec.Kind)
So(entity.Key().Parent(), ShouldBeNil)
})
})
})
Convey("When I resolve a key of an entity with parent", func() {
entity := &User{
keySpec: &appx.KeySpec{
Kind: "Entity",
IntID: 123,
HasParent: true,
},
}
parentKey := datastore.NewKey(context, "Parent", "key", 0, nil)
entity.SetParentKey(parentKey)
err := manager.Resolve(entity)
Convey("Then it succeeds", func() {
So(err, ShouldBeNil)
Convey("And the resolved key is set back to the entity", func() {
So(entity.Key().Kind(), ShouldEqual, entity.keySpec.Kind)
So(entity.Key().IntID(), ShouldEqual, entity.keySpec.IntID)
So(entity.Key().StringID(), ShouldEqual, "")
So(entity.Key().Parent().Kind(), ShouldEqual, parentKey.Kind())
So(entity.Key().Parent().IntID(), ShouldEqual, 0)
So(entity.Key().Parent().StringID(), ShouldEqual, parentKey.StringID())
})
})
})
Convey("When I resolve a key of an entity whose key spec is incomplete", func() {
entity := &User{
keySpec: &appx.KeySpec{
Kind: "People",
Incomplete: true,
},
}
err := manager.Resolve(entity)
Convey("Then key is resolved as incomplete", func() {
So(err, ShouldBeNil)
So(entity.Key().Incomplete(), ShouldBeTrue)
})
})
Convey("When I resolve a key of an entity whose key spec is missing kind information", func() {
err := manager.Resolve(&User{
keySpec: &appx.KeySpec{},
})
Convey("Then it fails key resolution", func() {
So(err, ShouldEqual, appx.ErrMissingEntityKind)
})
})
Convey("When I resolve a key of an entity whose key spec is of an incomplete key", func() {
err := manager.Resolve(&User{
keySpec: &appx.KeySpec{Kind: "Entity"},
})
Convey("Then it fails key resolution", func() {
So(err, ShouldEqual, appx.ErrIncompleteKey)
})
})
Convey("When I resolve a key of an entity whose key spec is incomplete an StringID is set", func() {
err := manager.Resolve(&User{
keySpec: &appx.KeySpec{
Kind: "Entity",
Incomplete: true,
StringID: "string id",
},
})
Convey("Then it fails key resolution", func() {
So(err, ShouldEqual, appx.ErrIncompleteKey)
})
})
Convey("When I resolve a key of an entity whose key spec is incomplete an IntID is set", func() {
err := manager.Resolve(&User{
keySpec: &appx.KeySpec{
Kind: "Entity",
Incomplete: true,
IntID: 123,
},
})
Convey("Then it fails key resolution", func() {
So(err, ShouldEqual, appx.ErrIncompleteKey)
})
})
Convey("When I resolve a key of an entity whose key spec requires a parent key and it's missing", func() {
err := manager.Resolve(&User{
keySpec: &appx.KeySpec{
Kind: "Entity",
IntID: 123,
HasParent: true,
},
})
Convey("Then it fails key resolution", func() {
So(err, ShouldEqual, appx.ErrMissingParentKey)
})
})
Convey("When I resolve a key of an entity whose key spec requires a parent key and parent key is incomplete", func() {
entity := &User{
keySpec: &appx.KeySpec{
Kind: "Entity",
IntID: 123,
HasParent: true,
},
}
entity.SetParentKey(datastore.NewIncompleteKey(context, "parent", nil))
err := manager.Resolve(entity)
Convey("Then it fails key resolution", func() {
So(err, ShouldEqual, appx.ErrIncompleteParentKey)
})
})
})
}