Created
January 5, 2018 01:43
-
-
Save samedhi/8c635f547d38e864e02948d682858489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fixture import TestCase | |
from google.appengine.ext import ndb | |
from hypothesis import given | |
from hypothesis.strategies import (floats, integers, text, booleans, | |
datetimes, none, fixed_dictionaries, | |
lists, sampled_from, composite) | |
from user import User | |
PROPERTIES = { | |
ndb.model.IntegerProperty: lambda: integers(), | |
ndb.model.FloatProperty: lambda: floats(allow_nan=False, allow_infinity=False), | |
ndb.model.BooleanProperty: lambda: booleans(), | |
ndb.model.StringProperty: lambda: text(max_size=500), | |
ndb.model.TextProperty: lambda: text(), | |
ndb.model.DateTimeProperty: lambda: datetimes(), | |
ndb.model.StructuredProperty: lambda: none() | |
} | |
def property_strategy(prop): | |
strategy_fx = PROPERTIES[type(prop)] | |
strategy = strategy_fx() | |
if prop._repeated: | |
strategy = lists(strategy) | |
if prop._choices: | |
ls = list(prop._choices) | |
return sampled_from(ls) | |
return strategy | |
def properties_strategy(klass): | |
assert type(klass) == ndb.model.MetaModel | |
props = klass._properties | |
gens = {k:property_strategy(v) for k,v in props.items() | |
if type(v) is not ndb.model.ComputedProperty} | |
return fixed_dictionaries(gens) | |
@composite | |
def entity_strategy(draw, klass): | |
prop_dict = draw(properties_strategy(klass)) | |
return klass(**prop_dict) | |
class UserTest(TestCase): | |
@given(entity_strategy(User)) | |
def test_user(self, user): | |
assert user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment