File: test_geospatial.py

package info (click to toggle)
python-beanie 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 14,427; makefile: 7; sh: 6
file content (121 lines) | stat: -rw-r--r-- 2,880 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from beanie.odm.operators.find.geospatial import (
    Box,
    GeoIntersects,
    GeoWithin,
    Near,
    NearSphere,
)
from tests.odm.models import Sample


async def test_geo_intersects():
    q = GeoIntersects(
        Sample.geo, geo_type="Polygon", coordinates=[[1, 1], [2, 2], [3, 3]]
    )
    assert q == {
        "geo": {
            "$geoIntersects": {
                "$geometry": {
                    "type": "Polygon",
                    "coordinates": [[1, 1], [2, 2], [3, 3]],
                }
            }
        }
    }


async def test_geo_within():
    q = GeoWithin(
        Sample.geo, geo_type="Polygon", coordinates=[[1, 1], [2, 2], [3, 3]]
    )
    assert q == {
        "geo": {
            "$geoWithin": {
                "$geometry": {
                    "type": "Polygon",
                    "coordinates": [[1, 1], [2, 2], [3, 3]],
                }
            }
        }
    }


async def test_box():
    q = Box(Sample.geo, lower_left=[1, 3], upper_right=[2, 4])
    assert q == {"geo": {"$geoWithin": {"$box": [[1, 3], [2, 4]]}}}


async def test_near():
    q = Near(Sample.geo, longitude=1.1, latitude=2.2)
    assert q == {
        "geo": {
            "$near": {
                "$geometry": {"type": "Point", "coordinates": [1.1, 2.2]}
            }
        }
    }

    q = Near(Sample.geo, longitude=1.1, latitude=2.2, max_distance=1)
    assert q == {
        "geo": {
            "$near": {
                "$geometry": {"type": "Point", "coordinates": [1.1, 2.2]},
                "$maxDistance": 1,
            }
        }
    }

    q = Near(
        Sample.geo,
        longitude=1.1,
        latitude=2.2,
        max_distance=1,
        min_distance=0.5,
    )
    assert q == {
        "geo": {
            "$near": {
                "$geometry": {"type": "Point", "coordinates": [1.1, 2.2]},
                "$maxDistance": 1,
                "$minDistance": 0.5,
            }
        }
    }


async def test_near_sphere():
    q = NearSphere(Sample.geo, longitude=1.1, latitude=2.2)
    assert q == {
        "geo": {
            "$nearSphere": {
                "$geometry": {"type": "Point", "coordinates": [1.1, 2.2]}
            }
        }
    }

    q = NearSphere(Sample.geo, longitude=1.1, latitude=2.2, max_distance=1)
    assert q == {
        "geo": {
            "$nearSphere": {
                "$geometry": {"type": "Point", "coordinates": [1.1, 2.2]},
                "$maxDistance": 1,
            }
        }
    }

    q = NearSphere(
        Sample.geo,
        longitude=1.1,
        latitude=2.2,
        max_distance=1,
        min_distance=0.5,
    )
    assert q == {
        "geo": {
            "$nearSphere": {
                "$geometry": {"type": "Point", "coordinates": [1.1, 2.2]},
                "$maxDistance": 1,
                "$minDistance": 0.5,
            }
        }
    }