class Entity(DatastoreEntity): updated_by = EntityValue(None) created_by = EntityValue() # Not an EntityValue extra_prop = "Extra" __kind__ = 'my_entity'
class User(DatastoreEntity): username = EntityValue(None) email = EntityValue(None) __kind__ = 'User' # call the super class here def __init__(self, **kwargs): super(User, self).__init__(**kwargs)
def test_none_value(): """ Check that when EntityValue is initialized without a value, it defaults to `None` """ entity_value = EntityValue() assert entity_value.value is None
def test_value_passed(): """ Check that the value passed to EntityValue is what is returned """ value = "foobar" entity_value = EntityValue(value) assert entity_value.value == value
class Post(DatastoreEntity): subject = EntityValue(None) text = EntityValue(None) user_id = EntityValue(None) date_created = EntityValue(datetime.datetime.utcnow()) img_name = EntityValue(None) __kind__ = "post" def __init__(self, **kwargs): super(Post, self).__init__(**kwargs) def __repr__(self): return '<Post {}>'.format(self.text) def get_subject(self): return self.subject.__getattribute__()
def test_dynamic_attrs_added_on_the_fly_are_used_as_entity_properties(): """ Attributes to be used as entity properties can be added after intitialization. This allows for entity properties that are not part of model definition to be dynamically added. """ entity = Entity(conn=False) entity.attr_on_the_fly = EntityValue("Dynamic Value") assert isinstance(entity.attr_on_the_fly, EntityValue)
class User(DatastoreEntity, UserMixin): id = EntityValue(None) user_name = EntityValue(None) password = EntityValue(None) user_img_name = EntityValue(None) # specify the name of the entity kind. __kind__ = "user" __exclude_from_index__ = ['password'] def __init__(self, **kwargs): super(User, self).__init__(**kwargs) def set_password(self, password): self.password = password def check_password(self, password): return self.password == password def set_avatar(self): self.user_img_name = self.id[-1] + '.jpg' def get_avatar(self): return self.user_img_name
class ModelMissingKind(DatastoreEntity): username = EntityValue('foo') password = EntityValue(None) date_created = EntityValue(datetime.datetime.utcnow())
class DBModel(DatastoreEntity, UserMixin): username = EntityValue('foo') password = EntityValue(None) date_created = EntityValue(datetime.datetime.utcnow()) __kind__ = 'user'