]> git.jsancho.org Git - datasette-pytables.git/blob - tests/test_api.py
Truncate query results according to page_size limit
[datasette-pytables.git] / tests / test_api.py
1 from .fixtures import app_client
2 import pytest
3
4 pytest.fixture(scope='module')(app_client)
5
6 def test_homepage(app_client):
7     _, response = app_client.get('/.json')
8     assert response.status == 200
9     assert response.json.keys() == {'test_tables': 0}.keys()
10     d = response.json['test_tables']
11     assert d['name'] == 'test_tables'
12     assert d['tables_count'] == 4
13
14 def test_database_page(app_client):
15     response = app_client.get('/test_tables.json', gather_request=False)
16     data = response.json
17     assert 'test_tables' == data['database']
18     assert [{
19         'name': '/array1',
20         'columns': [],
21         'primary_keys': [],
22         'count': 2,
23         'label_column': None,
24         'hidden': False,
25         'fts_table': None,
26         'foreign_keys': {'incoming': [], 'outgoing': []}
27     }, {
28         'name': '/group1/array2',
29         'columns': [],
30         'primary_keys': [],
31         'count': 10000,
32         'label_column': None,
33         'hidden': False,
34         'fts_table': None,
35         'foreign_keys': {'incoming': [], 'outgoing': []}
36     }, {
37         'name': '/group1/table1',
38         'columns': ['identity', 'idnumber', 'speed'],
39         'primary_keys': [],
40         'count': 10000,
41         'label_column': None,
42         'hidden': False,
43         'fts_table': None,
44         'foreign_keys': {'incoming': [], 'outgoing': []}
45     }, {
46         'name': '/group2/table2',
47         'columns': ['identity', 'idnumber', 'speed'],
48         'primary_keys': [],
49         'count': 10000,
50         'label_column': None,
51         'hidden': False,
52         'fts_table': None,
53         'foreign_keys': {'incoming': [], 'outgoing': []}
54     }] == data['tables']
55
56 def test_custom_sql(app_client):
57     response = app_client.get(
58         '/test_tables.json?sql=select+identity+from+[/group1/table1]&_shape=objects',
59         gather_request=False
60     )
61     data = response.json
62     assert {
63         'sql': 'select identity from [/group1/table1]',
64         'params': {}
65     } == data['query']
66     assert 50 == len(data['rows'])
67     assert [
68         {'identity': 'This is particle:  0'},
69         {'identity': 'This is particle:  1'},
70         {'identity': 'This is particle:  2'}
71     ] == data['rows'][:3]
72     assert ['identity'] == data['columns']
73     assert 'test_tables' == data['database']
74     assert data['truncated']