Skip to content
Merged

Sync #40

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix AttributeError; add 6 test cases
  • Loading branch information
zhangchunlin committed Oct 10, 2019
commit d960506336cd332aa1835be0ac117dc5a9d16a2e
114 changes: 114 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,120 @@ def test_apijson_get():
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}}]}

>>> #query array with some filter column
>>> data ='''{
... "[]":{
... "@count":4,
... "@page":0,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id-",
... "@role":"ADMIN",
... "username":"admin"
... }
... }
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'id': 1}}]}

>>> #query array with reference, @query = 1
>>> data ='''{
... "[]":{
... "@count":2,
... "@page":0,
... "@query":1,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id-",
... "@role":"ADMIN"
... }
... },
... "total@":"/[]/total"
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', 'total': 4}

>>> #query array with reference, @query = 2
>>> data ='''{
... "[]":{
... "@count":2,
... "@page":0,
... "@query":2,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id-",
... "@role":"ADMIN"
... }
... },
... "total@":"/[]/total"
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'userc', 'nickname': 'User C', 'email': 'userc@localhost', 'id': 4}}, {'user': {'username': 'userb', 'nickname': 'User B', 'email': 'userb@localhost', 'id': 3}}], 'total': 4}

>>> #query array with @order +
>>> data ='''{
... "[]":{
... "@count":2,
... "@page":0,
... "@query":2,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id+",
... "@role":"ADMIN"
... }
... },
... "total@":"/[]/total"
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'id': 1}}, {'user': {'username': 'usera', 'nickname': 'User A', 'email': 'usera@localhost', 'id': 2}}], 'total': 4}

>>> #query array with @order having a non existing column
>>> data ='''{
... "[]":{
... "@count":2,
... "@page":0,
... "@query":2,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"nonexist+",
... "@role":"ADMIN"
... }
... },
... "total@":"/[]/total"
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 400, 'msg': "'user' doesn't have column 'nonexist'"}

>>> #query array with @expr
>>> data ='''{
... "[]":{
... "@count":4,
... "@page":0,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id-",
... "@role":"ADMIN",
... "@expr":["username$","|","nickname$"],
... "username$":"%b%",
... "nickname$":"%c%"
... }
... }
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'userc', 'nickname': 'User C', 'email': 'userc@localhost', 'id': 4}}, {'user': {'username': 'userb', 'nickname': 'User B', 'email': 'userb@localhost', 'id': 3}}]}

>>> #Association query: Two tables, one to one,ref path is absolute path
>>> data ='''{
... "moment":{},
Expand Down
5 changes: 4 additions & 1 deletion uliweb_apijson/apijson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ def query_array(self):
else:
sort_key = k
sort_order = "asc"
column = getattr(self.model.c,sort_key)
try:
column = getattr(self.model.c,sort_key)
except AttributeError as e:
raise UliwebError("'%s' doesn't have column '%s'"%(self.name,sort_key))
q = q.order_by(getattr(column,sort_order)())
l = [self._get_info(i,True) for i in q]
self.parent.rdict[self.key] = l
Expand Down