Skip to content

Commit

Permalink
Add tests for valid_values
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeopti committed Sep 2, 2021
1 parent 357655f commit da319a6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/drivers/test_raster_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/handlers/test_valid_values.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions tests/server/test_flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da319a6

Please sign in to comment.