Skip to content

Instantly share code, notes, and snippets.

@mdornseif
Created December 13, 2018 14:31
Show Gist options
  • Save mdornseif/4a78b9139849f9b34d12fadc80e58bff to your computer and use it in GitHub Desktop.
Save mdornseif/4a78b9139849f9b34d12fadc80e58bff to your computer and use it in GitHub Desktop.
# on the Python side you can not ovbserve a change in type:
# https://stackoverflow.com/questions/15254344/db-stringproperty-but-property-is-occasionally-datastore-types-text
from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel
class TestModel(ndb.Model):
s_ni_nd = ndb.StringProperty(indexed=False)
s_ni_du = ndb.StringProperty(indexed=False, default=unicode('ufoo'))
s_ni_ds = ndb.StringProperty(indexed=False, default=str('sfoo'))
s_i_nd = ndb.StringProperty(indexed=True)
s_i_du = ndb.StringProperty(indexed=True, default=unicode('ufoo'))
s_i_ds = ndb.StringProperty(indexed=True, default=str('sfoo'))
class ReadModel(polymodel.PolyModel):
@classmethod
def _get_kind(cls):
return 'TestModel'
def test_ndb_types(gae):
t1 = TestModel(id='onlydefaults')
t1.put()
t1 = t1.key.get() # reload
t1 = ReadModel.get_by_id(t1.key.id())
assert type(t1.s_ni_du) == unicode
assert type(t1.s_ni_ds) == unicode
assert type(t1.s_i_du) == unicode
assert type(t1.s_i_ds) == unicode
assert type(t1._properties['s_i_ds']) == ndb.StringProperty
assert type(t1._properties['s_ni_du']) == ndb.StringProperty
assert type(t1._properties['s_ni_ds']) == ndb.StringProperty
assert type(t1._properties['s_i_nd']) == ndb.StringProperty
assert type(t1._properties['s_i_du']) == ndb.StringProperty
assert type(t1._properties['s_i_ds']) == ndb.StringProperty
t2 = TestModel(id='allstr',
s_ni_nd=str('s_ni_nd'),
s_ni_du=str('s_ni_du'),
s_ni_ds=str('s_ni_ds'),
s_i_nd=str('s_i_nd '),
s_i_du=str('s_i_du '),
s_i_ds=str('s_i_ds '),
)
t2.put()
t2 = t2.key.get() # reload
t2 = ReadModel.get_by_id(t2.key.id())
assert type(t2.s_ni_nd) == unicode
assert type(t2.s_ni_du) == unicode
assert type(t2.s_ni_ds) == unicode
assert type(t2.s_i_nd) == unicode
assert type(t2.s_i_du) == unicode
assert type(t2.s_i_ds) == unicode
assert type(t2._properties['s_i_ds']) == ndb.StringProperty
assert type(t2._properties['s_ni_du']) == ndb.StringProperty
assert type(t2._properties['s_ni_ds']) == ndb.StringProperty
assert type(t2._properties['s_i_nd']) == ndb.StringProperty
assert type(t2._properties['s_i_du']) == ndb.StringProperty
assert type(t2._properties['s_i_ds']) == ndb.StringProperty
t3 = TestModel(id='allunicode',
s_ni_du=unicode('s_ni_du'),
s_ni_nd=unicode('s_ni_nd'),
s_ni_ds=unicode('s_ni_ds'),
s_i_nd=unicode('s_i_nd '),
s_i_du=unicode('s_i_du '),
s_i_ds=unicode('s_i_ds '),
)
t3.put()
t3 = t2.key.get() # reload
t3 = ReadModel.get_by_id(t3.key.id())
assert type(t3.s_ni_nd) == unicode
assert type(t3.s_ni_du) == unicode
assert type(t3.s_ni_ds) == unicode
assert type(t3.s_i_nd) == unicode
assert type(t3.s_i_du) == unicode
assert type(t3.s_i_ds) == unicode
assert type(t3._properties['s_i_ds']) == ndb.StringProperty
assert type(t3._properties['s_ni_du']) == ndb.StringProperty
assert type(t3._properties['s_ni_ds']) == ndb.StringProperty
assert type(t3._properties['s_i_nd']) == ndb.StringProperty
assert type(t3._properties['s_i_du']) == ndb.StringProperty
assert type(t3._properties['s_i_ds']) == ndb.StringProperty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment