def test_sqlgrid(db, rf): usr = User.objects.create_user('username', '*****@*****.**', 'password', first_name=u'hello', last_name=u'world') class MySqlGrid(SqlGrid): def get_sql_query(self, request, *args, **kwargs): return SelectStmt( select="username, email, date_joined", from_="auth_user", where="1=1", order_by="username", limit=100, offset=0 ) view = MySqlGrid.as_view() r = view(rf.post('!get-records')) print("R:", r) assert r.status_code == 200 val = dict(jason.loads(r.content.decode('u8'))) print(jason.dumps(val)) rows = [dict(r) for r in val['rows']] print("ROWS:", rows) assert len(rows) == 1 row = rows[0] assert row['k'] == 0 assert row['c'] == [ 'username', '*****@*****.**', '@datetime:' + usr.date_joined.isoformat(), ] assert len(val['cols']) == 3 assert val['cols'][0]['name'] == 'username' assert val['cols'][0]['type'] == 'varchar'
def _params(self, request): """Extract parameters from kw (request.GET or something similar). """ kw = request.json if not kw: kw = jason.loads(request.GET.get('state', "{}")) sort_params = kw.get(Resultset.SORT, getattr(self, 'default_sort', '')) def direction(p): if p.startswith('-'): return 'desc' return 'asc' def field(p): if p.startswith('-'): return p[1:] return p sort_params = [(direction(p), field(p)) for p in sort_params.split(',')] res = dict( sort=sort_params, search=kw.get(Resultset.SEARCH, ''), filter=kw.get(Resultset.FILTER, {}), start=int(kw.get(Resultset.START, '0')), end=kw.get(Resultset.END), ) if res['end']: res['end'] = int(res['end']) # print "PARAMS:", res return res
def test_loads(): val = '{"k":"@datetime:1970-05-02T06:10:00"}' jval = jason.loads(val) assert jval['k'] == datetime.datetime(1970, 5, 2, 6, 10) val = '{"k":"@date:1970-05-02"}' jval = jason.loads(val) assert jval['k'] == datetime.date(1970, 5, 2) val = u'{"k":"@døte:1970-05-02"}' jval = jason.loads(val) assert not isinstance(jval['k'], datetime.date) val = u'{"k":"@date1970-05-02"}' jval = jason.loads(val) assert not isinstance(jval['k'], datetime.date) val = u'{"k":42}' jval = jason.loads(val) assert jval['k'] == 42
def test_modelgrid(db, rf): class MG(ModelGrid): model = User columns = ['username'] view = MG.as_view() r = view(rf.get('!get-records')) assert r.status_code == 200 val = dict(jason.loads(r.content.decode('u8'))) print(jason.dumps(val)) assert val['rows'] == [] assert len(val['cols']) == 1 assert val['cols'][0]['name'] == 'username' assert val['cols'][0]['type'] == 'Char'
def _load_json(self, request): """Loads any received json data into `request.json`. """ if request.META.get('CONTENT_TYPE', '').startswith('application/json'): body = request.body request.json = jason.loads(body.decode('u8')) else: request.json = {} params = {} params.update(request.GET) params.update(request.POST) for k, v in params.items(): if isinstance(v, list) and len(v) == 1: params[k] = v[0] # print("dkdj.views:PARAMS:", params) request.dkargs = params
def test_modelgrid_sort(db, rf): class MG(ModelGrid): model = User columns = 'username email date_joined'.split() def get_queryset(self, request, *args, **kwargs): qs = super(MG, self).get_queryset(request, *args, **kwargs) return qs def get_columns(self, request, qs, object_list): columns = super(MG, self).get_columns(request, qs, object_list) @columns.append class full_name(Column): def value(self, item): return item.get_full_name() return columns usr = User.objects.create_user('username', '*****@*****.**', 'password', first_name=u'hello', last_name=u'world') view = MG.as_view() r = view(rf.post('!get-records', {'s': '-username,email'})) assert r.status_code == 200 val = dict(jason.loads(r.content.decode('u8'))) print(jason.dumps(val)) rows = [dict(r) for r in val['rows']] print("ROWS:", rows) assert len(rows) == 1 row = rows[0] assert row['k'] == 1 assert row['c'] == [ 'username', '*****@*****.**', '@datetime:' + usr.date_joined.isoformat(), u'hello world' ] assert len(val['cols']) == 4 assert val['cols'][0]['name'] == 'username' assert val['cols'][0]['type'] == 'Char' assert val['cols'][3]['name'] == 'full_name'
def test_children(): """ `-- a (root) `-- b |-- c `-- d """ d = Section(id=4, name='d') c = Section(id=3, name='c') b = Section(id=2, name='b', children=[c, d]) root = Section(id=1, name='a', children=[b]) sections = [root, b, c, d] ds = TreeDatasource() with ds: ds += sections # for node in ds.cache.values(): # print "NODE:", node assert ds.validate() # print jason.dumps(ds) assert jason.loads(jason.dumps(ds)) == treeds_result
def test_path(): """ `-- a (root) `-- b |-- c `-- d """ root = Section(id=1, name='a', path=[1]) b = Section(id=2, name='b', path=[1, 2]) c = Section(id=3, name='c', path=[1, 2, 3]) d = Section(id=4, name='d', path=[1, 2, 4]) sections = [root, b, c, d] ds = TreeDatasource() with ds: ds += sections # for node in ds.cache.values(): # print "NODE:", node assert ds.validate() # print jason.dumps(ds) assert jason.loads(jason.dumps(ds)) == treeds_result