def test_attribute_not_between(): ast = parse('attr NOT BETWEEN 2 AND 5') assert ast == BetweenPredicateNode( AttributeExpression('attr'), LiteralExpression(2), LiteralExpression(5), True, )
def test_string_ilike(): ast = parse('attr ILIKE "some%"') assert ast == LikePredicateNode( AttributeExpression('attr'), LiteralExpression('some%'), False, False, )
def test_attribute_not_in_list(): ast = parse('attr NOT IN ("A", "B", \'C\', \'D\')') assert ast == InPredicateNode(AttributeExpression('attr'), [ LiteralExpression("A"), LiteralExpression("B"), LiteralExpression("C"), LiteralExpression("D"), ], True)
def test_attribute_in_list(): ast = parse('attr IN (1, 2, 3, 4)') assert ast == InPredicateNode(AttributeExpression('attr'), [ LiteralExpression(1), LiteralExpression(2), LiteralExpression(3), LiteralExpression(4), ], False)
def test_string_not_like(): ast = parse('attr NOT LIKE "some%"') assert ast == LikePredicateNode( AttributeExpression('attr'), LiteralExpression('some%'), True, True, )
def test_bbox_simple(): ast = parse('BBOX(geometry, 1, 2, 3, 4)') assert ast == BBoxPredicateNode( AttributeExpression('geometry'), LiteralExpression(1), LiteralExpression(2), LiteralExpression(3), LiteralExpression(4), )
def test_bbox_crs(): ast = parse('BBOX(geometry, 1, 2, 3, 4, "EPSG:3875")') assert ast == BBoxPredicateNode( AttributeExpression('geometry'), LiteralExpression(1), LiteralExpression(2), LiteralExpression(3), LiteralExpression(4), 'EPSG:3875', )
def test_attribute_arithmetic_div(): ast = parse('attr = 5 / 2') assert ast == ComparisonPredicateNode( AttributeExpression('attr'), ArithmeticExpressionNode( LiteralExpression(5), LiteralExpression(2), '/', ), '=', )
def evaluate(self, cql_expr, expected_ids, model_type=None): model_type = model_type or models.Record mapping = models.FIELD_MAPPING mapping_choices = models.MAPPING_CHOICES ast = parse(cql_expr, GEOSGeometry, Polygon.from_bbox, parse_datetime, parse_duration) filters = to_filter(ast, mapping, mapping_choices) qs = model_type.objects.filter(filters) self.assertEqual( expected_ids, type(expected_ids)(qs.values_list("identifier", flat=True)))
def test_attribute_arithmetic_div_sub_bracketted(): ast = parse('attr = 3 / (5 - 2)') assert ast == ComparisonPredicateNode( AttributeExpression('attr'), ArithmeticExpressionNode( LiteralExpression(3), ArithmeticExpressionNode( LiteralExpression(5), LiteralExpression(2), '-', ), '/', ), '=', )
def test_attribute_arithmetic_add_mul(): ast = parse('attr = 3 + 5 * 2') assert ast == ComparisonPredicateNode( AttributeExpression('attr'), ArithmeticExpressionNode( LiteralExpression(3), ArithmeticExpressionNode( LiteralExpression(5), LiteralExpression(2), '*', ), '+', ), '=', )
def apply(qs, cql, exclude=False): """ Applies a given CQL filter on a passed queryset. The field mapping is deducted from the model of the passed queryset. A new queryset is returned with all filters applied. :param qs: the base query to apply the filters on. The :attr:`model` is used to determine the metadata field mappings. :param cql: a string containing the CQL expressions to be parsed and applied :param exclude: whether the filters shall be applied using :meth:`exclude`. Default is ``False``. :returns: A new queryset object representing the filtered queryset. :rtype: :class:`django.db.models.QuerySet` """ mapping, mapping_choices = get_field_mapping_for_model(qs.model) ast = parse(cql) filters = to_filter(ast, mapping, mapping_choices) if exclude: return qs.exclude(filters) else: return qs.filter(filters)
def test_attribute_ne_literal(): ast = parse('attr <> 5') assert ast == ComparisonPredicateNode(AttributeExpression('attr'), LiteralExpression(5), '<>')
def test_attribute_gte_literal(): ast = parse('attr >= 5') assert ast == ComparisonPredicateNode(AttributeExpression('attr'), LiteralExpression(5.0), '>=')
def test_attribute_eq_literal(): ast = parse('attr = "A"') assert ast == ComparisonPredicateNode(AttributeExpression('attr'), LiteralExpression('A'), '=')
def test_attribute_is_not_null(): ast = parse('attr IS NOT NULL') assert ast == NullPredicateNode(AttributeExpression('attr'), True)