Skip to content

Commit c2ddeb0

Browse files
fix geo-polygon tests
1 parent f4f16d1 commit c2ddeb0

File tree

2 files changed

+78
-38
lines changed

2 files changed

+78
-38
lines changed

examples/requirements/test.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
Faker==0.7.17
2-
py==1.4.31
3-
pytest==3.0.2
41
factory_boy==2.7.0
52
fake-factory==0.7.2
6-
pytest-django==2.9.1
3+
Faker==0.7.17
4+
py==1.4.31
75
pytest-cov==2.2.1
6+
pytest-django==2.9.1
7+
pytest-ordering==0.5
8+
pytest==3.0.2
89
selenium==2.53.6
9-
tox==2.3.1
10+
tox==2.3.1

src/django_elasticsearch_dsl_drf/tests/test_filtering_geo_spatial.py

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,68 +88,107 @@ def test_field_filter_geo_distance(self):
8888
self.assertEqual(len(response.data['results']), self.geo_in_count + 1)
8989

9090
@pytest.mark.webtest
91-
def test_field_filter_geo_polygon(self):
92-
"""Field filter geo-polygon.
91+
def _test_field_filter_geo_polygon(self, points, count, fail_test=False):
92+
"""Private helper test field filter geo-polygon.
9393
9494
Example:
9595
9696
http://localhost:8000/api/articles/
97-
?location__geo_polygon=3.51,-71.46|-47.63,41.64|62.05,29.22
97+
?location__geo_polygon=3.51,71.46|-47.63,41.64|62.05,29.22
98+
99+
:param points:
100+
:param count:
101+
:param fail_test:
102+
:type points:
103+
:type count:
104+
:type fail_test:
105+
:return:
106+
:rtype:
98107
"""
99108
self.authenticate()
100109

101110
__params = '{},{}|{},{}|{},{}'.format(
102111
3.51,
103-
-71.46,
112+
71.46,
104113
-47.63,
105114
41.64,
106115
62.05,
107116
29.22,
108117
)
109118

119+
# valid_points = [
120+
# (-23.37, 47.51),
121+
# (-2.81, 63.15),
122+
# (15.99, 46.31),
123+
# (26.54, 42.42),
124+
# ]
125+
#
126+
# invalid_points = [
127+
# (-82.79, 72.34),
128+
# (54.31, 72.34),
129+
# (-6.50, 78.42),
130+
# # (-56.42, 82.78),
131+
# ]
132+
publishers = []
133+
134+
url = self.base_publisher_url[:] + '?{}={}'.format(
135+
'location__geo_polygon',
136+
__params
137+
)
138+
data = {}
139+
140+
for __lat, __lon in points:
141+
publishers.append(
142+
factories.PublisherFactory(
143+
latitude=__lat,
144+
longitude=__lon,
145+
)
146+
)
147+
148+
response = self.client.get(url, data)
149+
self.assertEqual(response.status_code, status.HTTP_200_OK)
150+
# Should contain only 4 results
151+
self.assertEqual(len(response.data['results']), count)
152+
153+
return publishers
154+
155+
@pytest.mark.webtest
156+
def test_field_filter_geo_polygon(self):
157+
"""Test field filter geo-polygon.
158+
159+
:return:
160+
"""
110161
valid_points = [
111162
(-23.37, 47.51),
112163
(-2.81, 63.15),
113164
(15.99, 46.31),
114165
(26.54, 42.42),
115166
]
167+
call_command('search_index', '--rebuild', '-f')
168+
return self._test_field_filter_geo_polygon(
169+
valid_points,
170+
4,
171+
fail_test=False
172+
)
116173

174+
@pytest.mark.webtest
175+
def test_field_filter_geo_polygon_fail_test(self):
176+
"""Test field filter geo-polygon (fail test).
177+
178+
:return:
179+
"""
117180
invalid_points = [
118181
(-82.79, 72.34),
119182
(54.31, 72.34),
120183
(-6.50, 78.42),
121184
# (-56.42, 82.78),
122185
]
123-
valid_publishers = []
124-
invalid_publishers = []
125-
126-
for __lat, __lon in valid_points:
127-
valid_publishers.append(
128-
factories.PublisherFactory(
129-
latitude=__lat,
130-
longitude=__lon,
131-
)
132-
)
133-
134-
for __lat, __lon in invalid_points:
135-
invalid_publishers.append(
136-
factories.PublisherFactory(
137-
latitude=__lat,
138-
longitude=__lon,
139-
)
140-
)
141-
142-
url = self.base_publisher_url[:] + '?{}={}'.format(
143-
'location__geo_polygon',
144-
__params
186+
call_command('search_index', '--rebuild', '-f')
187+
return self._test_field_filter_geo_polygon(
188+
invalid_points,
189+
0,
190+
fail_test=True
145191
)
146192

147-
data = {}
148-
response = self.client.get(url, data)
149-
self.assertEqual(response.status_code, status.HTTP_200_OK)
150-
# Should contain only 4 results
151-
self.assertEqual(len(response.data['results']), 4)
152-
153-
154193
if __name__ == '__main__':
155194
unittest.main()

0 commit comments

Comments
 (0)