def setup_class(cls): cls.ta = TestApp(ag.wsgi_test_app) Car.delete_all() Car.add(**{ 'make': u'chevy', 'model': u'cav', 'year': 2010 })
def test_get_by_and_where(): Car.delete_all() Car.add(**{'make': u'chevy', 'model': u'astro', 'year': 1993}) c = Car.get_by(make=u'chevy', model=u'astro', year=1993) assert c.model == 'astro' c = Car.get_where(Car.make == u'chevy', Car.year < 2000) assert c.model == 'astro' c = Car.get_where(Car.make == u'chevy', Car.year > 2000) assert c is None
def test_count_and_delete_all(): Car.delete_all() Car.add(**{'make': u'test', 'model': u'count', 'year': 2010}) Car.add(**{'make': u'test', 'model': u'count', 'year': 2009}) Car.add(**{'make': u'test', 'model': u'count2', 'year': 2010}) assert Car.count() == 3 assert Car.count_by(model=u'count') == 2 assert Car.count_where(Car.model == u'count') == 2 eq_(Car.delete_all(), 3)
def test_declarative_stuff(): c = Car.add(make=u'ford', model=u'windstar', year=u'1998') cd = c.to_dict() assert cd['make'] == u'ford' assert cd['model'] == u'windstar' assert cd['year'] == 1998 assert cd['createdts'] is not None assert cd['id'] > 0 assert cd['updatedts'] is None c.year = 1999 db.sess.commit() assert c.updatedts is not None
def test_delete_where(): Car.delete_all() Car.add(**{'make': u'test', 'model': u'count', 'year': 2010}) Car.add(**{'make': u'test', 'model': u'count', 'year': 2009}) Car.add(**{'make': u'test', 'model': u'count2', 'year': 2010}) # two clauses assert Car.delete_where(Car.model == u'count', Car.year == 2009) == 1 assert Car.count() == 2 # one clause assert Car.delete_where(Car.model == u'count2') == 1
def test_edit(): Car.delete_all() c1 = Car.add(**{'make': u'test', 'model': u'count', 'year': 2008}) cid = c1.id Car.edit(c1.id, make=u'ford', year=2010) db.sess.remove() c = Car.first() assert c.make == 'ford' assert c.model == 'count' assert c.year == 2010 c1 = Car.edit(year=2011, id=cid) assert c.make == 'ford' assert c.model == 'count' assert c.year == 2011 try: c1 = Car.edit(year=2011) assert False except ValueError: pass
def test_query_attribute(): Car.delete_all() c = Car.add(**{'make': u'test', 'model': u'count', 'year': 2010}) assert Car.query().count() == 1 # default usage should return all attributes c = Car.query().first() assert isinstance(c, Car) assert c.id assert c.make assert c.model # sending attribute names should give us a recordset with just those # attributes res = Car.query('id', 'make').first() assert not isinstance(res, Car) assert res.id assert res.make try: assert res.model assert False, 'expected exception' except AttributeError: pass
def default(self): # need to do something with the DB so that our Session gets removed # and any instances bound to it can not refresh Car.add(**{'make': u'ford', 'model': u'taurus', 'year': 2010}) return 'Index Page'
def test_lists_pairs_firsts(): Car.delete_all() c1 = Car.add(**{'make': u'test', 'model': u'count', 'year': 2008}) c2 = Car.add(**{'make': u'test', 'model': u'count', 'year': 2009}) c3 = Car.add(**{'make': u'test', 'model': u'count2', 'year': 2010}) result = Car.list() assert len(result) == 3 assert result[2] is c3 result = Car.list_by(model=u'count2') assert len(result) == 1 assert result[0] is c3 result = Car.list_where(Car.model == u'count2') assert len(result) == 1 assert result[0] is c3 # with order_by clauses result = Car.list(order_by=Car.year.desc()) assert result[2] is c1 # multiple values for order_by result = Car.list(order_by=(Car.model, Car.year.desc())) assert result[0] is c2, result # with order by result = Car.list_by(model=u'count', order_by=Car.year.desc()) assert result[0] is c2 # with order by result = Car.list_where(Car.model == u'count', order_by=Car.year.desc()) assert result[0] is c2 # with extra arg try: Car.list_where(Car.model == u'count', order_by=Car.year.desc(), erroneous='foo') assert False except ValueError: pass ### # test pairs ### expect = [ (c1.id, c1.year), (c2.id, c2.year), (c3.id, c3.year), ] result = Car.pairs('id:year') eq_(expect, result) expect = [ (c1.model, c1.year), (c2.model, c2.year), (c3.model, c3.year), ] result = Car.pairs('model:year') eq_(expect, result) expect = [ (c3.model, c3.year), (c2.model, c2.year), (c1.model, c1.year), ] result = Car.pairs('model:year', order_by=Car.year.desc()) eq_(expect, result) expect = [ (c2.model, c2.year), (c1.model, c1.year), ] result = Car.pairs_by('model:year', model=u'count', order_by=Car.year.desc()) eq_(expect, result) result = Car.pairs_where('model:year', Car.model == u'count', order_by=Car.year.desc()) eq_(expect, result) result = Car.pairs_where('model:year', Car.model == u'we-need-an-empty-list', order_by=Car.year.desc()) eq_([], result) ### # test firsts ### c = Car.first() assert c is c1 c = Car.first(order_by=Car.year.desc()) assert c is c3 c = Car.first_by(model=u'count2') assert c is c3 c = Car.first_by(model=u'count', order_by=Car.year.desc()) assert c is c2 c = Car.first_where(Car.model == u'count2') assert c is c3 c = Car.first_where(Car.model == u'count', order_by=Car.year.desc()) assert c is c2 c = Car.first_by(model=u'nothere') assert c is None try: c = Car.first_where(Car.model == u'count2', erronous='foo') except ValueError: pass
def test_delete(): c = Car.add(**{'make': u'chevy', 'model': u'astro', 'year': 1993}) cid = c.id assert Car.delete(cid) assert not Car.delete(cid)