示例#1
0
    def test_malformed_random_expression(self):
        """Raise exception if expression does not follow the format."""
        str_expr = "this is random expression"
        allowed_fields = {'time': int, 'date': datetime.date}

        msg = "Invalid expression: this is random expression"
        with self.assertRaisesRegex(ParsingException, msg):
            parsed_result = parse_search(str_expr, allowed_fields)
示例#2
0
    def test_malformed_expression(self):
        """Raise exception if expression does not follow the format."""
        str_expr = "time lte 600 AND (malformed expression)"
        allowed_fields = {'time': int}

        msg = "Invalid expression: malformed expression"
        with self.assertRaisesRegex(ParsingException, msg):
            parsed_result = parse_search(str_expr, allowed_fields)
示例#3
0
    def test_operator_not_supported(self):
        """Raise exception if different operators than supported are used."""
        str_expr = "user__pk = 16"
        allowed_fields = {'user__pk': 16}

        msg = "Operator not supported: ="
        with self.assertRaisesRegex(ParsingException, msg):
            parsed_result = parse_search(str_expr, allowed_fields)
示例#4
0
    def test_field_not_allowed(self):
        """Raise exception if expression contains fields that are not allowed.
        """
        str_expr = "(date lt 2020-04-15) OR (user__pk eq 16)"
        allowed_fields = {
            'date': datetime.date,
        }

        msg = "Search field not supported: user__pk"
        with self.assertRaisesRegex(ParsingException, msg):
            parsed_result = parse_search(str_expr, allowed_fields)
示例#5
0
 def test_empty_expression(self):
     """Test return None if empty string provided."""
     str_expr = "           "
     allowed_fields = {'time': int, 'date': datetime.date}
     parsed_result = parse_search(str_expr, allowed_fields)
     self.assertIsNone(parsed_result)
示例#6
0
 def _make_test(self, str_expr, allowed_fields, expected_filters):
     parsed_result = parse_search(str_expr, allowed_fields)
     self.assertEqual(expected_filters, parsed_result)
示例#7
0
    def get_search_filters(self):
        search_expr = self.request.GET.get('search', None)
        if not search_expr:
            return None

        return parse_search(search_expr, self.filter_fields)