Skip to content

Commit 5f4420b

Browse files
committed
Add validators test
1 parent 7dc04ab commit 5f4420b

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

django_mongodb_backend/fields/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LengthValidator(BaseValidator):
1313
code = "length"
1414

1515
def compare(self, a, b):
16-
return a == b
16+
return a != b
1717

1818
def clean(self, x):
1919
return len(x)

django_mongodb_backend/indexes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def create_mongodb_index(
126126
class SearchIndex(Index):
127127
suffix = "search"
128128
# Maps Django internal type to atlas search index type.
129-
mongo_data_types = {
129+
search_index_data_types = {
130130
"AutoField": "number",
131131
"BigAutoField": "number",
132132
"BinaryField": "string",
@@ -167,7 +167,7 @@ def create_mongodb_index(
167167
fields = {}
168168
for field_name, _ in self.fields_orders:
169169
field_ = model._meta.get_field(field_name)
170-
type_ = self.mongo_data_types[field_.get_internal_type()]
170+
type_ = self.search_index_data_types[field_.get_internal_type()]
171171
field_path = column_prefix + model._meta.get_field(field_name).column
172172
fields[field_path] = {"type": type_}
173173
return SearchIndexModel(

tests/indexes_/test_atlas_indexes.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
from .models import Article
88

99

10-
class AtlasIndexTests(TestCase):
11-
# Schema editor is used to create the index to test that it works.
12-
# available_apps = ["indexes"]
10+
class SearchIndexTests(TestCase):
11+
# Tests for creating, validating, and removing search indexes using Django's schema editor.
1312
available_apps = None
1413

1514
def assertAddRemoveIndex(self, editor, model, index):
@@ -93,10 +92,10 @@ def test_field_not_exists(self):
9392
editor.add_index(index=index, model=Article)
9493

9594

96-
class SearchIndexTests(TestCase):
97-
# Schema editor is used to create the index to test that it works.
98-
# available_apps = ["indexes"]
99-
available_apps = None # could be removed?
95+
class VectorSearchIndexTests(TestCase):
96+
# Tests for creating, validating, and removing vector search indexes
97+
# using Django's schema editor.
98+
available_apps = None
10099

101100
def assertAddRemoveIndex(self, editor, model, index):
102101
editor.add_index(index=index, model=model)

tests/validators_/__init__.py

Whitespace-only changes.

tests/validators_/tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.core.exceptions import ValidationError
2+
from django.test import SimpleTestCase
3+
4+
from django_mongodb_backend.fields.validators import LengthValidator
5+
6+
7+
class TestValidators(SimpleTestCase):
8+
def test_validators(self):
9+
validator = LengthValidator(10)
10+
with self.assertRaises(ValidationError):
11+
validator([])
12+
with self.assertRaises(ValidationError):
13+
validator(list(range(11)))
14+
self.assertEqual(validator(list(range(10))), None)

0 commit comments

Comments
 (0)