1 from .fixtures import app_client
3 from urllib.parse import urlencode
5 def test_homepage(app_client):
6 response = app_client.get('/.json')
7 assert response.status == 200
8 assert response.json.keys() == {'test_tables': 0}.keys()
9 d = response.json['test_tables']
10 assert d['name'] == 'test_tables'
11 assert d['tables_count'] == 5
13 def test_database_page(app_client):
14 response = app_client.get('/test_tables.json')
16 assert 'test_tables' == data['database']
24 'foreign_keys': {'incoming': [], 'outgoing': []},
27 'name': '/group1/array2',
33 'foreign_keys': {'incoming': [], 'outgoing': []},
36 'name': '/group1/table1',
37 'columns': ['identity', 'idnumber', 'speed'],
42 'foreign_keys': {'incoming': [], 'outgoing': []},
45 'name': '/group2/multi',
51 'foreign_keys': {'incoming': [], 'outgoing': []},
54 'name': '/group2/table2',
55 'columns': ['identity', 'idnumber', 'speed'],
60 'foreign_keys': {'incoming': [], 'outgoing': []},
64 def test_custom_sql(app_client):
65 response = app_client.get(
66 '/test_tables.json?' + urlencode({
67 'sql': 'select identity from [/group1/table1]',
73 'sql': 'select identity from [/group1/table1]',
76 assert 1000 == len(data['rows'])
78 {'identity': 'This is particle: 0'},
79 {'identity': 'This is particle: 1'},
80 {'identity': 'This is particle: 2'},
81 {'identity': 'This is particle: 3'}
83 assert ['identity'] == data['columns']
84 assert 'test_tables' == data['database']
85 assert data['truncated']
87 def test_custom_complex_sql(app_client):
88 response = app_client.get(
89 '/test_tables.json?' + urlencode({
90 'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
96 'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
99 assert 4 == len(data['rows'])
101 {'identity': 'This is particle: 51'},
102 {'identity': 'This is particle: 52'},
103 {'identity': 'This is particle: 53'},
104 {'identity': 'This is particle: 54'}
106 assert ['identity'] == data['columns']
107 assert 'test_tables' == data['database']
108 assert not data['truncated']
110 def test_custom_pytables_sql(app_client):
111 response = app_client.get(
112 '/test_tables.json?' + urlencode({
113 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
119 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
122 assert 199 == len(data['rows'])
124 {'identity': 'This is particle: 51'},
125 {'identity': 'This is particle: 52'},
126 {'identity': 'This is particle: 53'}
127 ] == data['rows'][:3]
128 assert ['identity'] == data['columns']
129 assert 'test_tables' == data['database']
130 assert not data['truncated']
132 def test_invalid_custom_sql(app_client):
133 response = app_client.get(
134 '/test_tables.json?sql=.schema',
136 assert response.status == 400
137 assert response.json['ok'] is False
138 assert 'Statement must be a SELECT' == response.json['error']
140 def test_table_json(app_client):
141 response = app_client.get(
142 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
144 assert response.status == 200
146 assert data['query']['sql'] == 'select rowid, * from [/group2/table2] order by rowid limit 51'
147 assert data['rows'][3:6] == [{
149 'identity': 'This is particle: 3',
154 'identity': 'This is particle: 4',
159 'identity': 'This is particle: 5',
164 def test_table_not_exists_json(app_client):
167 'error': 'Table not found: blah',
171 '/test_tables/blah.json',
174 def test_table_shape_arrays(app_client):
175 response = app_client.get(
176 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=arrays',
179 [6, 'This is particle: 6', 6, 12.0],
180 [7, 'This is particle: 7', 7, 14.0],
181 ] == response.json['rows'][6:8]
183 def test_table_shape_objects(app_client):
184 response = app_client.get(
185 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
189 'identity': 'This is particle: 6',
194 'identity': 'This is particle: 7',
197 }] == response.json['rows'][6:8]
199 def test_table_shape_array(app_client):
200 response = app_client.get(
201 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=array',
205 'identity': 'This is particle: 6',
210 'identity': 'This is particle: 7',
213 }] == response.json[6:8]
215 def test_table_shape_invalid(app_client):
216 response = app_client.get(
217 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=invalid',
221 'error': 'Invalid _shape: invalid',
226 @pytest.mark.parametrize('path, expected_rows, expected_pages', [
227 ('/test_tables/%2Farray1.json', 2, 1),
228 ('/test_tables/%2Farray1.json?_size=1', 2, 2),
229 ('/test_tables/%2Fgroup1%2Farray2.json?_size=1000', 10000, 10),
230 ('/test_tables/%2Fgroup2%2Fmulti.json?_size=5', 10, 2),
232 def test_paginate_tables_and_arrays(app_client, path, expected_rows, expected_pages):
236 response = app_client.get(path)
237 print("*****", response.json)
238 assert 200 == response.status
240 fetched.extend(response.json['rows'])
241 path = response.json['next_url']
243 assert response.json['next']
244 assert '_next={}'.format(response.json['next']) in path
246 assert expected_rows == len(fetched)
247 assert expected_pages == count