示例#1
0
 def test_count(self):
     self.create_data(4)
     assert User.count() == 4
     query = (Post & User).select(
         fn.count(User.id).alias('count_user_id'),
         fn.count(Post.post_id).alias('count_post_id'))
     results = query.execute()
     assert results.count == 1
     func = results.one()
     assert func.count_user_id == 4
     assert func.count_post_id == 4
示例#2
0
    def test_having(self):
        query = self.models.groupby(
            User.name).having(fn.count(User.id) >= 1).select()
        results = query.execute()
        assert results.count == 4

        query = self.models.groupby(
            User.name).having(fn.count(User.id) > 4).select()
        results = query.execute()
        assert results.count == 0

        query = self.models.groupby(
            User.name).having(fn.count(User.id) == 4).select()
        results = query.execute()
        assert results.count == 4   # 16 / 4 =4
示例#3
0
    def test_having(self):
        query = self.models.groupby(
            User.name).having(fn.count(User.id) >= 1).select()
        results = query.execute()
        assert results.count == 4

        query = self.models.groupby(
            User.name).having(fn.count(User.id) > 4).select()
        results = query.execute()
        assert results.count == 0

        query = self.models.groupby(
            User.name).having(fn.count(User.id) == 4).select()
        results = query.execute()
        assert results.count == 4   # 16 / 4 =4
示例#4
0
文件: tests.py 项目: bopo/skylark
    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
示例#5
0
    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
示例#6
0
def page(page_number):
    if page_number <= 0:
        abort(404)

    n = 9

    query = Post.where(published=True).orderby(
        Post.datetime, desc=True).limit(n, offset=n * (page_number - 1)
                                        ).select()
    results = query.execute()
    count = results.count

    if count < 0:  # no posts
        abort(404)

    query = Post.where(published=True).select(fn.count(Post.id))
    result = query.execute()
    func = result.one()
    total_count = func.count

    is_first_page = True if page_number == 1 else False
    is_last_page = True if n * page_number >= total_count else False

    posts = tuple(results.all())

    page = dict(
        number=page_number,
        posts=posts,
        first=is_first_page,
        last=is_last_page
    )
    return render_public('page.html', page=page)
示例#7
0
 def test_count(self):
     self.create_data(4)
     assert User.count() == 4
     assert Post.count() == 4
     query = User.select(fn.count(User.id))
     result = query.execute()
     assert result.count == 1
     assert result.tuples()[0][0] == 4
示例#8
0
文件: tests.py 项目: bopo/skylark
 def test_count(self):
     self.create_data(4)
     assert User.count() == 4
     assert Post.count() == 4
     query = User.select(fn.count(User.id))
     result = query.execute()
     assert result.count == 1
     assert result.tuples()[0][0] == 4
示例#9
0
文件: tests.py 项目: bopo/skylark
 def test_function_alias(self):
     User.create(name='jack', email='*****@*****.**')
     User.create(name='jack', email='*****@*****.**')
     User.create(name='amy', email='*****@*****.**')
     query = User.groupby(User.name).having(sql('count') >= 2).select(
         User.name, fn.count(User.id).alias('count')
     )
     result = query.execute()
     assert result.tuples()[0][1] == 2
     assert result.one().name == 'jack'
示例#10
0
 def test_function_alias(self):
     User.create(name='jack', email='*****@*****.**')
     User.create(name='jack', email='*****@*****.**')
     User.create(name='amy', email='*****@*****.**')
     query = User.groupby(User.name).having(sql('count') >= 2).select(
         User.name, fn.count(User.id).alias('count')
     )
     result = query.execute()
     assert result.tuples()[0][1] == 2
     assert result.one().name == 'jack'
示例#11
0
    def test_groupby(self):
        for x in range(2):
            User.create(name='jack', email='*****@*****.**')
        for x in range(3):
            User.create(name='tom', email='*****@*****.**')

        query = User.groupby(User.name).orderby(sql('count')).select(
            fn.count(User.id).alias('count'), User.name)
        result = query.execute()
        assert result.tuples() == ((2, 'jack'), (3, 'tom'))
示例#12
0
文件: tests.py 项目: bopo/skylark
    def test_groupby(self):
        for x in range(2):
            User.create(name='jack', email='*****@*****.**')
        for x in range(3):
            User.create(name='tom', email='*****@*****.**')

        query = User.groupby(User.name).orderby(sql('count')).select(
            fn.count(User.id).alias('count'), User.name)
        result = query.execute()
        assert result.tuples() == (
            (2, 'jack'), (3, 'tom')
        )
