Exemple #1
0
def test_lookups(books):
    assert books[F.last_modified.year == timezone.now().year].count() == 5
    assert isinstance(F.quantity.min(), models.Min)
    assert isinstance(F.quantity.max(), models.Max)
    assert isinstance(F.quantity.sum(), models.Sum)
    assert isinstance(F.quantity.mean(), models.Avg)
    assert str(F.quantity.count()).startswith('Count(F(quantity)')
    assert str(F.count(distinct=True)) == "Count('*', distinct=True)"
    assert isinstance(F.quantity.var(sample=True), models.Variance)
    assert isinstance(F.quantity.std(sample=True), models.StdDev)
    exprs = list(map(F.author.contains, 'AB'))
    assert str(F.any(
        exprs)) == "(OR: ('author__contains', 'A'), ('author__contains', 'B'))"
    assert str(
        F.all(exprs)
    ) == "(AND: ('author__contains', 'A'), ('author__contains', 'B'))"

    authors = books['author']
    assert set(authors.isin('AB')) == {'A', 'B'}
    assert set(authors.iexact('a')) == {'A'}
    assert set(authors.contains('A')) == {'A'}
    assert set(authors.icontains('a')) == {'A'}
    assert set(authors.startswith('A')) == {'A'}
    assert set(authors.istartswith('a')) == {'A'}
    assert set(authors.endswith('A')) == {'A'}
    assert set(authors.iendswith('a')) == {'A'}
    assert set(authors.range('A', 'B')) == {'A', 'B'}
    assert set(authors.regex('A')) == {'A'}
    assert set(authors.iregex('a')) == {'A'}
Exemple #2
0
def test_spatial_functions(books):
    from django.contrib.gis.geos import Point

    point = Point(0, 0, srid=4326)

    assert isinstance(F.location.area, gis.functions.Area)
    assert isinstance(F.location.geojson(), gis.functions.AsGeoJSON)
    assert isinstance(F.location.gml(), gis.functions.AsGML)
    assert isinstance(F.location.kml(), gis.functions.AsKML)
    assert isinstance(F.location.svg(), gis.functions.AsSVG)
    assert isinstance(F.location.bounding_circle(),
                      gis.functions.BoundingCircle)
    assert isinstance(F.location.centroid, gis.functions.Centroid)
    assert isinstance(F.location.envelope, gis.functions.Envelope)
    assert isinstance(F.location.geohash(), gis.functions.GeoHash)
    assert isinstance(F.location.make_valid(), gis.functions.MakeValid)
    assert isinstance(F.location.mem_size, gis.functions.MemSize)
    assert isinstance(F.location.num_geometries, gis.functions.NumGeometries)
    assert isinstance(F.location.num_points, gis.functions.NumPoints)
    assert isinstance(F.location.perimeter, gis.functions.Perimeter)
    assert isinstance(F.location.point_on_surface,
                      gis.functions.PointOnSurface)
    assert isinstance(F.location.reverse(), gis.functions.Reverse)
    assert isinstance(F.location.scale(0, 0), gis.functions.Scale)
    assert isinstance(F.location.snap_to_grid(0), gis.functions.SnapToGrid)
    assert isinstance(F.location.transform(point.srid),
                      gis.functions.Transform)
    assert isinstance(F.location.translate(0, 0), gis.functions.Translate)

    dist = F.location.distance(point)
    assert isinstance(dist, gis.functions.Distance)
    fields, items = zip(
        *F.all([(dist < 0), (dist <= 0), (dist > 0), (dist >= 0),
                dist.within(0)]).children)
    assert fields == (
        'location__distance_lt',
        'location__distance_lte',
        'location__distance_gt',
        'location__distance_gte',
        'location__dwithin',
    )
    assert items == ((point, 0), ) * 5
    ((field, values), ) = (F.location.distance(point) > 0).children
    assert values == (point, 0)

    assert isinstance(F.location.difference(point), gis.functions.Difference)
    assert isinstance(F.location.intersection(point),
                      gis.functions.Intersection)
    assert isinstance(F.location.symmetric_difference(point),
                      gis.functions.SymDifference)
    assert isinstance(F.location.union(point), gis.functions.Union)

    assert type(books).collect.__name__ == 'Collect'
    assert type(books).extent.__name__ == 'Extent'
    assert type(books).extent3d.__name__ == 'Extent3D'
    assert type(books).make_line.__name__ == 'MakeLine'
    assert type(books).union.__name__ == 'Union'
    with pytest.raises(
            ValueError,
            match="Geospatial aggregates only allowed on geometry fields."):
        books['id'].collect()