forked from limodou/uliweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generic.py
More file actions
105 lines (97 loc) · 3.24 KB
/
test_generic.py
File metadata and controls
105 lines (97 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#coding=utf-8
import time, sys
sys.path.insert(0, '../uliweb/lib')
from uliweb.orm import *
import uliweb
import uliweb.orm
uliweb.orm.__auto_create__ = True
uliweb.orm.__nullable__ = True
uliweb.orm.__server_default__ = False
import mock
from uliweb.utils.generic import QueryView, ListView
class Request(object):
values = {'page':1, 'rows':10, 'limit':10}
GET = {}
def __init__(self):
self.query_string = ''
self.path = ''
class Functions(object):
def get_model(self, name):
return get_model(name)
QueryView = QueryView
ListView = ListView
functions = mock.Mock(return_value=Functions())
uliweb.functions = functions()
from uliweb.contrib.generic import MultiView
def test_1():
"""
>>> db = get_connection('sqlite://')
>>> db.metadata.drop_all()
>>> class Test(Model):
... username = Field(unicode)
... year = Field(int, default=30)
... birth = Field(datetime.date)
>>> a = Test(username='limodou', birth='2011-03-04')
>>> a.save()
True
>>> a = Test(username='limodou', birth='2011-04-04')
>>> a.save()
True
>>> from uliweb.utils.generic import ListView
>>> request = mock.Mock(return_value=Request())
>>> uliweb.request = request()
>>> view = ListView(Test, group_by=Test.c.username)
>>> query = view.query()
>>> print str(query.get_query()).replace('\\n', ' ')
SELECT test.username, test.year, test.birth, test.id FROM test WHERE 1 = 1 GROUP BY test.username LIMIT ? OFFSET ?
>>> # set_echo(True)
>>> print query.count()
1
"""
def test_multi_view_basic():
"""
>>> db = get_connection('sqlite://')
>>> db.metadata.drop_all()
>>> class Test(Model):
... username = Field(unicode)
... year = Field(int, default=30)
... birth = Field(datetime.date)
>>> a = Test(username='limodou', birth='2011-03-04')
>>> a.save()
True
>>> class MyView(MultiView):
... _model = 'test'
>>> view = MyView()
>>> fields = [
... {'name':'username'}
... ]
>>> c = {'username':'guest'}
>>> print str(view._get_query_condition(Test, fields=fields, values=c))
test.username = :username_1
>>> fields = [
... {'name':'username', 'like':'%_%'},
... {'name':'year', 'op':'ge'}
... ]
>>> print rawsql(view._get_query_condition(Test, fields=fields, values=c))
test.username LIKE '%guest%'
>>> c = {'username':'guest', 'year':30}
>>> print rawsql(view._get_query_condition(Test, fields=fields, values=c))
test.year >= 30 AND test.username LIKE '%guest%'
"""
def test_multi_view_basic():
"""
>>> db = get_connection('sqlite://')
>>> db.metadata.drop_all()
>>> class Test(Model):
... username = Field(unicode)
... year = Field(int, default=30)
... birth = Field(datetime.date)
>>> a = Test(username='limodou', birth='2011-03-04')
>>> a.save()
True
>>> class MyView(MultiView):
... _model = 'test'
>>> view = MyView()
>>> view._list() # doctest:+ELLIPSIS
{'page_rows': 10, 'query_form': <uliweb.utils.generic.DummyForm object at ...>, 'limit': 10, 'pageno': 1, 'table': <uliweb.utils.generic.ListView object at ...>, 'total': 0, 'table_id': 'listview_table', 'page': 1}
"""