This repository was archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathpolymodel_test.py
More file actions
455 lines (382 loc) · 11.2 KB
/
polymodel_test.py
File metadata and controls
455 lines (382 loc) · 11.2 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#
# Copyright 2008 The ndb Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for polymodel.py.
See issue 35. http://goo.gl/iHkCm
"""
import pickle
from .google_imports import namespace_manager
from .google_imports import datastore_types
from .google_test_imports import unittest
from . import polymodel
from . import model
from . import query
from . import test_utils
PolyModel = polymodel.PolyModel
class PolyModelTests(test_utils.NDBTest):
def setUp(self):
super(PolyModelTests, self).setUp()
the_module = polymodel
def testBasics(self):
# Test basic PolyModel functionality.
class Shoe(PolyModel):
color = model.StringProperty()
class Moccasin(Shoe):
leather = model.StringProperty()
class Sneaker(Shoe):
pump = model.BooleanProperty()
self.assertEqual(Shoe._class_name(), 'Shoe')
self.assertEqual(Shoe._class_key(), ['Shoe'])
self.assertEqual(Moccasin._class_name(), 'Moccasin')
self.assertEqual(Moccasin._class_key(), ['Shoe', 'Moccasin'])
self.assertEqual(Sneaker._class_name(), 'Sneaker')
self.assertEqual(Sneaker._class_key(), ['Shoe', 'Sneaker'])
s_key = model.Key('Shoe', 1)
self.assertEqual(Shoe().put(), s_key)
s = s_key.get()
self.assertEqual(s._get_kind(), 'Shoe')
self.assertEqual(s._class_key(), ['Shoe'])
self.assertEqual(s.class_, ['Shoe'])
m_key = model.Key('Shoe', 2)
self.assertEqual(Moccasin(color='brown', leather='cattlehide').put(),
m_key)
m = m_key.get()
self.assertEqual(m._get_kind(), 'Shoe')
self.assertEqual(m.class_, ['Shoe', 'Moccasin'])
snkr_key = model.Key('Shoe', 3)
self.assertEqual(Sneaker(color='red', pump=False).put(), snkr_key)
snkr = snkr_key.get()
self.assertEqual(snkr._get_kind(), 'Shoe')
self.assertEqual(snkr.class_, ['Shoe', 'Sneaker'])
self.assertEqual(Shoe.query().fetch(), [s, m, snkr])
self.assertEqual(Shoe.query(Sneaker.pump == False).fetch(), [snkr])
self.assertEqual(Moccasin.query().fetch(), [m])
self.assertEqual(Sneaker.query().fetch(), [snkr])
def testBlobKeyProperty(self):
class MyModel(PolyModel):
pass
class MyDerivedModel(MyModel):
image = model.BlobKeyProperty()
test_blobkey = datastore_types.BlobKey('testkey123')
m = MyDerivedModel()
m.image = test_blobkey
m.put()
m = m.key.get()
m.image = test_blobkey
m.put()
self.assertTrue(isinstance(m.image, datastore_types.BlobKey))
self.assertEqual(str(m.image), str(test_blobkey))
def testClassKeyProperty(self):
# Tests for the class_ property.
class Animal(PolyModel):
pass
class Dog(Animal):
pass
fido = Dog()
self.assertEqual(fido.class_, ['Animal', 'Dog'])
self.assertRaises(TypeError, setattr, fido, 'class_', ['Animal', 'Dog'])
def testPolyExpando(self):
# Test that PolyModel can be combined with Expando.
# (See also testExpandoPoly, and the Ghoul class in testInheritance.)
class Animal(PolyModel, model.Expando):
pass
class Mammal(Animal):
pass
cat = Mammal(name='Tom', naps=18, sound='purr')
cat1 = cat.put().get()
self.assertFalse(cat1 is cat)
self.assertEqual(cat1, cat)
self.assertEqual(cat1.name, 'Tom')
self.assertEqual(cat1.naps, 18)
self.assertEqual(cat1.sound, 'purr')
def testExpandoPoly(self):
# Like testPolyExpando, but switch the order of the base classes.
# It should work either way.
class Animal(model.Expando, PolyModel):
pass
class Mammal(Animal):
pass
cat = Mammal(name='Tom', naps=18, sound='purr')
cat1 = cat.put().get()
self.assertFalse(cat1 is cat)
self.assertEqual(cat1, cat)
self.assertEqual(cat1.name, 'Tom')
self.assertEqual(cat1.naps, 18)
self.assertEqual(cat1.sound, 'purr')
def testInheritance(self):
# Tests focused on the inheritance model, including diamond inheritance.
class NamedThing(model.Model):
name = model.StringProperty()
class Animal(PolyModel, NamedThing):
legs = model.IntegerProperty(default=4)
class Canine(Animal):
pass
class Dog(Canine):
breed = model.StringProperty(default='mutt')
class Wolf(Canine):
mythical = model.BooleanProperty(default=False)
class Feline(Animal):
sound = model.StringProperty()
class Cat(Feline):
naps = model.IntegerProperty()
class Panther(Feline):
pass
class Monster(Dog, Cat):
ancestry = model.StringProperty()
class Ghoul(Monster, model.Expando):
pass
k9 = Canine(name='Reynard')
self.assertEqual(k9.legs, 4)
self.assertEqual(k9._get_kind(), 'Animal')
self.assertEqual(k9._class_name(), 'Canine')
self.assertEqual(k9._class_key(), ['Animal', 'Canine'])
tom = Cat(name='Tom', naps=12, sound='purr')
self.assertTrue(isinstance(tom, Cat))
self.assertTrue(isinstance(tom, Feline))
self.assertTrue(isinstance(tom, Animal))
self.assertTrue(isinstance(tom, PolyModel))
self.assertEqual(tom.naps, 12)
self.assertEqual(tom.sound, 'purr')
self.assertEqual(tom.legs, 4)
self.assertEqual(tom._get_kind(), 'Animal')
self.assertEqual(tom._class_name(), 'Cat')
self.assertEqual(tom._class_key(), ['Animal', 'Feline', 'Cat'])
fido = Wolf(name='Warg')
self.assertEqual(fido._get_kind(), 'Animal')
self.assertEqual(fido._class_name(), 'Wolf')
self.assertEqual(fido._class_key(), ['Animal', 'Canine', 'Wolf'])
self.assertRaises(AttributeError, lambda: fido.breed)
scary = Ghoul(name='Westminster', book='The Graveyard Book')
self.assertEqual(scary.ancestry, None)
self.assertEqual(scary._get_kind(), 'Animal')
self.assertEqual(scary._class_name(), 'Ghoul')
self.assertEqual(scary._class_key(), ['Animal',
'Feline', 'Cat',
'Canine', 'Dog',
'Monster', 'Ghoul'])
k91 = k9.put().get()
self.assertTrue(isinstance(k9, Canine))
self.assertEqual(k9.name, 'Reynard')
self.assertEqual(k9._get_kind(), 'Animal')
self.assertEqual(k9._class_name(), 'Canine')
self.assertEqual(k9._class_key(), ['Animal', 'Canine'])
self.assertTrue(isinstance(k91, Canine))
self.assertEqual(k91.name, 'Reynard')
self.assertEqual(k91._get_kind(), 'Animal')
self.assertEqual(k91._class_name(), 'Canine')
self.assertEqual(k91._class_key(), ['Animal', 'Canine'])
self.assertEqual(k91, k9)
tom1 = tom.put().get()
self.assertEqual(tom1, tom)
fido1 = fido.put().get()
self.assertEqual(fido1, fido)
scary1 = scary.put().get()
self.assertEqual(scary1, scary)
self.assertEqual(scary1.book, 'The Graveyard Book')
def testPickling(self):
# Test that PolyModel instances are pickled and unpickled properly.
global Animal, Dog
class Animal(PolyModel):
name = model.StringProperty()
class Dog(Animal):
breed = model.StringProperty()
for proto in 0, 1, 2:
fido = Dog(name='Fido', breed='chihuahua')
s = pickle.dumps(fido, proto)
fido1 = pickle.loads(s)
self.assertEqual(fido1.name, 'Fido')
self.assertEqual(fido1.breed, 'chihuahua')
self.assertEqual(fido1.class_, ['Animal', 'Dog'])
self.assertEqual(fido, fido1)
def testClassNameOverride(self):
# Test that overriding _class_name() works.
class Animal(PolyModel):
pass
class Feline(Animal):
pass
class Cat(Feline):
@classmethod
def _class_name(cls):
return 'Pussycat'
tom = Cat()
self.assertEqual(tom.class_, ['Animal', 'Feline', 'Pussycat'])
tom.put()
self.assertEqual(Cat.query().fetch(), [tom])
def testEdgeCases(self):
# Test some edge cases.
self.assertEqual(PolyModel._get_kind(), 'PolyModel')
def testMixins(self):
class Mixin(object):
pass
class Entity(polymodel.PolyModel):
pass
class ChildEntity(Entity):
pass
class RightMixinEntity(Entity, Mixin):
pass
class LeftMixinEntity(Mixin, Entity):
pass
self.assertEqual(Entity._get_kind(), 'Entity')
self.assertEqual(ChildEntity._get_kind(), 'Entity')
self.assertEqual(RightMixinEntity._get_kind(), 'Entity')
self.assertEqual(LeftMixinEntity._get_kind(), 'Entity')
def testGql(self):
# See issue 199.
class A(polymodel.PolyModel):
pass
class B(A):
pass
class C(A):
pass
b = B()
b.put()
c = C()
c.put()
self.assertEqual(query.gql('SELECT * FROM A').fetch(), [b, c])
self.assertEqual(B.gql('').fetch(), [b])
self.assertEqual(query.gql('SELECT * FROM B').fetch(), [b])
def testQueryFilter(self):
# Test that query on root class should not filter.
class Animal(PolyModel):
pass
class Cat(Animal):
pass
self.assertEqual(Animal.query().filters, None)
self.assertNotEqual(Cat.query().filters, None)
TOM_PB2 = """\
key {
app: "ndb-test-app-id"
path {
Element {
type: "Animal"
}
}
}
property {
name: "class"
multiple: true
value {
stringValue: "Animal"
}
}
property {
name: "class"
multiple: true
value {
stringValue: "Feline"
}
}
property {
name: "class"
multiple: true
value {
stringValue: "Cat"
}
}
property {
name: "name"
multiple: false
value {
stringValue: "Tom"
}
}
property {
name: "purr"
multiple: false
value {
stringValue: "loud"
}
}
property {
name: "whiskers"
multiple: false
value {
booleanValue: true
}
}
entity_group {
}
"""
TOM_PB1 = """\
key <
app: "ndb-test-app-id"
path <
Element {
type: "Animal"
}
>
>
entity_group <
>
property <
name: "class"
value <
stringValue: "Animal"
>
multiple: true
>
property <
name: "class"
value <
stringValue: "Feline"
>
multiple: true
>
property <
name: "class"
value <
stringValue: "Cat"
>
multiple: true
>
property <
name: "name"
value <
stringValue: "Tom"
>
multiple: false
>
property <
name: "purr"
value <
stringValue: "loud"
>
multiple: false
>
property <
name: "whiskers"
value <
booleanValue: true
>
multiple: false
>
"""
class CompatibilityTests(test_utils.NDBTest):
def testCompatibility(self):
class Animal(PolyModel):
name = model.StringProperty()
class Feline(Animal):
whiskers = model.BooleanProperty()
class Cat(Feline):
purr = model.StringProperty()
tom = Cat(name='Tom', purr='loud', whiskers=True)
tom._prepare_for_put()
try:
self.assertEqual(str(tom._to_pb()), TOM_PB2)
except AssertionError:
# Some test environment setups (e.g py27 runtime) use proto2.
# Some still use proto1 (e.g dev_appserver).
self.assertEqual(str(tom._to_pb()), TOM_PB1)
if __name__ == '__main__':
unittest.main()