def test_0_property(self): """Test that unbound Property objects can be created successfully""" prop = Property() self.assertIsNotNone(prop) self.assertIsInstance(prop, Property) self.assertIsInstance(type(prop), MetaProperty) self.assertRegexpMatches(str(prop), r".*unbound.*", re.I) roprop = Property(traits=['ro']) self.assertIsNotNone(roprop) self.assertIsInstance(roprop, ROProperty) self.assertIsInstance(type(prop), MetaProperty) roprop = ROProperty() self.assertIsNotNone(roprop) self.assertIsInstance(roprop, ROProperty) lazyprop = Property(lazy=True) self.assertIsInstance(lazyprop, LazyProperty) self.assertFalse(isinstance(lazyprop, SafeProperty)) safelazyprop = Property(lazy=True, isa=str) self.assertIsInstance(safelazyprop, LazyProperty) self.assertIsInstance(safelazyprop, SafeProperty) self.assertRaises(exc.LazyIsFalse, Property, lazy=False) self.assertRaises(exc.CoerceWithoutType, Property, coerce=lambda x: 1)
class Person(LegalPerson): age = Property(isa=int) kids = Property(isa=int, extraneous=True) interests = SafeProperty(isa=list) info = SafeProperty(isa=dict) primary_key = ['id'] family = DictProperty(of=LegalPerson)
class Mixed(Record): id = Property(required=True, isa=int, coerce=positive_int_or_none) num = Property(isa=int, coerce=positive_int_or_none) def get_what(self): return "I'm Mixed %d" % self.id what = Property( default=get_what, isa=int, lazy=True, required=True, ) def get_hmm(self): return positive_int_or_none(self.what) hmm = Property(isa=int, required=True, lazy=True, default=get_hmm) def get_huh(self): return str(self.what) huh = Property(isa=int, required=True, lazy=True, coerce=positive_int_or_none, default=get_huh)
class Post(Record): comments = ListProperty(of=Comment) content = Property() edited = DatetimeProperty(required=True) post_id = Property(required=True, isa=int) wall_id = Property(required=True, isa=int) primary_key = [wall_id, post_id]
class Star(Record): hip_id = Property(isa=int, required=True, coerce=fix_id, check=lambda i: 0 < i < 120000) name = Property(isa=str) spectral_type = Property(isa=str)
class Star(Record): hip_id = Property(isa=int, required=True, coerce=fix_id, check=lambda i: 0 < i < 120000) name = Property(isa=str) spectral_type = Property(isa=str) designations = DictProperty(of=basestring) coordinates = DictProperty(of=list_of(basestring))
def test_property_mixin_exc(self): """Test that bad property mixes raise the right exceptions""" class SuperProperty(SafeProperty): __trait__ = "pony" def __init__(self, hero_name=None, **kwargs): super(SuperProperty, self).__init__(**kwargs) Property(hero_name="Bruce Wayne") with self.assertRaises(exc.PropertyTypeMixinNotPossible): Property(hero_name="Bruce Wayne", traits=['unsafe'])
class NaturalBornObject(Record): count = Property( isa=(NaturalNumber, BigNaturalNumber), coerce=lambda x: (abs(int(x)) if abs(long(x)) < sys.maxsize else abs(long(x))), check=lambda N: N > 0, )
class PackedDate(Record): created_date = Property( check=lambda x: re.match(r'\d{4}-\d{2}-\d{2}', x), isa=str, json_in=date_in, json_name="created", json_out=date_out, )
def test_property_mixin_ok(self): """Test that properties can be mixed in automagically""" class MyLittleProperty(Property): __trait__ = "mylittle" def __init__(self, pony_name=None, **kwargs): super(MyLittleProperty, self).__init__(**kwargs) mlp = Property(pony_name="Applejack", isa=str) self.assertIsInstance(mlp, MyLittleProperty) self.assertIsInstance(mlp, SafeProperty) self.assertEqual(type(mlp).traits, ("mylittle", "safe")) lazypony = Property(pony_name="Persnickety", lazy=lambda: "x") self.assertEqual(type(lazypony).traits, ("lazy", "mylittle")) self.assertIsInstance(lazypony, MyLittleProperty) self.assertIsInstance(lazypony, LazyProperty)
class TrapDoorRecord(Record): def _shoot(self): projectile = self.chamber self.chamber = "empty" return projectile chamber = Property() fired = LazyProperty(default=_shoot) ask = LazyProperty(default=_seq) plus = LazyProperty(default=_func_with_default_args)
class StreamChunk(JsonRecordList): itemtype = CheeseRecord next_url = Property() previous_url = Property() @classmethod def json_to_initkwargs(cls, json_data, kwargs): paging = json_data.get('paging', {}) kwargs['next_url'] = paging.get('next', None) kwargs['previous_url'] = paging.get('previous', None) kwargs = super(StreamChunk, cls).json_to_initkwargs( json_data.get('data', []), kwargs, ) return kwargs def json_data(self): return dict( data=super(StreamChunk, self).json_data(), paging=dict( next=self.next_url, previous=self.previous_url, ), )
class Comment(Record): content = Property() edited = DatetimeProperty(required=True) id = Property(required=True, isa=int) primary_key = [id] poster = Property(isa=Person)
class LegalPerson(Record): id = Property(required=True, isa=int) name = Property(isa=basestring, coerce=str)
class PullRequest(Record): number = Property() created_at = DatetimeProperty(default=lambda: datetime.now()) merged_at = DatetimeProperty(isa=(datetime, types.NoneType))
class NamedThing(Thing): name = Property()
class Component(Record): ident = Property(isa=str) pk = (ident)
class Nullable(Record): data = Property()
class Wall(Record): id = Property(required=True, isa=int) owner = Property(isa=Person) posts = ListProperty(of=Post)
class TrivialRecord(Record): id = ROProperty() name = Property()
class BasicRecord(Record): name = Property() defaulted = Property(default=lambda: []) default_none = Property(default=None)
def test_unknown_kwarg(self): with self.assertRaisesRegexp(TypeError, r"'yo_momma' of Property"): Property(yo_momma="so fat, when she sits around the house, " "she really SITS AROUND THE HOUSE")
class Warfare(Record): proleteriat = Property(list_of=Person) bourgeois = ListProperty(of=Person)
class Person(Record): name = Property()
class FakeSite(JsonRecord): slug = Property() _custom_tags = DictProperty(of=list_of(str))
class Binary(Record): name = Property(isa=str) primary = Property(isa=Star) secondary = Property(isa=Star)
class Container(Record): name = Property() things = ListProperty(of=Nullable)
class NamedStarList(StarList): name = Property()
class Compound(Record): part_a = Property(isa=Component) part_b = Property(isa=Component) pk = [part_a, part_b]
class StarSystem(Record): name = Property(isa=str) components = Property(isa=StarList) attributes = DictProperty(of=StarAttribute)