Skip to content

Commit 05d2835

Browse files
author
m-vdb
committed
add tests
1 parent f57e496 commit 05d2835

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tests/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class Example(models.Model):
88
lat = models.FloatField()
99
lng = models.FloatField()
1010
category = []
11+
locations = []
1112

1213
def location(self):
1314
return (self.lat, self.lng)
15+
16+
def geolocations(self):
17+
return self.locations

tests/test_index.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def setUp(self):
1717
lat=63.3,
1818
lng=-32.0)
1919
self.instance.category = ['Shop', 'Grocery']
20+
self.instance.locations = [
21+
{'lat': 10.3, 'lng': -20.0},
22+
{'lat': 22.3, 'lng': 10.0},
23+
]
2024

2125
def test_default_index_name(self):
2226
index = AlgoliaIndex(Example, self.client)
@@ -60,6 +64,26 @@ class ExampleIndex(AlgoliaIndex):
6064
obj = index._build_object(self.instance)
6165
self.assertEqual(obj['_geoloc'], {'lat': 63.3, 'lng': -32.0})
6266

67+
def test_several_geo_fields(self):
68+
class ExampleIndex(AlgoliaIndex):
69+
geo_field = 'geolocations'
70+
71+
index = ExampleIndex(Example, self.client)
72+
obj = index._build_object(self.instance)
73+
self.assertEqual(obj['_geoloc'], [
74+
{'lat': 10.3, 'lng': -20.0},
75+
{'lat': 22.3, 'lng': 10.0},
76+
])
77+
78+
def test_geo_fields_already_formatted(self):
79+
class ExampleIndex(AlgoliaIndex):
80+
geo_field = 'geolocations'
81+
82+
self.instance.locations = {'lat': 10.3, 'lng': -20.0}
83+
index = ExampleIndex(Example, self.client)
84+
obj = index._build_object(self.instance)
85+
self.assertEqual(obj['_geoloc'], {'lat': 10.3, 'lng': -20.0})
86+
6387
def test_none_geo_fields(self):
6488
class ExampleIndex(AlgoliaIndex):
6589
geo_field = 'location'

0 commit comments

Comments
 (0)