OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import datetime |
| 4 import unittest |
| 5 |
| 6 from google.appengine.ext import ndb |
| 7 from google.appengine.ext.ndb import metadata |
| 8 |
| 9 class NormalModel(ndb.Model): |
| 10 boolProp = ndb.BooleanProperty() |
| 11 intProp = ndb.IntegerProperty() |
| 12 stringProp = ndb.StringProperty() |
| 13 keyProp = ndb.KeyProperty() |
| 14 blobProp = ndb.BlobProperty() |
| 15 dateProp = ndb.DateTimeProperty() |
| 16 |
| 17 stringListProp = ndb.StringProperty(repeated = True) |
| 18 |
| 19 |
| 20 class ExpandoModel(ndb.Expando): |
| 21 boolProp = ndb.BooleanProperty() |
| 22 intProp = ndb.IntegerProperty() |
| 23 stringProp = ndb.StringProperty() |
| 24 keyProp = ndb.KeyProperty() |
| 25 blobProp = ndb.BlobProperty() |
| 26 dateProp = ndb.DateTimeProperty() |
| 27 |
| 28 stringListProp = ndb.StringProperty(repeated = True) |
| 29 |
| 30 def verifyData(model, num): |
| 31 assert model.boolProp == (num % 2 == 0) |
| 32 assert model.intProp == num + 42 |
| 33 assert model.stringProp == 'foobar %d' % num |
| 34 assert model.keyProp == ndb.Key('NormalModel', num + 10) |
| 35 assert model.blobProp == '\x01\x02\x03\x04%d' % num |
| 36 |
| 37 # Different |
| 38 assert model.dateProp == datetime.datetime(2014, 12, 31, num) |
| 39 |
| 40 assert model.stringListProp |
| 41 stringList = model.stringListProp |
| 42 assert len(stringList) == 3 |
| 43 assert stringList[0] == 'a%d' % num |
| 44 assert stringList[1] == 'b%d' % num |
| 45 assert stringList[2] == 'c%d' % num |
| 46 |
| 47 def fillData(model, num): |
| 48 model.boolProp = (num % 2 == 0) |
| 49 model.intProp = num + 42 |
| 50 model.stringProp = 'foobar %d' % num |
| 51 model.keyProp = ndb.Key('NormalModel', num + 10) |
| 52 model.blobProp = '\x01\x02\x03\x04%d' % num |
| 53 model.dateProp = datetime.datetime(2014, 12, 31, num) |
| 54 model.stringListProp = ['a%d' % num, 'b%d' % num, 'c%d' % num] |
| 55 |
| 56 |
| 57 def debug_showKinds(): |
| 58 print 'kinds:' |
| 59 for key in metadata.get_kinds(): |
| 60 print ' - ', key |
| 61 |
| 62 def runTests(writing_mode): |
| 63 key = ndb.Key('NormalModel', 99) |
| 64 ekey = ndb.Key('ExpandoModel', 102, parent=key) |
| 65 |
| 66 if writing_mode: |
| 67 model = NormalModel(key=key) |
| 68 em = ExpandoModel(key=ekey) |
| 69 |
| 70 fillData(model, 1) |
| 71 fillData(em, 5) |
| 72 |
| 73 model.put() |
| 74 em.put() |
| 75 |
| 76 if not writing_mode: |
| 77 models = NormalModel.query(NormalModel.key == key).fetch() |
| 78 ems = ExpandoModel.query(ExpandoModel.key == ekey).fetch() |
| 79 |
| 80 for m, id, i in [(models, 99, 1), |
| 81 (ems, 102, 5)]: |
| 82 assert len(m) == 1 |
| 83 model = m[0] |
| 84 assert model |
| 85 assert model.key.id() == id |
| 86 verifyData(model, i) |
| 87 assert ems[0].key.parent() == key |
| 88 |
| 89 print 'All tests ran' |
| 90 |
OLD | NEW |