def test_set_sets(self): """__set__ should push to __setitem__ on owner""" attr = Field() attr.name = 'bar' self.owner.__class__.bar = attr self.owner.bar = "Setting this thing!" self.assertEqual(self.owner['bar'], 'Setting this thing!')
def test_get_gets(self): """__get__ should pull from __getitem__ on owner""" self.owner['foo'] = "FunTimes!" attr = Field() attr.name = 'foo' self.owner.__class__.foo = attr self.assertEqual(self.owner.foo, 'FunTimes!')
def test_get_gets_with_type(self): """__get__ should cast value retreived to `type` if set""" attr = Field(type=int) attr.name = 'id' self.owner.__class__.id = attr self.owner['id'] = '123' self.assertEqual(self.owner.id, 123)
def test_del_dels(self): """__delete__ should call __delitem__ on owner""" attr = Field() self.owner['baz'] = 42 attr.name = 'baz' self.owner.__class__.baz = attr del self.owner.baz assert not self.owner.dict.has_key('baz')
def test_leaves_none_alone(self): """should not cast None""" attr = Field(type=int) attr.name = 'foo' self.owner.__class__.foo = attr try: self.owner.foo = None except TypeError: assert False, "should not cast None, but did"
class ReloadTestModel(pyperry.Base): id = Field() a = Field() b = Field() @classmethod def fetch_records(cls, relation): cls.last_relation = relation return [cls({'id': 2, 'a': 3, 'b': 4})]
class HelpModel(pyperry.Base): """a model with a docstring""" attr1 = Field() attr2 = Field() foo = BelongsTo(polymorphic=True) ape = BelongsTo() bars = HasMany(through='bananas') bananas = HasMany()
def test_primary_key(self): """should use configured primary_key in url""" TestModel.id = Field() TestModel.foo = Field() model = TestModel({'id': 7, 'foo': 12345}) self.config['primary_key'] = 'foo' adapter = RestfulHttpAdapter(self.config) url = adapter.url_for('GET', model) self.assertEqual(url, '/widgets/12345.xml')
class Comment(AssocTest): id = Field() person_id = Field() parent_id = Field() parent_type = Field() text = Field() parent = BelongsTo(polymorphic=True) author = BelongsTo(class_name='Person', foreign_key='person_id', namespace='tests.fixtures.association_models')
class Article(AssocTest): id = Field() site_id = Field() author_id = Field() title = Field() text = Field() site = BelongsTo(class_name='Site') author = BelongsTo(class_name='Person') comments = HasMany(as_='parent', class_name='Comment') awesome_comments = HasMany(as_='parent', class_name='Comment', conditions="text LIKE '%awesome%'") comment_authors = HasMany(through='comments', source='author')
class Person(AssocTest): id = Field() name = Field() manager_id = Field() company_id = Field() manager = BelongsTo(class_name='Person', foreign_key='manager_id') authored_comments = HasMany(class_name='Comment', foreign_key='person_id') articles = HasMany(class_name='Article', foreign_key='author_id') comments = HasMany(as_='parent', class_name='Comment') employees = HasMany(class_name='Person', foreign_key='manager_id') sites = HasMany(class_name='Site', foreign_key='maintainer_id') commented_articles = HasMany(through='comments', source='parent', source_type='Article') maintained_articles = HasMany(through='sites', source='articles')
class CallbackTest(pyperry.Base): id = Field() reader = TestAdapter() writer = TestAdapter() log = [] bld = c_bld(lambda (self): self.log.append('before_load')) ald = c_ald(lambda (self): self.log.append('after_load')) bsv = c_bsv(lambda (self): self.log.append('before_save')) asv = c_asv(lambda (self): self.log.append('after_save')) bct = c_bct(lambda (self): self.log.append('before_create')) act = c_act(lambda (self): self.log.append('after_create')) bup = c_bup(lambda (self): self.log.append('before_update')) aup = c_aup(lambda (self): self.log.append('after_update')) bde = c_bde(lambda (self): self.log.append('before_delete')) ade = c_ade(lambda (self): self.log.append('after_delete'))
class Model(pyperry.Base): id = Field() foo = Field()
class Foo(pyperry.Base): id = Field() foo = Field() writer = TestAdapter() writer.features['batch_write'] = True
class Test(pyperry.Base): id = Field() foo = Scope(where='bar') reader = TestAdapter()
class Model(pyperry.Base): id = Field() reader = PreloadTestAdapter(processors=[(PreloadAssociations, {})])
class Test(fixtures.association_models.AssocTest): id = Field() foo_id = Field()
class Test(pyperry.Base): id = Field() name = Field() poop = Field() foo = Field(name='bar')
class Test2(pyperry.Base): id = Field() name = Field()
class SourceModel(pyperry.Base): id = Field() bar = Field() _primary_key = 'bar'
class Test(pyperry.Base): id = Field() name = Field() foo = Field(type=int) bar = Field(default=3)
class Source(pyperry.Base): id = Field()
def test_set_sets_with_type(self): attr = Field(type=int) attr.name = 'id' self.owner.__class__.id = attr self.owner.id = '123' self.assertEqual(self.owner.dict['id'], 123)
class Test(pyperry.Base): foo_id = Field() foo = associations.HasMany()
def test_keywords(self): """should accept keywords type and default""" attr = Field(type=str, default=6, name='foo') self.assertEqual(attr.type, str) self.assertEqual(attr.default, 6) self.assertEqual(attr.name, 'foo')
class Company(AssocTest): id = Field() name = Field() employees = HasMany(class_name='Person', namespace='tests.fixtures.extended_association_models')
def test_sets_name_to_none(self): """should set name attribute to None""" attr = Field() self.assertEqual(attr.name, None)
class Test(pyperry.Base): id = Field() baz_id = Field() baz = associations.BelongsTo( class_name='pyperry_foobar.foo.baz.Bazaroo5234')
class Test(pyperry.Base): id = Field() reader = TestAdapter(middlewares=[(LocalCache, {})])
class TargetModel(pyperry.Base): id = Field() foo = Field() whatever_type = Field() _primary_key = 'foo'
class Test(pyperry.Base): id = Field() writer = TestAdapter()
class Test(pyperry.Base): id = Field()
class Parent2(pyperry.Base): name2 = Field()