Skip to content

Commit d960506

Browse files
committed
fix AttributeError; add 6 test cases
1 parent 85534ca commit d960506

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

Diff for: tests/test.py

+114
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,120 @@ def test_apijson_get():
453453
>>> print(d)
454454
{'code': 200, 'msg': 'success', '[]': [{'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}}]}
455455
456+
>>> #query array with some filter column
457+
>>> data ='''{
458+
... "[]":{
459+
... "@count":4,
460+
... "@page":0,
461+
... "user":{
462+
... "@column":"id,username,nickname,email",
463+
... "@order":"id-",
464+
... "@role":"ADMIN",
465+
... "username":"admin"
466+
... }
467+
... }
468+
... }'''
469+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
470+
>>> d = json_loads(r.data)
471+
>>> print(d)
472+
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'id': 1}}]}
473+
474+
>>> #query array with reference, @query = 1
475+
>>> data ='''{
476+
... "[]":{
477+
... "@count":2,
478+
... "@page":0,
479+
... "@query":1,
480+
... "user":{
481+
... "@column":"id,username,nickname,email",
482+
... "@order":"id-",
483+
... "@role":"ADMIN"
484+
... }
485+
... },
486+
... "total@":"/[]/total"
487+
... }'''
488+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
489+
>>> d = json_loads(r.data)
490+
>>> print(d)
491+
{'code': 200, 'msg': 'success', 'total': 4}
492+
493+
>>> #query array with reference, @query = 2
494+
>>> data ='''{
495+
... "[]":{
496+
... "@count":2,
497+
... "@page":0,
498+
... "@query":2,
499+
... "user":{
500+
... "@column":"id,username,nickname,email",
501+
... "@order":"id-",
502+
... "@role":"ADMIN"
503+
... }
504+
... },
505+
... "total@":"/[]/total"
506+
... }'''
507+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
508+
>>> d = json_loads(r.data)
509+
>>> print(d)
510+
{'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}
511+
512+
>>> #query array with @order +
513+
>>> data ='''{
514+
... "[]":{
515+
... "@count":2,
516+
... "@page":0,
517+
... "@query":2,
518+
... "user":{
519+
... "@column":"id,username,nickname,email",
520+
... "@order":"id+",
521+
... "@role":"ADMIN"
522+
... }
523+
... },
524+
... "total@":"/[]/total"
525+
... }'''
526+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
527+
>>> d = json_loads(r.data)
528+
>>> print(d)
529+
{'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}
530+
531+
>>> #query array with @order having a non existing column
532+
>>> data ='''{
533+
... "[]":{
534+
... "@count":2,
535+
... "@page":0,
536+
... "@query":2,
537+
... "user":{
538+
... "@column":"id,username,nickname,email",
539+
... "@order":"nonexist+",
540+
... "@role":"ADMIN"
541+
... }
542+
... },
543+
... "total@":"/[]/total"
544+
... }'''
545+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
546+
>>> d = json_loads(r.data)
547+
>>> print(d)
548+
{'code': 400, 'msg': "'user' doesn't have column 'nonexist'"}
549+
550+
>>> #query array with @expr
551+
>>> data ='''{
552+
... "[]":{
553+
... "@count":4,
554+
... "@page":0,
555+
... "user":{
556+
... "@column":"id,username,nickname,email",
557+
... "@order":"id-",
558+
... "@role":"ADMIN",
559+
... "@expr":["username$","|","nickname$"],
560+
... "username$":"%b%",
561+
... "nickname$":"%c%"
562+
... }
563+
... }
564+
... }'''
565+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
566+
>>> d = json_loads(r.data)
567+
>>> print(d)
568+
{'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}}]}
569+
456570
>>> #Association query: Two tables, one to one,ref path is absolute path
457571
>>> data ='''{
458572
... "moment":{},

Diff for: uliweb_apijson/apijson/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ def query_array(self):
231231
else:
232232
sort_key = k
233233
sort_order = "asc"
234-
column = getattr(self.model.c,sort_key)
234+
try:
235+
column = getattr(self.model.c,sort_key)
236+
except AttributeError as e:
237+
raise UliwebError("'%s' doesn't have column '%s'"%(self.name,sort_key))
235238
q = q.order_by(getattr(column,sort_order)())
236239
l = [self._get_info(i,True) for i in q]
237240
self.parent.rdict[self.key] = l

0 commit comments

Comments
 (0)