def test_belongs_to(self): """ should use the source class's primary key in a belongs to association """ association = BelongsTo(target_klass=TargetModel, id='whatever', class_name='SourceModel') self.assertEqual(association.primary_key(), SourceModel.primary_key())
def setUp(self): self.klass = fixtures.association_models.Test self.id = 'belongs' self.foreign_key = 'some_id' self.belongs_to = BelongsTo(target_klass=self.klass, id=self.id) self.article = fixtures.association_models.Article self.comment = fixtures.association_models.Comment
def test_polymorphic_belongs_to(self): """ should use the source class's primary key in a polymorphic belongs to association """ association = BelongsTo(TargetModel, "whatever", polymorphic=True) target_instance = TargetModel({"whatever_type": "SourceModel"}) self.assertEqual(association.primary_key(target_instance), SourceModel.primary_key())
def test_kwarg(self): """should use the primary key given in the kwargs""" association = BelongsTo(target_klass=TargetModel, id='whatever', primary_key='asdf') self.assertEqual(association.primary_key(), 'asdf') association = Has(target_klass=TargetModel, id='whatever', primary_key='asdf') self.assertEqual(association.primary_key(), 'asdf')
def test_polymorphic(self): """should return true for polymorphic if polymorphic options specified and false otherwise""" self.assertFalse(self.belongs_to.polymorphic()) belongs = BelongsTo(self.comment, "parent", polymorphic=True) self.assertTrue(belongs.polymorphic()) comment = self.comment({"parent_id": 1, "parent_type": "Article"}) scope = belongs.scope(comment) self.assertEqual(scope.__class__, pyperry.Relation)
def test_polymorphic(self): """should return true for polymorphic if polymorphic options specified and false otherwise""" self.assertFalse(self.belongs_to.polymorphic()) belongs = BelongsTo(target_klass=self.comment, id='parent', polymorphic=True) self.assertTrue(belongs.polymorphic()) comment = self.comment({ 'parent_id': 1, 'parent_type': 'Article' }) scope = belongs.scope(comment) self.assertEqual(scope.__class__, pyperry.Relation)
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_polymorphic_belongs_to(self): """ should use the source class's primary key in a polymorphic belongs to association """ association = BelongsTo(target_klass=TargetModel, id='whatever', polymorphic=True) target_instance = TargetModel({'whatever_type': 'SourceModel'}) self.assertEqual(association.primary_key(target_instance), SourceModel.primary_key())
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')
def setUp(self): self.klass = fixtures.association_models.Test self.id = "belongs" self.foreign_key = "some_id" self.belongs_to = BelongsTo(self.klass, self.id) self.article = fixtures.association_models.Article self.comment = fixtures.association_models.Comment
class DirModel(pyperry.Base): id = Field() foo = Field() bar = Field() owner = BelongsTo() children = HasMany()
class Site(AssocTest): id = Field() name = Field() maintainer_id = Field() maintainer = BelongsTo(klass=lambda: Person) headline = HasOne(klass=lambda: Article) master_comment = HasOne(as_='parent', class_name='Comment') fun_articles = HasOne(class_name='Article', conditions='1', sql=lambda s: """ SELECT articles.* FROM articles WHERE articles.text LIKE %%monkeyonabobsled%% AND articles.site_id = %s """ % s.id) articles = HasMany(class_name='Article', namespace='tests.fixtures.association_models') comments = HasMany(as_='parent', class_name='Comment') awesome_comments = HasMany(class_name='Comment', conditions='1', sql=lambda s: """ SELECT comments.* FROM comments WHERE comments.text LIKE '%%awesome%%' AND parent_type = "Site" AND parent_id = %s """ % s.id) article_comments = HasMany(through='articles', source='comments')
def test_get_descriptor(self): """should return first object in scope""" TestAdapter.data = { 'id': 1 } class Test(fixtures.association_models.AssocTest): id = Field() foo_id = Field() assoc = BelongsTo(target_klass=Test, id='foo', klass=Test) Test.foo = assoc instance = Test(foo_id=1) self.assertEqual(Test.foo, assoc) self.assertEqual(type(instance.foo), Test)
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')
def test_kwarg(self): """should use the primary key given in the kwargs""" association = BelongsTo(TargetModel, "whatever", primary_key="asdf") self.assertEqual(association.primary_key(), "asdf") association = Has(TargetModel, "whatever", primary_key="asdf") self.assertEqual(association.primary_key(), "asdf")
class AssociationModel(pyperry.Base): you = BelongsTo() foo = BelongsTo(polymorphic=True) bar = HasOne() bizs = HasMany() bazs = HasMany(through='bizs')
def test_polymorphic_type(self): """should return #{id}_type if it is polymorphic""" belongs = BelongsTo(target_klass=self.klass, id='bar', polymorphic=True) self.assertEqual('bar_type', belongs.polymorphic_type())
class BelongsToTestCase(BaseAssociationTestCase): def setUp(self): self.klass = fixtures.association_models.Test self.id = "belongs" self.foreign_key = "some_id" self.belongs_to = BelongsTo(self.klass, self.id) self.article = fixtures.association_models.Article self.comment = fixtures.association_models.Comment def test_type(self): """should return 'belongs_to' as the type""" self.assertEqual("belongs_to", self.belongs_to.type()) def test_collection(self): """should return False on a collection""" self.assertEqual(False, self.belongs_to.collection()) def test_foreign_key_not_set(self): """should return the association's foreign_key if one is set""" self.belongs_to.foreign_key = self.foreign_key self.assertEqual(self.foreign_key, self.belongs_to.foreign_key) def test_foreign_key_not_set_in_super(self): """sould return #{id}_id if the association doesn't have a foreign_key""" self.assertEqual("%s_id" % self.id, self.belongs_to.foreign_key) # need to test polymorphic and polymorphic_type def test_polymorphic(self): """should return true for polymorphic if polymorphic options specified and false otherwise""" self.assertFalse(self.belongs_to.polymorphic()) belongs = BelongsTo(self.comment, "parent", polymorphic=True) self.assertTrue(belongs.polymorphic()) comment = self.comment({"parent_id": 1, "parent_type": "Article"}) scope = belongs.scope(comment) self.assertEqual(scope.__class__, pyperry.Relation) def test_polymorphic_type(self): """should return #{id}_type if it is polymorphic""" belongs = BelongsTo(self.klass, "bar", polymorphic=True) self.assertEqual("bar_type", belongs.polymorphic_type()) # scope tests def test_scope_foreign_key(self): """should return None if no foreign_key present""" record = self.article({}) self.assertEqual(None, self.article.defined_associations["site"].scope(record)) def test_scope_for_source_class(self): """should return a scope for the source class if association present""" record = self.article({"site_id": 1}) self.assertTrue(self.article.defined_associations["site"].scope(record).__class__ is Relation) def test_scope_options(self): """should the scope should have the options for the association query""" record = self.article({"site_id": 1}) self.assertEqual({"id": 1}, self.article.defined_associations["site"].scope(record).params["where"][0]) def test_association_with_records(self): """ should use an array of primary key values if the argument is an array of records """ records = [self.article({"site_id": x}) for x in [1, 2, 3]] association = self.article.defined_associations["site"] where_values = association.scope(records).query()["where"] self.assertEqual(where_values, [{"id": [1, 2, 3]}]) def test_poly_association_with_records(self): """ should raise AssociationPreloadNotSupported for polymorphic associations when the argument is an array of records """ records = [self.comment({"parent_id": x}) for x in [1, 2, 3]] association = self.comment.defined_associations["parent"] self.assertRaises(errors.AssociationPreloadNotSupported, association.scope, records)
def test_polymorphic_type(self): """should return #{id}_type if it is polymorphic""" belongs = BelongsTo(self.klass, "bar", polymorphic=True) self.assertEqual("bar_type", belongs.polymorphic_type())
class BelongsToTestCase(BaseAssociationTestCase): def setUp(self): self.klass = fixtures.association_models.Test self.id = 'belongs' self.foreign_key = 'some_id' self.belongs_to = BelongsTo(target_klass=self.klass, id=self.id) self.article = fixtures.association_models.Article self.comment = fixtures.association_models.Comment def test_type(self): """should return 'belongs_to' as the type""" self.assertEqual('belongs_to', self.belongs_to.type()) def test_collection(self): """should return False on a collection""" self.assertEqual(False, self.belongs_to.collection()) def test_foreign_key_not_set(self): """should return the association's foreign_key if one is set""" self.belongs_to.foreign_key = self.foreign_key self.assertEqual(self.foreign_key, self.belongs_to.foreign_key) def test_foreign_key_not_set_in_super(self): """sould return #{id}_id if the association doesn't have a foreign_key""" self.assertEqual('%s_id' % self.id, self.belongs_to.foreign_key) # need to test polymorphic and polymorphic_type def test_polymorphic(self): """should return true for polymorphic if polymorphic options specified and false otherwise""" self.assertFalse(self.belongs_to.polymorphic()) belongs = BelongsTo(target_klass=self.comment, id='parent', polymorphic=True) self.assertTrue(belongs.polymorphic()) comment = self.comment({ 'parent_id': 1, 'parent_type': 'Article' }) scope = belongs.scope(comment) self.assertEqual(scope.__class__, pyperry.Relation) def test_polymorphic_type(self): """should return #{id}_type if it is polymorphic""" belongs = BelongsTo(target_klass=self.klass, id='bar', polymorphic=True) self.assertEqual('bar_type', belongs.polymorphic_type()) # scope tests def test_scope_foreign_key(self): """should return None if no foreign_key present""" record = self.article({}) self.assertEqual(None, self.article.defined_associations['site'].scope(record)) def test_scope_for_source_class(self): """should return a scope for the source class if association present""" record = self.article({'site_id': 1}) self.assertTrue(self.article.defined_associations['site'].scope(record).__class__ is Relation) def test_scope_options(self): """should the scope should have the options for the association query""" record = self.article({'site_id': 1}) self.assertEqual({'id': 1}, self.article.defined_associations['site'].scope(record).params['where'][0]) def test_association_with_records(self): """ should use an array of primary key values if the argument is an array of records """ records = [self.article({'site_id': x}) for x in [1,2,3]] association = self.article.defined_associations['site'] where_values = association.scope(records).query()['where'] self.assertEqual(where_values, [{'id': [1, 2, 3]}]) def test_poly_association_with_records(self): """ should raise AssociationPreloadNotSupported for polymorphic associations when the argument is an array of records """ records = [self.comment({'parent_id': x}) for x in [1,2,3]] association = self.comment.defined_associations['parent'] self.assertRaises(errors.AssociationPreloadNotSupported, association.scope, records) def test_get_descriptor(self): """should return first object in scope""" TestAdapter.data = { 'id': 1 } class Test(fixtures.association_models.AssocTest): id = Field() foo_id = Field() assoc = BelongsTo(target_klass=Test, id='foo', klass=Test) Test.foo = assoc instance = Test(foo_id=1) self.assertEqual(Test.foo, assoc) self.assertEqual(type(instance.foo), Test) def test_set_descriptor(self): """should set the foreign_key field with value of record if set""" article = self.article(id=1) comment = self.comment() comment.parent = article self.assertEqual(comment.parent_id, 1) self.assertEqual(comment.parent_type, 'Article') def test_set_descriptor_none(self): """should set fk to None when descriptor called with None""" comment = self.comment(parent_id=1, parent_type="Article") self.assertEqual(comment.parent_id, 1) self.assertEqual(comment.parent_type, "Article") comment.parent = None self.assertEqual(comment.parent_id, None) self.assertEqual(comment.parent_type, None) def test_getter_returns_none_on_none_scope(self): comment = self.comment() self.assertEqual(comment.parent, None)
def test_eager_loadable(self): """should be eager_loadable when the moethods do not rely on instance data""" association = BelongsTo(target_klass=self.klass, id=self.id) self.assertEqual(True, association.eager_loadable())
def test_eager_loadable_poly(self): """should return false for polymorphic belongs to associations""" association = BelongsTo(target_klass=self.klass, id=self.id, polymorphic=True) self.assertEqual(association.eager_loadable(), False)
def test_belongs_to(self): """ should use the source class's primary key in a belongs to association """ association = BelongsTo(TargetModel, "whatever", class_name="SourceModel") self.assertEqual(association.primary_key(), SourceModel.primary_key())