Skip to content

Commit 62363f3

Browse files
committed
add test for tags
1 parent 20bd6d5 commit 62363f3

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ def __init__(self, model, client):
6767
if self.custom_objectID:
6868
if not (hasattr(model, self.custom_objectID) or
6969
(self.custom_objectID in all_fields)):
70-
raise AlgoliaIndex('{} is not an attribute of {}.'.format(
70+
raise AlgoliaIndexError('{} is not an attribute of {}.'.format(
7171
self.custom_objectID, model))
7272

7373
# Check tags
7474
if self.tags:
7575
if not (hasattr(model, self.tags) or (self.tags in all_fields)):
76-
raise AlgoliaIndex('{} is not an attribute of {}'.format(
76+
raise AlgoliaIndexError('{} is not an attribute of {}'.format(
7777
self.tags, model))
7878

7979
# Check geo_field

tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Example(models.Model):
77
address = models.CharField(max_length=200)
88
lat = models.FloatField()
99
lng = models.FloatField()
10+
category = []
1011

1112
def location(self):
1213
return (self.lat, self.lng)

tests/test_index.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def setUp(self):
1616
address='Finland',
1717
lat=63.3,
1818
lng=-32.0)
19+
self.instance.category = ['Shop', 'Grocery']
1920

2021
def test_default_index_name(self):
2122
index = AlgoliaIndex(Example, self.client)
@@ -36,6 +37,21 @@ class ExampleIndex(AlgoliaIndex):
3637
except:
3738
self.assertRegexpMatches(index.index_name, regex)
3839

40+
def test_custom_objectID(self):
41+
class ExampleIndex(AlgoliaIndex):
42+
custom_objectID = 'uid'
43+
44+
index = ExampleIndex(Example, self.client)
45+
obj = index._build_object(self.instance)
46+
self.assertEqual(obj['objectID'], 4)
47+
48+
def test_invalid_custom_objectID(self):
49+
class ExampleIndex(AlgoliaIndex):
50+
custom_objectID = 'uuid'
51+
52+
with self.assertRaises(AlgoliaIndexError):
53+
index = ExampleIndex(Example, self.client)
54+
3955
def test_geo_fields(self):
4056
class ExampleIndex(AlgoliaIndex):
4157
geo_field = 'location'
@@ -44,13 +60,27 @@ class ExampleIndex(AlgoliaIndex):
4460
obj = index._build_object(self.instance)
4561
self.assertEqual(obj['_geoloc'], {'lat': 63.3, 'lng': -32.0})
4662

47-
def test_custom_objectID(self):
63+
def test_invalid_geo_fields(self):
4864
class ExampleIndex(AlgoliaIndex):
49-
custom_objectID = 'uid'
65+
geo_field = 'position'
66+
67+
with self.assertRaises(AlgoliaIndexError):
68+
index = ExampleIndex(Example, self.client)
69+
70+
def test_tags(self):
71+
class ExampleIndex(AlgoliaIndex):
72+
tags = 'category'
5073

5174
index = ExampleIndex(Example, self.client)
5275
obj = index._build_object(self.instance)
53-
self.assertEqual(obj['objectID'], 4)
76+
self.assertListEqual(obj['_tags'], self.instance.category)
77+
78+
def test_invalid_tags(self):
79+
class ExampleIndex(AlgoliaIndex):
80+
tags = 'categories'
81+
82+
with self.assertRaises(AlgoliaIndexError):
83+
index = ExampleIndex(Example, self.client)
5484

5585
def test_one_field(self):
5686
class ExampleIndex(AlgoliaIndex):
@@ -64,6 +94,7 @@ class ExampleIndex(AlgoliaIndex):
6494
self.assertNotIn('lat', obj)
6595
self.assertNotIn('lng', obj)
6696
self.assertNotIn('location', obj)
97+
self.assertNotIn('category', obj)
6798
self.assertEqual(len(obj), 2)
6899

69100
def test_multiple_fields(self):
@@ -78,6 +109,7 @@ class ExampleIndex(AlgoliaIndex):
78109
self.assertNotIn('lat', obj)
79110
self.assertNotIn('lng', obj)
80111
self.assertNotIn('location', obj)
112+
self.assertNotIn('category', obj)
81113
self.assertEqual(len(obj), 3)
82114

83115
def test_invalid_fields(self):

0 commit comments

Comments
 (0)