示例#13
0
    def test_having(self):
        for x in range(2):
            User.create(name='jack', email='*****@*****.**')
        for x in range(3):
            User.create(name='tom', email='*****@*****.**')

        query = User.groupby(User.name).having(sql('count') > 2).select(
            fn.count(User.id).alias('count'), User.name)
        result = query.execute()
        user = result.one()
        count = result.tuples()[0][0]
        assert count == 3 and user.name == 'tom'
示例#14
0
文件: tests.py 项目: bopo/skylark
    def test_having(self):
        for x in range(2):
            User.create(name='jack', email='*****@*****.**')
        for x in range(3):
            User.create(name='tom', email='*****@*****.**')

        query = User.groupby(User.name).having(sql('count') > 2).select(
            fn.count(User.id).alias('count'), User.name)
        result = query.execute()
        user = result.one()
        count = result.tuples()[0][0]
        assert count == 3 and user.name == 'tom'
示例#15
0
    def test_having(self):
        for x in range(2):
            User.create(name='jack', email='*****@*****.**')
        for x in range(3):
            User.create(name='tom', email='*****@*****.**')

        query = User.groupby(User.name).having(sql('count') > 2).select(
            fn.count(User.id).alias('count'), User.name)
        results = query.execute()
        assert results.count == 1
        user, func = results.one()
        assert func.count == 3 and user.name == 'tom'
示例#16
0
    def test_groupby(self):
        for x in range(2):
            User.create(name='jack', email='*****@*****.**')
        for x in range(3):
            User.create(name='tom', email='*****@*****.**')

        query = User.groupby(User.name).select(fn.count(User.id), User.name)

        for user, func in query:
            if user.name == 'jack':
                assert func.count == 2
            elif user.name == 'tom':
                assert func.count == 3
示例#17
0
 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
示例#18
0
    def test_func_only(self):
        self.create_data(3, table=1)
        query = User.select(fn.count(User.id))
        results = query.execute()
        func = results.one()
        assert func.count == 3
        assert results.count == 1

        query = User.at(2).select(fn.ucase(User.name).alias('uname'))
        results = query.execute()
        func = results.one()
        assert func.uname == 'NAME2'

        query = User.at(3).select(fn.concat(User.name, ' + ', 'hello'))
        results = query.execute()
        func = results.one()
        assert func.concat == 'name3 + hello'
示例#19
0
    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},
        )
示例#20
0
from models import User
from skylark import fn

query = User.at(1).select(User.name.alias('un'))
result = query.execute()
user = result.one()
user.un  # retrieve `name` by user.un

query = User.select(fn.count(User.name))
result = query.execute()
result.tuples()[0][0]  # retrieve count result by result.tuples()
示例#21
0
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]
示例#22
0
from models import User
from skylark import fn, sql

# create data..
User.create(name='jack')
User.create(name='jack')
User.create(name='foo')

# select count(user.id) as count_id, user.name from user group by user.name having count_id >= '2'
query = User.groupby(User.name).having(
    sql('count_id') >= 2
).select(fn.count(User.id).alias('count_id'), User.name)
result = query.execute()

for row in result.tuples():
    print row[0]  # count of id
    print row[1]  # user's name
示例#23
0
 def test_fullname(self):
     assert fn.max(User.id).fullname == 'max(user.id)'
     assert fn.count(User.id).fullname == 'count(user.id)'
     assert fn.concat(User.name, 'hello') == "concat(user.name, 'hello')"
示例#24
0
 def test_name(self):
     assert fn.count(User.id).name == 'count'
     assert fn.concat(User.name, 'hello').name == 'concat'
     assert fn.ucase(User.name).alias(
         'upper_cased_name').name == 'upper_cased_name'
示例#25
0
from models import User
from skylark import fn, sql

# create data..
User.create(name='jack')
User.create(name='jack')
User.create(name='foo')

# select count(user.id) as count_id, user.name from user group by user.name having count_id >= '2'
query = User.groupby(User.name).having(sql('count_id') >= 2).select(
    fn.count(User.id).alias('count_id'), User.name)
result = query.execute()

for row in result.tuples():
    print row[0]  # count of id
    print row[1]  # user's name
示例#26
0
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