From 1034362b84d4385d8342a180ce2afa6d0ca10652 Mon Sep 17 00:00:00 2001 From: rr- Date: Thu, 2 Jun 2016 20:48:28 +0200 Subject: [PATCH] server/search: fix searching by partial range --- server/szurubooru/search/base_search_config.py | 15 +++++++++++---- .../tests/search/test_tag_search_config.py | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/szurubooru/search/base_search_config.py b/server/szurubooru/search/base_search_config.py index f07f204..6df7eb5 100644 --- a/server/szurubooru/search/base_search_config.py +++ b/server/szurubooru/search/base_search_config.py @@ -1,6 +1,6 @@ import sqlalchemy import szurubooru.errors -from szurubooru import db +from szurubooru import db, errors from szurubooru.func import util from szurubooru.search import criteria @@ -43,8 +43,15 @@ class BaseSearchConfig(object): elif isinstance(criterion, criteria.ArraySearchCriterion): expr = column.in_(int(value) for value in criterion.values) elif isinstance(criterion, criteria.RangedSearchCriterion): - expr = column.between( - int(criterion.min_value), int(criterion.max_value)) + assert criterion.min_value != '' \ + or criterion.max_value != '' + if criterion.min_value != '' and criterion.max_value != '': + expr = column.between( + int(criterion.min_value), int(criterion.max_value)) + elif criterion.min_value != '': + expr = column >= int(criterion.min_value) + elif criterion.max_value != '': + expr = column <= int(criterion.max_value) else: assert False if criterion.negated: @@ -68,7 +75,7 @@ class BaseSearchConfig(object): for value in criterion.values: expr = expr | column.ilike(transformer(value)) elif isinstance(criterion, criteria.RangedSearchCriterion): - raise szurubooru.errors.SearchError( + raise errors.SearchError( 'Composite token %r is invalid in this context.' % (criterion,)) else: assert False diff --git a/server/szurubooru/tests/search/test_tag_search_config.py b/server/szurubooru/tests/search/test_tag_search_config.py index e1d1786..a3c4899 100644 --- a/server/szurubooru/tests/search/test_tag_search_config.py +++ b/server/szurubooru/tests/search/test_tag_search_config.py @@ -133,6 +133,8 @@ def test_filter_by_edit_time( @pytest.mark.parametrize('input,expected_tag_names', [ ('post-count:2', ['t1']), ('post-count:1', ['t2']), + ('post-count:1..', ['t1', 't2']), + ('post-count:..1', ['t2']), ('usage-count:2', ['t1']), ('usage-count:1', ['t2']), ('usages:2', ['t1']),