11from django .db import connection
2- from django .test import TestCase , skipUnlessDBFeature
2+ from django .test import TestCase , skipIfDBFeature , skipUnlessDBFeature
33
44from django_mongodb_backend .indexes import SearchIndex , VectorSearchIndex
55
66from .models import Article
77
88
9- @skipUnlessDBFeature ("supports_atlas_search" )
109class SearchIndexTests (TestCase ):
1110 # Tests for creating, validating, and removing search indexes using Django's schema editor.
1211 available_apps = None
@@ -29,6 +28,7 @@ def assertAddRemoveIndex(self, editor, model, index):
2928 ),
3029 )
3130
31+ @skipUnlessDBFeature ("supports_atlas_search" )
3232 def test_simple (self ):
3333 with connection .schema_editor () as editor :
3434 index = SearchIndex (
@@ -38,6 +38,23 @@ def test_simple(self):
3838 editor .add_index (index = index , model = Article )
3939 self .assertAddRemoveIndex (editor , Article , index )
4040
41+ @skipIfDBFeature ("supports_atlas_search" )
42+ def test_index_not_created (self ):
43+ with connection .schema_editor () as editor :
44+ index = SearchIndex (
45+ name = "recent_article_idx" ,
46+ fields = ["number" ],
47+ )
48+ editor .add_index (index = index , model = Article )
49+ self .assertNotIn (
50+ index .name ,
51+ connection .introspection .get_constraints (
52+ cursor = None ,
53+ table_name = Article ._meta .db_table ,
54+ ),
55+ )
56+
57+ @skipUnlessDBFeature ("supports_atlas_search" )
4158 def test_multiple_fields (self ):
4259 with connection .schema_editor () as editor :
4360 index = SearchIndex (
@@ -80,7 +97,6 @@ def test_multiple_fields(self):
8097 self .assertAddRemoveIndex (editor , Article , index )
8198
8299
83- @skipUnlessDBFeature ("supports_atlas_search" )
84100class VectorSearchIndexTests (TestCase ):
85101 # Tests for creating, validating, and removing vector search indexes
86102 # using Django's schema editor.
@@ -104,6 +120,7 @@ def assertAddRemoveIndex(self, editor, model, index):
104120 ),
105121 )
106122
123+ @skipUnlessDBFeature ("supports_atlas_search" )
107124 def test_deconstruct_default_similarity (self ):
108125 index = VectorSearchIndex (
109126 name = "recent_article_idx" ,
@@ -113,6 +130,23 @@ def test_deconstruct_default_similarity(self):
113130 new = VectorSearchIndex (* args , ** kwargs )
114131 self .assertEqual (new .similarities , index .similarities )
115132
133+ @skipIfDBFeature ("supports_atlas_search" )
134+ def test_index_not_created (self ):
135+ with connection .schema_editor () as editor :
136+ index = VectorSearchIndex (
137+ name = "recent_article_idx" ,
138+ fields = ["number" ],
139+ )
140+ editor .add_index (index = index , model = Article )
141+ self .assertNotIn (
142+ index .name ,
143+ connection .introspection .get_constraints (
144+ cursor = None ,
145+ table_name = Article ._meta .db_table ,
146+ ),
147+ )
148+
149+ @skipUnlessDBFeature ("supports_atlas_search" )
116150 def test_deconstruct_with_similarities (self ):
117151 index = VectorSearchIndex (
118152 name = "recent_article_idx" ,
@@ -123,6 +157,7 @@ def test_deconstruct_with_similarities(self):
123157 new = VectorSearchIndex (* args , ** kwargs )
124158 self .assertEqual (new .similarities , index .similarities )
125159
160+ @skipUnlessDBFeature ("supports_atlas_search" )
126161 def test_simple_vector_search (self ):
127162 with connection .schema_editor () as editor :
128163 index = VectorSearchIndex (
@@ -132,6 +167,7 @@ def test_simple_vector_search(self):
132167 editor .add_index (index = index , model = Article )
133168 self .assertAddRemoveIndex (editor , Article , index )
134169
170+ @skipUnlessDBFeature ("supports_atlas_search" )
135171 def test_multiple_fields (self ):
136172 with connection .schema_editor () as editor :
137173 index = VectorSearchIndex (
0 commit comments