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 idnumber < 55',
66 print("*************************", data)
68 'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
71 assert 4 == len(data['rows'])
73 {'identity': 'This is particle: 51'},
74 {'identity': 'This is particle: 52'},
75 {'identity': 'This is particle: 53'},
76 {'identity': 'This is particle: 54'}
78 assert ['identity'] == data['columns']
79 assert 'test_tables' == data['database']
80 assert False == data['truncated']
82 def test_custom_pytables_sql(app_client):
83 response = app_client.get(
84 '/test_tables.json?' + urlencode({
85 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
92 'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
95 assert 199 == len(data['rows'])
97 {'identity': 'This is particle: 51'},
98 {'identity': 'This is particle: 52'},
99 {'identity': 'This is particle: 53'}
100 ] == data['rows'][:3]
101 assert ['identity'] == data['columns']
102 assert 'test_tables' == data['database']
103 assert data['truncated']
105 def test_invalid_custom_sql(app_client):
106 response = app_client.get(
107 '/test_tables.json?sql=.schema',
110 assert response.status == 400
111 assert response.json['ok'] is False
112 assert 'Statement must be a SELECT' == response.json['error']
114 def test_table_json(app_client):
115 response = app_client.get(
116 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
119 assert response.status == 200
121 assert data['query']['sql'] == 'select rowid, * from [/group2/table2] order by rowid limit 51'
122 assert data['rows'][3:6] == [{
124 'identity': 'This is particle: 3',
129 'identity': 'This is particle: 4',
134 'identity': 'This is particle: 5',
139 def test_table_not_exists_json(app_client):
142 'error': 'Table not found: blah',
146 '/test_tables/blah.json', gather_request=False
149 def test_table_shape_arrays(app_client):
150 response = app_client.get(
151 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=arrays',
155 [6, 'This is particle: 6', 6, 12.0],
156 [7, 'This is particle: 7', 7, 14.0],
157 ] == response.json['rows'][6:8]
159 def test_table_shape_objects(app_client):
160 response = app_client.get(
161 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
166 'identity': 'This is particle: 6',
171 'identity': 'This is particle: 7',
174 }] == response.json['rows'][6:8]
176 def test_table_shape_array(app_client):
177 response = app_client.get(
178 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=array',
183 'identity': 'This is particle: 6',
188 'identity': 'This is particle: 7',
191 }] == response.json[6:8]
193 def test_table_shape_invalid(app_client):
194 response = app_client.get(
195 '/test_tables/%2Fgroup2%2Ftable2.json?_shape=invalid',
200 'error': 'Invalid _shape: invalid',
205 @pytest.mark.parametrize('path, expected_rows, expected_pages', [
206 ('/test_tables/%2Farray1.json', 2, 1),
207 ('/test_tables/%2Farray1.json?_size=1', 2, 2),
208 ('/test_tables/%2Fgroup1%2Farray2.json?_size=1000', 10000, 10),
210 def test_paginate_tables_and_arrays(app_client, path, expected_rows, expected_pages):
214 response = app_client.get(path, gather_request=False)
215 print("*****", response.json)
216 assert 200 == response.status
218 fetched.extend(response.json['rows'])
219 path = response.json['next_url']
221 assert response.json['next']
222 assert '_next={}'.format(response.json['next']) in path
224 assert expected_rows == len(fetched)
225 assert expected_pages == count