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] where speed > 100 and identity < 55',
67 'sql': 'select identity from [/group1/table1] where speed > 100 and identity < 55',
70 assert 4 == len(data['rows'])
72 {'identity': 'This is particle: 51'},
73 {'identity': 'This is particle: 52'},
74 {'identity': 'This is particle: 53'},
75 {'identity': 'This is particle: 54'}
77 assert ['identity'] == data['columns']
78 assert 'test_tables' == data['database']
79 assert False == data['truncated']
81 def test_custom_pytables_sql(app_client):
82 response = app_client.get(
83 '/test_tables.json?' + urlencode({
84 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
91 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
94 assert 199 == len(data['rows'])
96 {'identity': 'This is particle: 51'},
97 {'identity': 'This is particle: 52'},
98 {'identity': 'This is particle: 53'}
100 assert ['identity'] == data['columns']
101 assert 'test_tables' == data['database']
102 assert data['truncated']
104 def test_invalid_custom_sql(app_client):
105 response = app_client.get(
106 '/test_tables.json?sql=.schema',
109 assert response.status == 400
110 assert response.json['ok'] is False
111 assert 'Statement must be a SELECT' == response.json['error']
113 def test_table_json(app_client):
114 response = app_client.get(
115 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
118 assert response.status == 200
120 assert data['query']['sql'] == 'select rowid, * from [/group2/table2] order by rowid limit 51'
121 assert data['rows'][3:6] == [{
123 'identity': 'This is particle: 3',
128 'identity': 'This is particle: 4',
133 'identity': 'This is particle: 5',
138 def test_table_not_exists_json(app_client):
141 'error': 'Table not found: blah',
145 '/test_tables/blah.json', gather_request=False
148 def test_table_shape_arrays(app_client):
149 response = app_client.get(
150 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=arrays',
154 [6, 'This is particle: 6', 6, 12.0],
155 [7, 'This is particle: 7', 7, 14.0],
156 ] == response.json['rows'][6:8]
158 def test_table_shape_objects(app_client):
159 response = app_client.get(
160 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
165 'identity': 'This is particle: 6',
170 'identity': 'This is particle: 7',
173 }] == response.json['rows'][6:8]
175 def test_table_shape_array(app_client):
176 response = app_client.get(
177 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=array',
182 'identity': 'This is particle: 6',
187 'identity': 'This is particle: 7',
190 }] == response.json[6:8]
192 def test_table_shape_invalid(app_client):
193 response = app_client.get(
194 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=invalid',
199 'error': 'Invalid _shape: invalid',
204 @pytest.mark.parametrize('path, expected_rows, expected_pages', [
205 ('/test_tables/%2Farray1.json', 2, 1),
206 ('/test_tables/%2Farray1.json?_size=1', 2, 2),
207 ('/test_tables/%2Fgroup1%2Farray2.json?_size=1000', 10000, 10),
209 def test_paginate_tables_and_arrays(app_client, path, expected_rows, expected_pages):
213 response = app_client.get(path, gather_request=False)
214 print("*****", response.json)
215 assert 200 == response.status
217 fetched.extend(response.json['rows'])
218 path = response.json['next_url']
220 assert response.json['next']
221 assert '_next={}'.format(response.json['next']) in path
223 assert expected_rows == len(fetched)
224 assert expected_pages == count