Example #1
0
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()
Example #2
0
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')
Example #3
0
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')
Example #4
0
 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())
Example #5
0
 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
Example #6
0
class DirModel(pyperry.Base):
    id = Field()
    foo = Field()
    bar = Field()

    owner = BelongsTo()
    children = HasMany()
Example #7
0
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')
Example #8
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')
Example #9
0
 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)
Example #10
0
 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())
Example #11
0
    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)
Example #12
0
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')
Example #13
0
 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)
Example #14
0
 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())
Example #15
0
 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())
Example #16
0
class AssociationModel(pyperry.Base):
    you = BelongsTo()
    foo = BelongsTo(polymorphic=True)
    bar = HasOne()
    bizs = HasMany()
    bazs = HasMany(through='bizs')