From da319a6210db2bff7a77bb033cbb3db69ba364c6 Mon Sep 17 00:00:00 2001 From: NNicklas Boserup Date: Thu, 2 Sep 2021 11:06:16 +0200 Subject: [PATCH] Add tests for valid_values --- tests/drivers/test_raster_drivers.py | 34 ++++++++++++++++++++++++ tests/handlers/test_valid_values.py | 10 +++++++ tests/server/test_flask_api.py | 39 ++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tests/handlers/test_valid_values.py diff --git a/tests/drivers/test_raster_drivers.py b/tests/drivers/test_raster_drivers.py index c3abaac2..b853d879 100644 --- a/tests/drivers/test_raster_drivers.py +++ b/tests/drivers/test_raster_drivers.py @@ -46,6 +46,40 @@ def test_path_override(driver_path, provider, raster_file): assert bogus_path in exc.value +@pytest.mark.parametrize('provider', DRIVERS) +def test_valid_values(driver_path, provider, raster_file): + from terracotta import drivers, exceptions + db = drivers.get_driver(driver_path, provider=provider) + keys = ('some', 'keynames') + + db.create(keys) + db.insert(['some', 'value'], str(raster_file)) + db.insert(['some', 'other_value'], str(raster_file)) + db.insert({'some': 'a', 'keynames': 'third_value'}, str(raster_file)) + + data = db.get_valid_values({}) + assert len(data) == 2 + assert len(data['some']) == 2 + assert len(data['keynames']) == 3 + + data = db.get_valid_values(where=dict(some='some')) + assert len(data) == 2 + assert data['some'] == ['some'] + assert set(data['keynames']) == set(['value', 'other_value']) + + data = db.get_valid_values(where=dict(some='some', keynames='value')) + assert set(data.keys()) == set(['some', 'keynames']) + assert data['some'] == ['some'] + assert data['keynames'] == ['value'] + + data = db.get_valid_values(where=dict(some='unknown')) + assert data == {'some': ['unknown'], 'keynames': []} + + with pytest.raises(exceptions.InvalidKeyError) as exc: + db.get_valid_values(where=dict(unknown='foo')) + assert 'unrecognized keys' in str(exc.value) + + @pytest.mark.parametrize('provider', DRIVERS) def test_where(driver_path, provider, raster_file): from terracotta import drivers, exceptions diff --git a/tests/handlers/test_valid_values.py b/tests/handlers/test_valid_values.py new file mode 100644 index 00000000..1017a575 --- /dev/null +++ b/tests/handlers/test_valid_values.py @@ -0,0 +1,10 @@ + +def test_valid_values_handler(testdb, use_testdb): + import terracotta + from terracotta.handlers import valid_values + + driver = terracotta.get_driver(str(testdb)) + + handler_response = valid_values.valid_values({}) + assert handler_response + assert set(handler_response.keys()) == set(driver.key_names) diff --git a/tests/server/test_flask_api.py b/tests/server/test_flask_api.py index 0ee0f9b6..e337a430 100644 --- a/tests/server/test_flask_api.py +++ b/tests/server/test_flask_api.py @@ -44,6 +44,45 @@ def test_get_metadata_nonexisting(client, use_testdb): assert rv.status_code == 404 +def test_get_valid_values(client, use_testdb): + rv = client.get('/valid_values') + assert rv.status_code == 200 + valid_values = json.loads(rv.data, object_pairs_hook=OrderedDict)['valid_values'] + assert len(valid_values) == 3 + assert len(valid_values['key1']) == 2 + assert 'val11' in valid_values['key1'] and 'val21' in valid_values['key1'] + assert valid_values['akey'] == ['x'] + + +def test_get_valid_values_selective(client, use_testdb): + rv = client.get('/valid_values?key1=val21') + assert rv.status_code == 200 + valid_values = json.loads(rv.data, object_pairs_hook=OrderedDict)['valid_values'] + assert len(valid_values) == 3 + assert valid_values['key1'] == ['val21'] + assert len(valid_values['key2']) == 3 + assert 'val22' in valid_values['key2'] and 'val23' in valid_values['key2'] + assert valid_values['akey'] == ['x'] + + rv = client.get('/valid_values?key1=[val21]') + assert rv.status_code == 200 + valid_values = json.loads(rv.data, object_pairs_hook=OrderedDict)['valid_values'] + assert len(valid_values) == 3 + assert valid_values['key1'] == ['val21'] + assert len(valid_values['key2']) == 3 + assert 'val22' in valid_values['key2'] and 'val23' in valid_values['key2'] + assert valid_values['akey'] == ['x'] + + rv = client.get('/valid_values?key1=val21&key2=[val23,val24]') + assert rv.status_code == 200 + valid_values = json.loads(rv.data, object_pairs_hook=OrderedDict)['valid_values'] + assert len(valid_values) == 3 + assert valid_values['key1'] == ['val21'] + assert len(valid_values['key2']) == 2 + assert 'val23' in valid_values['key2'] and 'val24' in valid_values['key2'] + assert valid_values['akey'] == ['x'] + + def test_get_datasets(client, use_testdb): rv = client.get('/datasets') assert rv.status_code == 200