def test_distinct(self): assert User.create(name='jack', email='*****@*****.**') assert User.create(name='jack', email='*****@*****.**') assert User.create(name='wangchao', email='*****@*****.**') assert User.create(name='hit9', email='*****@*****.**') query = User.select(fn.count(distinct(User.name))) result = query.execute() count = result.tuples()[0][0] assert count == 3 query = User.orderby(User.id).select(distinct(User.email)) result = query.execute() assert result.tuples() == ( ('*****@*****.**',), ('*****@*****.**',), ('*****@*****.**', ) ) emails = [user.email for user in result.all()] assert len(emails) == len(set(emails)) query = User.orderby(User.id).select(distinct(User.name, User.email)) try: result = query.execute() except Exception: # should raise an error pass else: raise Exception query = User.orderby(User.id).select(distinct(User.name), User.email) result = query.execute() assert result.count == 4
def test_distinct_nodes(self): assert User.create(name='jack', email='*****@*****.**') assert User.create(name='amy', email='*****@*****.**') assert User.create(name='amy', email='*****@*****.**') assert User.create(name='tom', email='*****@*****.**') query = User.select(distinct(User.name)) result = query.execute() users = result.all() assert [user.name for user in users] == ['jack', 'amy', 'tom']
def test_distinct(self): assert User.create(name='jack', email='*****@*****.**') assert User.create(name='jack', email='*****@*****.**') assert User.create(name='wangchao', email='*****@*****.**') assert User.create(name='hit9', email='*****@*****.**') query = User.select(fn.count(distinct(User.name))) results = query.execute() func = results.one() assert func.count == 3 query = User.orderby(User.id).select(distinct(User.email)) results = query.execute() assert tuple(results.tuples()) == ( ('*****@*****.**', ), ('*****@*****.**', ), ('*****@*****.**', ) ) query = User.orderby(User.id).select(distinct(User.name), User.id) results = query.execute() assert tuple(results.dicts()) == ( {'name': 'jack', 'id': 1}, {'name': 'jack', 'id': 2}, {'name': 'wangchao', 'id': 3}, {'name': 'hit9', 'id': 4}, )
def test_examples(self): User.create(name='jack', email='*****@*****.**') User.create(name='jack', email='*****@*****.**') User.create(name='amy', email='*****@*****.**') # distinct query = User.select(distinct(User.name)) results = query.execute() assert results.one().name == 'jack' assert results.one().name == 'amy' # groupby & orderby query = User.groupby(User.name).orderby(sql('count')).select( User.name, fn.count(User.id).alias('count')) results = query.execute() user, func = results.one() assert user.name == 'amy' and func.count == 1 user, func = results.one() assert user.name == 'jack' and func.count == 2
def test_distinct(self): query = self.models.select(distinct(User.name)) result = query.execute() assert result.count == 4
from skylark import fn, distinct from models import User # select count(distinct(user.name)) from user query = User.select(fn.count(distinct(User.name))) result = query.execute() func = result.one() return func.count
from skylark import fn, distinct from models import User # select count(distinct(user.name)) from user query = User.select(fn.count(distinct(User.name))) result = query.execute() return result.tuples()[0][0]