Exemplo n.º 1
0
    def test_all_from_db_of_has_many_relation(self, Criteria):
        class Phone(ActiveRecord):
            __columns__ = ['id', 'created_at', 'updated_at', 'user_id']
            __dbmanager__ = fudge.Fake('dbmanager').provides('get_connection').returns(None)
 
        class User(ActiveRecord):
            __columns__ = ['id', 'name', 'created_at', 'updated_at']
            has_many(Phone)
 
        u = User(id=10, name='py', age=35)
        u._ActiveRecord__is_persisted = True
        
        Criteria.is_callable().returns_fake()\
                .expects('set_record_class').with_args(Phone).returns_fake()\
                .expects('where').with_args(user_id=10).returns_fake()\
                .expects('all').returns([
                     Phone(id=1, created_at=datetime.now(), updated_at=datetime.now(), user_id=10),
                     Phone(id=2, created_at=datetime.now(), updated_at=datetime.now(), user_id=10),
                     Phone(id=3, created_at=datetime.now(), updated_at=datetime.now(), user_id=10),
                 ])
        self.assertTrue(isinstance(u.phones, Collection))
        phones = u.phones.all()
        self.assertEqual(3, len(phones))
        self.assertEqual(1, phones[0].id)
        self.assertEqual(2, phones[1].id)
        self.assertEqual(3, phones[2].id)
        self.assertEqual(phones, u.phones._cache)
Exemplo n.º 2
0
 def test_last_return_last_results(self):
     results = [
         {'id': 20, 'tag': 'foo'},
     ]
     conn = fudge.Fake('conn')\
             .expects('fetchall')\
             .with_args('SELECT * FROM `users` WHERE `users`.`tag` IN (?, ?) LIMIT 1 OFFSET 19', 'boom', 'foo')\
             .returns(results)
     criteria = Criteria(conn)
     criteria.count = lambda column='*': 20
     criteria.select('*').from_('users').where(tag=['boom', 'foo'])
     self.assertEqual(results[0], criteria.last())
Exemplo n.º 3
0
    def test_max_from_db_of_has_many_relation(self, Criteria):
        class Phone(ActiveRecord):
            __columns__ = ['id', 'created_at', 'updated_at', 'user_id']
            __dbmanager__ = fudge.Fake('dbmanager').provides('get_connection').returns(None)
 
        class User(ActiveRecord):
            __columns__ = ['id', 'name', 'created_at', 'updated_at']
            has_many(Phone)
 
        u = User(id=10, name='py', age=35)
        u._ActiveRecord__is_persisted = True
        
        Criteria.is_callable().returns_fake()\
                .expects('set_record_class').with_args(Phone).returns_fake()\
                .expects('where').with_args(user_id=10).returns_fake()\
                .expects('max').with_args('age').returns(37)
        self.assertEqual(37, u.phones.max('age'))