]> git.jsancho.org Git - datasette-pytables.git/blob - tests/test_api.py
c9ef73ed35047a60b54df97b424684caf443c68f
[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']
75
76 def test_invalid_custom_sql(app_client):
77     response = app_client.get(
78         '/test_tables.json?sql=.schema',
79         gather_request=False
80     )
81     assert response.status == 400
82     assert response.json['ok'] is False
83     assert 'Statement must be a SELECT' == response.json['error']
84
85 def test_table_json(app_client):
86     response = app_client.get(
87         '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
88         gather_request=False
89     )
90     assert response.status == 200
91     data = response.json
92     assert data['query']['sql'] == 'select rowid, * from [/group2/table2] order by rowid limit 51'
93     assert data['rows'][3:6] == [{
94         'rowid': 3,
95         'identity': 'This is particle:  3',
96         'idnumber': 3,
97         'speed': 6.0
98     }, {
99         'rowid': 4,
100         'identity': 'This is particle:  4',
101         'idnumber': 4,
102         'speed': 8.0
103     }, {
104         'rowid': 5,
105         'identity': 'This is particle:  5',
106         'idnumber': 5,
107         'speed': 10.0
108     }]
109
110 def test_table_not_exists_json(app_client):
111     assert {
112         'ok': False,
113         'error': 'Table not found: blah',
114         'status': 404,
115         'title': None,
116     } == app_client.get(
117         '/test_tables/blah.json', gather_request=False
118     ).json
119
120 def test_table_shape_arrays(app_client):
121     response = app_client.get(
122         '/test_tables/%2Fgroup2%2Ftable2.json?_shape=arrays',
123         gather_request=False
124     )
125     assert [
126         [6, 'This is particle:  6', 6, 12.0],
127         [7, 'This is particle:  7', 7, 14.0],
128     ] == response.json['rows'][6:8]
129
130 def test_table_shape_objects(app_client):
131     response = app_client.get(
132         '/test_tables/%2Fgroup2%2Ftable2.json?_shape=objects',
133         gather_request=False
134     )
135     assert [{
136         'rowid': 6,
137         'identity': 'This is particle:  6',
138         'idnumber': 6,
139         'speed': 12.0,
140     }, {
141         'rowid': 7,
142         'identity': 'This is particle:  7',
143         'idnumber': 7,
144         'speed': 14.0,
145     }] == response.json['rows'][6:8]
146
147 def test_table_shape_array(app_client):
148     response = app_client.get(
149         '/test_tables/%2Fgroup2%2Ftable2.json?_shape=array',
150         gather_request=False
151     )
152     assert [{
153         'rowid': 6,
154         'identity': 'This is particle:  6',
155         'idnumber': 6,
156         'speed': 12.0,
157     }, {
158         'rowid': 7,
159         'identity': 'This is particle:  7',
160         'idnumber': 7,
161         'speed': 14.0,
162     }] == response.json[6:8]
163
164 def test_table_shape_invalid(app_client):
165     response = app_client.get(
166         '/test_tables/%2Fgroup2%2Ftable2.json?_shape=invalid',
167         gather_request=False
168     )
169     assert {
170         'ok': False,
171         'error': 'Invalid _shape: invalid',
172         'status': 400,
173         'title': None,
174     } == response.json