def test_max(self): c1 = Post.select('id').max() self.assertEqual(c1, 5) c2 = Post.select('id').where('id < 3').max() self.assertEqual(c2, 2) c3 = Post.select('id').where('id > 10').max() self.assertEqual(c3, None)
def test_like(self): posts = Post.select().where('content').like("test%").all() self.assertEqual([p.id for p in Post.select().all()], [i.id for i in posts]) posts = Post.select().where('id').like("1").all() self.assertEqual([Post.get(id=1).id], [p.id for p in posts]) posts = Post.select().where('content').like('%est%').all() self.assertEqual([p.id for p in Post.select().all()], [i.id for i in posts])
def test_select(self): p1 = Post.select('*').where(id=2).all() self.assertEqual(len(p1), 1) self.assertEqual(p1[0].id, 2) p2 = Post.select('id', 'content').where(id=2).all() self.assertEqual(len(p1), 1) self.assertEqual(p2[0].id, 2) self.assertEqual(p2[0].content, 'test content 2') p3 = Post.select().where("id < 5").all() self.assertEqual(len(p3), 4) self.assertEqual([1, 2, 3, 4], [i.id for i in p3]) p4 = Post.select().first() self.assertEqual(p4.id, 1)
def test_sum(self): c1 = Post.select('id').sum() self.assertEqual(c1, 15)
def test_avg(self): c1 = Post.select('id').avg() self.assertEqual(c1, 3)
def test_min(self): c1 = Post.select('id').min() self.assertEqual(c1, 1)
def test_count(self): c1 = Post.select().count() self.assertEqual(c1, 5) c2 = Post.select().where('id>3').count() self.assertEqual(c2, 2)
def test_orderby(self): posts = Post.select().orderby('id', 'asc').all() self.assertEqual([p.id for p in posts], [1, 2, 3, 4, 5]) posts = Post.select().orderby('id', 'desc').all() self.assertEqual([p.id for p in posts], [5, 4, 3, 2, 1])