diff --git a/app.py b/app.py index d199bf45..8806fa8f 100644 --- a/app.py +++ b/app.py @@ -325,14 +325,17 @@ def build_where_clause(args): 'contains': '"{}" like ?', 'endswith': '"{}" like ?', 'startswith': '"{}" like ?', + 'gt': '"{}" > ?', + 'gte': '"{}" >= ?', + 'lt': '"{}" < ?', + 'lte': '"{}" <= ?', }[lookup] value = values[0] value_convert = { - 'exact': lambda s: s, 'contains': lambda s: '%{}%'.format(s), 'endswith': lambda s: '%{}'.format(s), 'startswith': lambda s: '{}%'.format(s), - }[lookup] + }.get(lookup, lambda s: s) converted = value_convert(value) sql_bits.append( (template.format(column), converted) diff --git a/test_helpers.py b/test_helpers.py index 75779d71..4f00ef55 100644 --- a/test_helpers.py +++ b/test_helpers.py @@ -67,17 +67,39 @@ def test_custom_json_encoder(obj, expected): @pytest.mark.parametrize('args,expected_where,expected_params', [ - ({ - 'name_english__contains': ['foo'], - }, '"name_english" like ?', ['%foo%']), - ({ - 'foo': ['bar'], - 'bar__contains': ['baz'], - }, '"bar" like ? and "foo" = ?', ['%baz%', 'bar']), - ({ - 'foo__startswith': ['bar'], - 'bar__endswith': ['baz'], - }, '"bar" like ? and "foo" like ?', ['%baz', 'bar%']), + ( + { + 'name_english__contains': ['foo'], + }, + '"name_english" like ?', + ['%foo%'] + ), + ( + { + 'foo': ['bar'], + 'bar__contains': ['baz'], + }, + '"bar" like ? and "foo" = ?', + ['%baz%', 'bar'] + ), + ( + { + 'foo__startswith': ['bar'], + 'bar__endswith': ['baz'], + }, + '"bar" like ? and "foo" like ?', + ['%baz', 'bar%'] + ), + ( + { + 'foo__lt': ['1'], + 'bar__gt': ['2'], + 'baz__gte': ['3'], + 'bax__lte': ['4'], + }, + '"bar" > ? and "bax" <= ? and "baz" >= ? and "foo" < ?', + ['2', '4', '3', '1'] + ), ]) def test_build_where(args, expected_where, expected_params): actual_where, actual_params = app.build_where_clause(args)