X-Git-Url: https://git.jsancho.org/?p=datasette-connectors.git;a=blobdiff_plain;f=tests%2Ftest_api.py;fp=tests%2Ftest_api.py;h=e7303a83e697d7ecfa509cc3410e803110b55ffa;hp=63555cddf7dc2e62b519708939c4e03ce30773d7;hb=2bbd8101389969b749f91dea1231682773caebae;hpb=b0802bdb9d86cd65524d6ffa7afb66488d167b1e diff --git a/tests/test_api.py b/tests/test_api.py index 63555cd..e7303a8 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -2,7 +2,7 @@ from .fixtures import app_client from urllib.parse import urlencode def test_homepage(app_client): - _, response = app_client.get('/.json') + response = app_client.get('/.json') assert response.status == 200 assert response.json.keys() == {'dummy_tables': 0}.keys() d = response.json['dummy_tables'] @@ -10,28 +10,12 @@ def test_homepage(app_client): assert d['tables_count'] == 2 def test_database_page(app_client): - response = app_client.get('/dummy_tables.json', gather_request=False) + response = app_client.get('/dummy_tables.json') data = response.json assert 'dummy_tables' == data['database'] - assert [{ - 'name': 'table1', - 'columns': ['c1', 'c2', 'c3'], - 'primary_keys': [], - 'count': 2, - 'label_column': None, - 'hidden': False, - 'fts_table': None, - 'foreign_keys': {'incoming': [], 'outgoing': []} - }, { - 'name': 'table2', - 'columns': ['c1', 'c2', 'c3'], - 'primary_keys': [], - 'count': 2, - 'label_column': None, - 'hidden': False, - 'fts_table': None, - 'foreign_keys': {'incoming': [], 'outgoing': []} - }] == data['tables'] + assert len(data['tables']) == 2 + assert data['tables'][0]['count'] == 2 + assert data['tables'][0]['columns'] == ['c1', 'c2', 'c3'] def test_custom_sql(app_client): response = app_client.get( @@ -39,7 +23,6 @@ def test_custom_sql(app_client): 'sql': 'select c1 from table1', '_shape': 'objects' }), - gather_request=False ) data = response.json assert { @@ -56,33 +39,27 @@ def test_custom_sql(app_client): assert not data['truncated'] def test_invalid_custom_sql(app_client): - response = app_client.get( - '/dummy_tables.json?sql=.schema', - gather_request=False - ) + response = app_client.get('/dummy_tables.json?sql=.schema') assert response.status == 400 assert response.json['ok'] is False assert 'Statement must be a SELECT' == response.json['error'] def test_table_json(app_client): - response = app_client.get( - '/dummy_tables/table2.json?_shape=objects', - gather_request=False - ) + response = app_client.get('/dummy_tables/table2.json?_shape=objects') assert response.status == 200 data = response.json - assert data['query']['sql'] == 'select rowid, * from table2 order by rowid limit 51' - assert data['rows'] == [{ - 'rowid': 1, - 'c1': 100, - 'c2': 120, - 'c3': 130 - }, { - 'rowid': 2, - 'c1': 200, - 'c2': 220, - 'c3': 230 - }] + assert data['query']['sql'] == 'select c1, c2, c3 from table2 limit 51' + assert data['rows'] == [ + { + 'c1': 100, + 'c2': 120, + 'c3': 130, + }, + { + 'c1': 200, + 'c2': 220, + 'c3': 230, + }] def test_table_not_exists_json(app_client): assert { @@ -90,59 +67,47 @@ def test_table_not_exists_json(app_client): 'error': 'Table not found: blah', 'status': 404, 'title': None, - } == app_client.get( - '/dummy_tables/blah.json', gather_request=False - ).json + } == app_client.get('/dummy_tables/blah.json').json def test_table_shape_arrays(app_client): - response = app_client.get( - '/dummy_tables/table2.json?_shape=arrays', - gather_request=False - ) + response = app_client.get('/dummy_tables/table2.json?_shape=arrays') assert [ - [1, 100, 120, 130], - [2, 200, 220, 230], + [100, 120, 130], + [200, 220, 230], ] == response.json['rows'] def test_table_shape_objects(app_client): - response = app_client.get( - '/dummy_tables/table2.json?_shape=objects', - gather_request=False - ) - assert [{ - 'rowid': 1, - 'c1': 100, - 'c2': 120, - 'c3': 130, - }, { - 'rowid': 2, - 'c1': 200, - 'c2': 220, - 'c3': 230, - }] == response.json['rows'] + response = app_client.get('/dummy_tables/table2.json?_shape=objects') + assert [ + { + 'c1': 100, + 'c2': 120, + 'c3': 130, + }, + { + 'c1': 200, + 'c2': 220, + 'c3': 230, + }, + ] == response.json['rows'] def test_table_shape_array(app_client): - response = app_client.get( - '/dummy_tables/table2.json?_shape=array', - gather_request=False - ) - assert [{ - 'rowid': 1, - 'c1': 100, - 'c2': 120, - 'c3': 130, - }, { - 'rowid': 2, - 'c1': 200, - 'c2': 220, - 'c3': 230, - }] == response.json + response = app_client.get('/dummy_tables/table2.json?_shape=array') + assert [ + { + 'c1': 100, + 'c2': 120, + 'c3': 130, + }, + { + 'c1': 200, + 'c2': 220, + 'c3': 230, + }, + ] == response.json def test_table_shape_invalid(app_client): - response = app_client.get( - '/dummy_tables/table2.json?_shape=invalid', - gather_request=False - ) + response = app_client.get('/dummy_tables/table2.json?_shape=invalid') assert { 'ok': False, 'error': 'Invalid _shape: invalid',