Skip to content

Commit e0a83d1

Browse files
committed
Refactor unit test
1 parent d9de716 commit e0a83d1

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

django_mongodb_backend/indexes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def check(self, model, connection):
114114
if not connection.features.supports_atlas_search:
115115
errors.append(
116116
Warning(
117-
"This MongoDB server does not support search indexes.",
117+
"This MongoDB server does not support atlas search.",
118118
hint=(
119119
"The index won't be created. Silence this warning if you "
120120
"don't care about it."

tests/system_checks/tests.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@isolate_apps("system_checks", attr_name="apps")
1616
@override_system_checks([check_vector_search_indexes])
1717
class InvalidSearchIndexesTest(TestCase):
18-
def test_search_requires_search_index_support(self):
18+
def test_requires_atlas_search_support(self):
1919
class Article(models.Model):
2020
title = models.CharField(max_length=10)
2121

@@ -29,7 +29,7 @@ class Meta:
2929
errors,
3030
[
3131
checks.Warning(
32-
"This MongoDB server does not support search indexes.",
32+
"This MongoDB server does not support atlas search.",
3333
hint=(
3434
"The index won't be created. Silence this warning if you "
3535
"don't care about it."
@@ -41,11 +41,37 @@ class Meta:
4141
)
4242

4343

44-
@skipUnlessDBFeature("supports_atlas_search")
4544
@isolate_apps("system_checks", attr_name="apps")
4645
@override_system_checks([check_vector_search_indexes])
4746
class InvalidVectorSearchIndexesTest(TestCase):
48-
def test_vectorsearch_requires_size(self):
47+
@skipIfDBFeature("supports_atlas_search")
48+
def test_requires_atlas_search_support(self):
49+
class Article(models.Model):
50+
title = models.CharField(max_length=10)
51+
52+
class Meta:
53+
indexes = [
54+
VectorSearchIndex(fields=["title"]),
55+
]
56+
57+
errors = checks.run_checks(app_configs=self.apps.get_app_configs(), databases={"default"})
58+
self.assertEqual(
59+
errors,
60+
[
61+
checks.Warning(
62+
"This MongoDB server does not support atlas search.",
63+
hint=(
64+
"The index won't be created. Silence this warning if you "
65+
"don't care about it."
66+
),
67+
obj=Article._meta.indexes[0],
68+
id="django_mongodb_backend.indexes.VectorSearchIndex.W001",
69+
)
70+
],
71+
)
72+
73+
@skipUnlessDBFeature("supports_atlas_search")
74+
def test_requires_size(self):
4975
class Article(models.Model):
5076
title_embedded = ArrayField(models.FloatField())
5177

@@ -66,7 +92,8 @@ class Meta:
6692
],
6793
)
6894

69-
def test_vectorsearch_requires_float_inner_field(self):
95+
@skipUnlessDBFeature("supports_atlas_search")
96+
def test_requires_float_inner_field(self):
7097
class Article(models.Model):
7198
title_embedded = ArrayField(models.CharField(), size=30)
7299

@@ -87,7 +114,8 @@ class Meta:
87114
],
88115
)
89116

90-
def test_vectorsearch_unsupported_type(self):
117+
@skipUnlessDBFeature("supports_atlas_search")
118+
def test_unsupported_type(self):
91119
class Article(models.Model):
92120
data = models.JSONField()
93121

@@ -108,7 +136,8 @@ class Meta:
108136
],
109137
)
110138

111-
def test_vectorsearch_invalid_similarity_function(self):
139+
@skipUnlessDBFeature("supports_atlas_search")
140+
def test_invalid_similarity_function(self):
112141
class Article(models.Model):
113142
vector_data = ArrayField(models.DecimalField(), size=10)
114143

@@ -130,7 +159,8 @@ class Meta:
130159
],
131160
)
132161

133-
def test_vectorsearch_invalid_similarities_function(self):
162+
@skipUnlessDBFeature("supports_atlas_search")
163+
def test_invalid_similarities_function(self):
134164
class Article(models.Model):
135165
vector_data = ArrayField(models.DecimalField(), size=10)
136166

@@ -161,7 +191,8 @@ class Meta:
161191
],
162192
)
163193

164-
def test_vectorsearch(self):
194+
@skipUnlessDBFeature("supports_atlas_search")
195+
def test_simple(self):
165196
class Article(models.Model):
166197
vector_data = ArrayField(models.DecimalField(), size=10)
167198

0 commit comments

Comments
 (0)