diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2014-10-14 12:28:11 +0200 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2014-10-14 12:28:11 +0200 |
commit | 7ebbd0bb7afacb859c313ddaba194197f8226a2d (patch) | |
tree | a5cd9e5459dd434678183f8aaf7b1ae8d2f71f9f | |
parent | b668eb44557ba2bb8239f5abd4a9b4ffcf4d38ae (diff) |
common: Fix case with multiple defined query args.
* If we wanted to search for status=PASS&status=FAIL, before
it would have search only based on the last value analyzed
(could have been PASS or FAIL). Now, if we pass a query like
that, we search for both values.
Internally this will result in a slightly different mongodb
spec SON object, using the $in operator.
* Fix test covering this case.
Change-Id: I206616495873cc4a3ca36a3f12a85aae816a75db
-rw-r--r-- | app/handlers/common.py | 29 | ||||
-rw-r--r-- | app/handlers/tests/test_handlers_common.py | 2 |
2 files changed, 27 insertions, 4 deletions
diff --git a/app/handlers/common.py b/app/handlers/common.py index a708e21..f071dec 100644 --- a/app/handlers/common.py +++ b/app/handlers/common.py @@ -96,14 +96,37 @@ def get_query_spec(query_args_func, valid_keys): :type valid_keys: list :return A `spec` data structure (dictionary). """ + def _get_spec_values(): + """Get the values for the spec data structure. + + Internally used only, with some logic to differentiate between single + and multiple values. Makes sure also that the list of values if valid, + meaning that we do not have None or empty values. + + :return A tuple with the key and its value. + """ + for key in valid_keys: + val = query_args_func(key) or [] + if val: + # Go through the values and make sure we have valid ones. + val = [v for v in val if v] + len_val = len(val) + + if len_val == 1: + val = val[0] + elif len_val > 1: + # More than one value, make sure we look for all of them. + val = {'$in': val} + + yield key, val + spec = {} if all([valid_keys and isinstance(valid_keys, types.ListType)]): spec = { k: v for k, v in [ (key, val) - for key in valid_keys - for val in (query_args_func(key) or []) - if val is not None + for key, val in _get_spec_values() + if val ] } diff --git a/app/handlers/tests/test_handlers_common.py b/app/handlers/tests/test_handlers_common.py index ed2d644..ba3ab04 100644 --- a/app/handlers/tests/test_handlers_common.py +++ b/app/handlers/tests/test_handlers_common.py @@ -151,7 +151,7 @@ class TestHandlersCommon(unittest.TestCase): } return args.get(key, []) - expected = {"a": 2, "b": 3} + expected = {"a": {"$in": [1, 2]}, "b": 3} self.assertEqual(expected, get_query_spec(query_args_func, valid_keys)) def test_get_query_spec_raises(self): |