Пример #1
0
    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)
Пример #2
0
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)
Пример #3
0
        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)
Пример #4
0
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]
Пример #5
0
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)
Пример #6
0
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))
Пример #7
0
    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'])
Пример #8
0
 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,
     )
Пример #9
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,
     )
Пример #10
0
    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)
Пример #11
0
        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)
Пример #12
0
        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,
                    ),
                )
Пример #13
0
class Comment(Record):
    content = Property()
    edited = DatetimeProperty(required=True)
    id = Property(required=True, isa=int)
    primary_key = [id]
    poster = Property(isa=Person)
Пример #14
0
class LegalPerson(Record):
    id = Property(required=True, isa=int)
    name = Property(isa=basestring, coerce=str)
Пример #15
0
class PullRequest(Record):
    number = Property()
    created_at = DatetimeProperty(default=lambda: datetime.now())
    merged_at = DatetimeProperty(isa=(datetime, types.NoneType))
Пример #16
0
 class NamedThing(Thing):
     name = Property()
Пример #17
0
 class Component(Record):
     ident = Property(isa=str)
     pk = (ident)
Пример #18
0
 class Nullable(Record):
     data = Property()
Пример #19
0
class Wall(Record):
    id = Property(required=True, isa=int)
    owner = Property(isa=Person)
    posts = ListProperty(of=Post)
Пример #20
0
 class TrivialRecord(Record):
     id = ROProperty()
     name = Property()
Пример #21
0
 class BasicRecord(Record):
     name = Property()
     defaulted = Property(default=lambda: [])
     default_none = Property(default=None)
Пример #22
0
 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")
Пример #23
0
 class Warfare(Record):
     proleteriat = Property(list_of=Person)
     bourgeois = ListProperty(of=Person)
Пример #24
0
 class Person(Record):
     name = Property()
Пример #25
0
 class FakeSite(JsonRecord):
     slug = Property()
     _custom_tags = DictProperty(of=list_of(str))
Пример #26
0
class Binary(Record):
    name = Property(isa=str)
    primary = Property(isa=Star)
    secondary = Property(isa=Star)
Пример #27
0
 class Container(Record):
     name = Property()
     things = ListProperty(of=Nullable)
Пример #28
0
class NamedStarList(StarList):
    name = Property()
Пример #29
0
 class Compound(Record):
     part_a = Property(isa=Component)
     part_b = Property(isa=Component)
     pk = [part_a, part_b]
Пример #30
0
class StarSystem(Record):
    name = Property(isa=str)
    components = Property(isa=StarList)
    attributes = DictProperty(of=StarAttribute)