]> git.jsancho.org Git - datasette-connectors.git/blob - tests/test_api.py
Adapting project to last datasette version (WIP)
[datasette-connectors.git] / tests / test_api.py
1 from .fixtures import app_client
2 from urllib.parse import urlencode
3
4 def test_homepage(app_client):
5     response = app_client.get('/.json')
6     assert response.status == 200
7     assert response.json.keys() == {'dummy_tables': 0}.keys()
8     d = response.json['dummy_tables']
9     assert d['name'] == 'dummy_tables'
10     assert d['tables_count'] == 2
11
12 def test_database_page(app_client):
13     response = app_client.get('/dummy_tables.json')
14     data = response.json
15     assert 'dummy_tables' == data['database']
16     assert len(data['tables']) == 2
17     assert data['tables'][0]['count'] == 2
18     assert data['tables'][0]['columns'] == ['c1', 'c2', 'c3']
19
20 def test_custom_sql(app_client):
21     response = app_client.get(
22         '/dummy_tables.json?' + urlencode({
23             'sql': 'select c1 from table1',
24             '_shape': 'objects'
25         }),
26     )
27     data = response.json
28     assert {
29         'sql': 'select c1 from table1',
30         'params': {}
31     } == data['query']
32     assert 2 == len(data['rows'])
33     assert [
34         {'c1': 10},
35         {'c1': 20}
36     ] == data['rows']
37     assert ['c1'] == data['columns']
38     assert 'dummy_tables' == data['database']
39     assert not data['truncated']
40
41 def test_invalid_custom_sql(app_client):
42     response = app_client.get(
43         '/dummy_tables.json?sql=.schema',
44         gather_request=False
45     )
46     assert response.status == 400
47     assert response.json['ok'] is False
48     assert 'Statement must be a SELECT' == response.json['error']
49
50 def test_table_json(app_client):
51     response = app_client.get(
52         '/dummy_tables/table2.json?_shape=objects',
53         gather_request=False
54     )
55     assert response.status == 200
56     data = response.json
57     assert data['query']['sql'] == 'select rowid, * from table2 order by rowid limit 51'
58     assert data['rows'] == [{
59         'rowid': 1,
60         'c1': 100,
61         'c2': 120,
62         'c3': 130
63     }, {
64         'rowid': 2,
65         'c1': 200,
66         'c2': 220,
67         'c3': 230
68     }]
69
70 def test_table_not_exists_json(app_client):
71     assert {
72         'ok': False,
73         'error': 'Table not found: blah',
74         'status': 404,
75         'title': None,
76     } == app_client.get(
77         '/dummy_tables/blah.json', gather_request=False
78     ).json
79
80 def test_table_shape_arrays(app_client):
81     response = app_client.get(
82         '/dummy_tables/table2.json?_shape=arrays',
83         gather_request=False
84     )
85     assert [
86         [1, 100, 120, 130],
87         [2, 200, 220, 230],
88     ] == response.json['rows']
89
90 def test_table_shape_objects(app_client):
91     response = app_client.get(
92         '/dummy_tables/table2.json?_shape=objects',
93         gather_request=False
94     )
95     assert [{
96         'rowid': 1,
97         'c1': 100,
98         'c2': 120,
99         'c3': 130,
100     }, {
101         'rowid': 2,
102         'c1': 200,
103         'c2': 220,
104         'c3': 230,
105     }] == response.json['rows']
106
107 def test_table_shape_array(app_client):
108     response = app_client.get(
109         '/dummy_tables/table2.json?_shape=array',
110         gather_request=False
111     )
112     assert [{
113         'rowid': 1,
114         'c1': 100,
115         'c2': 120,
116         'c3': 130,
117     }, {
118         'rowid': 2,
119         'c1': 200,
120         'c2': 220,
121         'c3': 230,
122     }] == response.json
123
124 def test_table_shape_invalid(app_client):
125     response = app_client.get(
126         '/dummy_tables/table2.json?_shape=invalid',
127         gather_request=False
128     )
129     assert {
130         'ok': False,
131         'error': 'Invalid _shape: invalid',
132         'status': 400,
133         'title': None,
134     } == response.json