Exemplo n.º 1
0
 def test_funcs(self):
     q1 = select(
         f for f in Foo
         if f.id < max(f.id for f in Foo)
     )
     q2 = Foo.query.filter(
         Foo.id < Foo.query(funcs.MAX(Foo.id))
     )
     self.is_same_q(q1, q2)
     q1 = select_(
         f for f in Foo
         if f.id < max(f.id for f in Foo)
     )
     q2 = Foo.query.filter(
         Foo.id < Foo.query(funcs.MAX(Foo.id))
     )
     self.is_same_q(q1, q2)
     q1 = select(
         f.id.distinct().count() for f in Foo
         if f.id < 1 and f.name == 'foo'
     )
     q2 = select(
         funcs.COUNT(funcs.DISTINCT(f.id)) for f in Foo
         if f.id < 1 and f.name == 'foo'
     )
     q3 = Foo.query(funcs.COUNT(funcs.DISTINCT(Foo.id))).filter(
         (Foo.id < 1) & (Foo.name == 'foo')
     )
     self.is_same_q(q1, q2)
     self.is_same_q(q2, q3)
Exemplo n.º 2
0
 def test_specific_entities(self):
     Dummy.create(name='foo0', age=2)
     Dummy.create(name='foo1', age=5)
     Dummy.create(name='bar0', age=3)
     Dummy.create(name='bar0', age=1, tags=['love', 'you'])
     Dummy.create(name='bar1', age=4)
     res = Dummy.query('name', 'age').order_by(Dummy.age.desc()).all()
     self.assertEqual(res, [
         ('foo1', 5),
         ('bar1', 4),
         ('bar0', 3),
         ('foo0', 2),
         ('bar0', 1),
     ])
     res = Dummy.query(Dummy.id).order_by(Dummy.age.desc()).all()
     self.assertEqual(len(res), 5)
     self.assertEqual(res[-1], 4)
     res = Dummy.query(funcs.COUNT(funcs.DISTINCT(Dummy.name))).all()
     self.assertEqual(len(res), 1)
     self.assertEqual(res[0], 4)
     res = Dummy.query('tags', 'name').filter(age=1).one()
     self.assertEqual(res[0], ['love', 'you'])
     self.assertEqual(res[1], 'bar0')
     res = Dummy.query('tags', 'name', raw=True).filter(age=1).one()
     self.assertEqual(res[0], json.dumps(['love', 'you']))
     res = Dummy.query('age').order_by(Dummy.age.desc()).all()
     self.assertEqual(res, [5, 4, 3, 2, 1])
     res = Dummy.query('age').filter(id=4).first()
     self.assertEqual(res, 1)
     res = Dummy.query('age', Dummy, Dummy.id).filter(id=4).first()
     d = Dummy.get(4)
     self.assertEqual(res, (1, d, d.id))
Exemplo n.º 3
0
    def test_func(self):
        q1 = Foo.query(funcs.COUNT(funcs.DISTINCT(
            Foo.id))).filter((Foo.id < 1) & (Foo.name == 'foo'))

        @select
        def q2():
            for f in Foo:
                if f.id < 1 and f.name == 'foo':
                    yield funcs.COUNT(funcs.DISTINCT(Foo.id))

        self.is_same_q(q1, q2)
Exemplo n.º 4
0
 def q2():
     for f in Foo:
         if f.id < 1 and f.name == 'foo':
             yield funcs.COUNT(funcs.DISTINCT(Foo.id))
Exemplo n.º 5
0
 def test_distinct(self):
     self.assertEqual(
         funcs.DISTINCT(Dummy.id).get_sql_and_params()[0], 'DISTINCT(`id`)')
Exemplo n.º 6
0
 def test_distinct(self):
     self.assertEqual(
         funcs.DISTINCT(Dummy.id).get_sql_ast(),
         ['CALL', 'DISTINCT', ['COLUMN', 'dummy', 'id']])