Added support for gt, gte, lt, lte lookups

Refs #23
magic-columns
Simon Willison 2017-10-24 18:46:49 -07:00
rodzic 6823b09406
commit 630b40038e
2 zmienionych plików z 38 dodań i 13 usunięć

7
app.py
Wyświetl plik

@ -325,14 +325,17 @@ def build_where_clause(args):
'contains': '"{}" like ?', 'contains': '"{}" like ?',
'endswith': '"{}" like ?', 'endswith': '"{}" like ?',
'startswith': '"{}" like ?', 'startswith': '"{}" like ?',
'gt': '"{}" > ?',
'gte': '"{}" >= ?',
'lt': '"{}" < ?',
'lte': '"{}" <= ?',
}[lookup] }[lookup]
value = values[0] value = values[0]
value_convert = { value_convert = {
'exact': lambda s: s,
'contains': lambda s: '%{}%'.format(s), 'contains': lambda s: '%{}%'.format(s),
'endswith': lambda s: '%{}'.format(s), 'endswith': lambda s: '%{}'.format(s),
'startswith': lambda s: '{}%'.format(s), 'startswith': lambda s: '{}%'.format(s),
}[lookup] }.get(lookup, lambda s: s)
converted = value_convert(value) converted = value_convert(value)
sql_bits.append( sql_bits.append(
(template.format(column), converted) (template.format(column), converted)

Wyświetl plik

@ -67,17 +67,39 @@ def test_custom_json_encoder(obj, expected):
@pytest.mark.parametrize('args,expected_where,expected_params', [ @pytest.mark.parametrize('args,expected_where,expected_params', [
({ (
'name_english__contains': ['foo'], {
}, '"name_english" like ?', ['%foo%']), 'name_english__contains': ['foo'],
({ },
'foo': ['bar'], '"name_english" like ?',
'bar__contains': ['baz'], ['%foo%']
}, '"bar" like ? and "foo" = ?', ['%baz%', 'bar']), ),
({ (
'foo__startswith': ['bar'], {
'bar__endswith': ['baz'], 'foo': ['bar'],
}, '"bar" like ? and "foo" like ?', ['%baz', '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): def test_build_where(args, expected_where, expected_params):
actual_where, actual_params = app.build_where_clause(args) actual_where, actual_params = app.build_where_clause(args)