1- from unittest import mock
2-
31from django .db import connection
42from django .test import TestCase , skipIfDBFeature , skipUnlessDBFeature
53
86from .models import Article
97
108
11- class SearchIndexTests (TestCase ):
12- # Tests for creating, validating, and removing search indexes using Django's schema editor.
13- available_apps = None
14-
9+ class TestMixin :
1510 def assertAddRemoveIndex (self , editor , model , index ):
1611 editor .add_index (index = index , model = model )
1712 self .assertIn (
@@ -30,6 +25,10 @@ def assertAddRemoveIndex(self, editor, model, index):
3025 ),
3126 )
3227
28+
29+ class SearchIndexTests (TestMixin , TestCase ):
30+ """Tests for creating, validating, and removing search indexes using Django's schema editor."""
31+
3332 @skipUnlessDBFeature ("supports_atlas_search" )
3433 def test_simple (self ):
3534 with connection .schema_editor () as editor :
@@ -40,17 +39,6 @@ def test_simple(self):
4039 editor .add_index (index = index , model = Article )
4140 self .assertAddRemoveIndex (editor , Article , index )
4241
43- def test_drop_non_existing_index (self ):
44- with connection .schema_editor () as editor :
45- index = SearchIndex (
46- name = "recent_article_idx" ,
47- fields = ["number" ],
48- )
49- editor .get_collection = mock .MagicMock ()
50- editor .remove_index (index = index , model = Article )
51- # Verify that the collection was not accessed
52- editor .get_collection .assert_not_called ()
53-
5442 @skipIfDBFeature ("supports_atlas_search" )
5543 def test_index_not_created (self ):
5644 with connection .schema_editor () as editor :
@@ -110,39 +98,11 @@ def test_multiple_fields(self):
11098 self .assertAddRemoveIndex (editor , Article , index )
11199
112100
113- class VectorSearchIndexTests (TestCase ):
114- # Tests for creating, validating, and removing vector search indexes
115- # using Django's schema editor.
116- available_apps = None
117-
118- def assertAddRemoveIndex (self , editor , model , index ):
119- editor .add_index (index = index , model = model )
120- self .assertIn (
121- index .name ,
122- connection .introspection .get_constraints (
123- cursor = None ,
124- table_name = model ._meta .db_table ,
125- ),
126- )
127- editor .remove_index (index = index , model = model )
128- self .assertNotIn (
129- index .name ,
130- connection .introspection .get_constraints (
131- cursor = None ,
132- table_name = model ._meta .db_table ,
133- ),
134- )
135-
136- def test_drop_non_existing_index (self ):
137- with connection .schema_editor () as editor :
138- index = SearchIndex (
139- name = "recent_article_idx" ,
140- fields = ["number" ],
141- )
142- editor .get_collection = mock .MagicMock ()
143- editor .remove_index (index = index , model = Article )
144- # Verify that the collection was not accessed
145- editor .get_collection .assert_not_called ()
101+ class VectorSearchIndexTests (TestMixin , TestCase ):
102+ """
103+ Tests for creating, validating, and removing vector search indexes
104+ using Django's schema editor.
105+ """
146106
147107 @skipUnlessDBFeature ("supports_atlas_search" )
148108 def test_deconstruct_default_similarity (self ):
0 commit comments