1 from .fixtures import app_client
3 from urllib.parse import urlencode
5 pytest.fixture(scope='module')(app_client)
7 def test_homepage(app_client):
8 _, response = app_client.get('/.json')
9 assert response.status == 200
10 assert response.json.keys() == {'test_tables': 0}.keys()
11 d = response.json['test_tables']
12 assert d['name'] == 'test_tables'
13 assert d['tables_count'] == 4
15 def test_database_page(app_client):
16 response = app_client.get('/test_tables.json', gather_request=False)
18 assert 'test_tables' == data['database']
27 'foreign_keys': {'incoming': [], 'outgoing': []}
29 'name': '/group1/array2',
36 'foreign_keys': {'incoming': [], 'outgoing': []}
38 'name': '/group1/table1',
39 'columns': ['identity', 'idnumber', 'speed'],
45 'foreign_keys': {'incoming': [], 'outgoing': []}
47 'name': '/group2/table2',
48 'columns': ['identity', 'idnumber', 'speed'],
54 'foreign_keys': {'incoming': [], 'outgoing': []}
57 def test_custom_sql(app_client):
58 response = app_client.get(
59 '/test_tables.json?' + urlencode({
60 'sql': 'select identity from [/group1/table1]',
67 'sql': 'select identity from [/group1/table1]',
70 assert 1000 == len(data['rows'])
72 {'identity': 'This is particle: 0'},
73 {'identity': 'This is particle: 1'},
74 {'identity': 'This is particle: 2'},
75 {'identity': 'This is particle: 3'}
77 assert ['identity'] == data['columns']
78 assert 'test_tables' == data['database']
79 assert data['truncated']
81 def test_custom_complex_sql(app_client):
82 response = app_client.get(
83 '/test_tables.json?' + urlencode({
84 'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
91 'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
94 assert 4 == len(data['rows'])
96 {'identity': 'This is particle: 51'},
97 {'identity': 'This is particle: 52'},
98 {'identity': 'This is particle: 53'},
99 {'identity': 'This is particle: 54'}
101 assert ['identity'] == data['columns']
102 assert 'test_tables' == data['database']
103 assert not data['truncated']
105 def test_custom_pytables_sql(app_client):
106 response = app_client.get(
107 '/test_tables.json?' + urlencode({
108 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
115 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
118 assert 199 == len(data['rows'])
120 {'identity': 'This is particle: 51'},
121 {'identity': 'This is particle: 52'},
122 {'identity': 'This is particle: 53'}
123 ] == data['rows'][:3]
124 assert ['identity'] == data['columns']
125 assert 'test_tables' == data['database']
126 assert not data['truncated']
128 def test_invalid_custom_sql(app_client):
129 response = app_client.get(
130 '/test_tables.json?sql=.schema',
133 assert response.status == 400
134 assert response.json['ok'] is False
135 assert 'Statement must be a SELECT' == response.json['error']
137 def test_table_json(app_client):
138 response = app_client.get(
139 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
142 assert response.status == 200
144 assert data['query']['sql'] == 'select rowid, * from [/group2/table2] order by rowid limit 51'
145 assert data['rows'][3:6] == [{
147 'identity': 'This is particle: 3',
152 'identity': 'This is particle: 4',
157 'identity': 'This is particle: 5',
162 def test_table_not_exists_json(app_client):
165 'error': 'Table not found: blah',
169 '/test_tables/blah.json', gather_request=False
172 def test_table_shape_arrays(app_client):
173 response = app_client.get(
174 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=arrays',
178 [6, 'This is particle: 6', 6, 12.0],
179 [7, 'This is particle: 7', 7, 14.0],
180 ] == response.json['rows'][6:8]
182 def test_table_shape_objects(app_client):
183 response = app_client.get(
184 '/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',
206 'identity': 'This is particle: 6',
211 'identity': 'This is particle: 7',
214 }] == response.json[6:8]
216 def test_table_shape_invalid(app_client):
217 response = app_client.get(
218 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=invalid',
223 'error': 'Invalid _shape: invalid',
228 @pytest.mark.parametrize('path, expected_rows, expected_pages', [
229 ('/test_tables/%2Farray1.json', 2, 1),
230 ('/test_tables/%2Farray1.json?_size=1', 2, 2),
231 ('/test_tables/%2Fgroup1%2Farray2.json?_size=1000', 10000, 10),
233 def test_paginate_tables_and_arrays(app_client, path, expected_rows, expected_pages):
237 response = app_client.get(path, gather_request=False)
238 print("*****", response.json)
239 assert 200 == response.status
241 fetched.extend(response.json['rows'])
242 path = response.json['next_url']
244 assert response.json['next']
245 assert '_next={}'.format(response.json['next']) in path
247 assert expected_rows == len(fetched)
248 assert expected_pages == count