Example #1
0
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)
Example #2
0
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)