]> git.jsancho.org Git - datasette-connectors.git/blob - tests/test_api.py
Overwriting Connector class is enough to operate with
[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('/dummy_tables.json?sql=.schema')
43     assert response.status == 400
44     assert response.json['ok'] is False
45     assert 'Statement must be a SELECT' == response.json['error']
46
47 def test_table_json(app_client):
48     response = app_client.get('/dummy_tables/table2.json?_shape=objects')
49     assert response.status == 200
50     data = response.json
51     assert data['query']['sql'] == 'select c1, c2, c3 from table2 limit 51'
52     assert data['rows'] == [
53         {
54             'c1': 100,
55             'c2': 120,
56             'c3': 130,
57         },
58         {
59             'c1': 200,
60             'c2': 220,
61             'c3': 230,
62         }]
63
64 def test_table_not_exists_json(app_client):
65     assert {
66         'ok': False,
67         'title': 'Invalid SQL',
68         'error': 'no such table: blah',
69         'status': 400,
70     } == app_client.get('/dummy_tables/blah.json').json
71
72 def test_table_shape_arrays(app_client):
73     response = app_client.get('/dummy_tables/table2.json?_shape=arrays')
74     assert [
75         [100, 120, 130],
76         [200, 220, 230],
77     ] == response.json['rows']
78
79 def test_table_shape_objects(app_client):
80     response = app_client.get('/dummy_tables/table2.json?_shape=objects')
81     assert [
82         {
83             'c1': 100,
84             'c2': 120,
85             'c3': 130,
86         },
87         {
88             'c1': 200,
89             'c2': 220,
90             'c3': 230,
91         },
92     ] == response.json['rows']
93
94 def test_table_shape_array(app_client):
95     response = app_client.get('/dummy_tables/table2.json?_shape=array')
96     assert [
97         {
98             'c1': 100,
99             'c2': 120,
100             'c3': 130,
101         },
102         {
103             'c1': 200,
104             'c2': 220,
105             'c3': 230,
106         },
107     ] == response.json
108
109 def test_table_shape_invalid(app_client):
110     response = app_client.get('/dummy_tables/table2.json?_shape=invalid')
111     assert {
112         'ok': False,
113         'error': 'Invalid _shape: invalid',
114         'status': 400,
115         'title': None,
116     } == response.json