Esempio n. 1
0
 def setUp(self):
     self.site = fixtures.association_models.Site
     self.klass = fixtures.association_models.Test
     self.id = 'has'
     self.foreign_key = 'some_id'
     self.has = Has(target_klass=self.klass, id=self.id)
     self.article = fixtures.association_models.Article
Esempio n. 2
0
 def setUp(self):
     self.site = fixtures.association_models.Site
     self.klass = fixtures.association_models.Test
     self.id = "has"
     self.foreign_key = "some_id"
     self.has = Has(self.klass, self.id)
     self.article = fixtures.association_models.Article
Esempio n. 3
0
 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')
Esempio n. 4
0
 def test_foreign_key_option(self):
     """should use 'foreign_key' option if specified"""
     a = Has(target_klass=self.klass, id='bar', foreign_key='purple_monkey')
     self.assertEqual('purple_monkey', a.foreign_key)
Esempio n. 5
0
 def test_polymorphic(self):
     """should return set polymorphic true if 'as' option passed and false otherwise"""
     self.assertEqual(False, self.has.polymorphic())
     self.assertTrue(Has(target_klass=self.klass, id='bar',
         as_='parent').polymorphic())
Esempio n. 6
0
class HasTestCase(BaseAssociationTestCase):

    def setUp(self):
        self.site = fixtures.association_models.Site
        self.klass = fixtures.association_models.Test
        self.id = 'has'
        self.foreign_key = 'some_id'
        self.has = Has(target_klass=self.klass, id=self.id)
        self.article = fixtures.association_models.Article

    def test_primary_key(self):
        """should use 'id' as default primary key"""
        self.assertEqual('id', self.has.primary_key())

    # Need to test polymorphic stuff
    def test_polymorphic(self):
        """should return set polymorphic true if 'as' option passed and false otherwise"""
        self.assertEqual(False, self.has.polymorphic())
        self.assertTrue(Has(target_klass=self.klass, id='bar',
            as_='parent').polymorphic())

    def test_foreign_key(self):
        """should return the lowercase target class name followed by _id if association is not polymorphic"""
        articles = self.site.defined_associations['articles']
        self.assertEqual('site_id', articles.foreign_key)

    def test_foreign_key_option(self):
        """should use 'foreign_key' option if specified"""
        a = Has(target_klass=self.klass, id='bar', foreign_key='purple_monkey')
        self.assertEqual('purple_monkey', a.foreign_key)

    # 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['comments'].scope(record))

    def test_scope_for_source_class(self):
        """should return a scope for the source class if association present"""
        record = self.article({'id': 1})
        self.assertTrue(self.article.defined_associations['comments'].scope(record).__class__ is Relation)

    def test_scope_options(self):
        """should the scope should have the options for the association method"""
        record = self.article({'id': 1})
        self.assertEqual([{ 'parent_id': 1 }, { 'parent_type': 'Article' }],
            self.article.defined_associations['comments'].scope(record).params['where'])

    def test_scope_base_assoc_options(self):
        """should include base association options in the scope"""
        record = self.article({'id': 1})
        self.assertEqual("text LIKE '%awesome%'",
            self.article.defined_associations['awesome_comments'].scope(record).params['where'][0])

    def test_association_with_records(self):
        """
        should raise AssociationPreloadNotSupported for polymorphic
        associations when the argument is an array of records
        """
        records = [self.article({'id': x}) for x in [1,2,3]]
        association = self.article.defined_associations['comments']
        where_values = association.scope(records).query()['where']
        where_values.sort()
        expected = [{'parent_id': [1, 2, 3]}, {'parent_type': 'Article'}]
        self.assertEqual(where_values, expected)

    def test_poly_association_with_records(self):
        """
        should raise AssociationPreloadNotSupported when the association is not
        eager loadable
        """
        records = [self.site({'id': x}) for x in [1,2,3]]
        association = self.site.defined_associations['awesome_comments']
        self.assertRaises(errors.AssociationPreloadNotSupported,
                          association.scope, records)