1
- from unittest import mock
2
-
3
1
from django .db import connection
4
2
from django .test import TestCase , skipIfDBFeature , skipUnlessDBFeature
5
3
8
6
from .models import Article
9
7
10
8
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 :
15
10
def assertAddRemoveIndex (self , editor , model , index ):
16
11
editor .add_index (index = index , model = model )
17
12
self .assertIn (
@@ -30,6 +25,10 @@ def assertAddRemoveIndex(self, editor, model, index):
30
25
),
31
26
)
32
27
28
+
29
+ class SearchIndexTests (TestMixin , TestCase ):
30
+ """Tests for creating, validating, and removing search indexes using Django's schema editor."""
31
+
33
32
@skipUnlessDBFeature ("supports_atlas_search" )
34
33
def test_simple (self ):
35
34
with connection .schema_editor () as editor :
@@ -40,17 +39,6 @@ def test_simple(self):
40
39
editor .add_index (index = index , model = Article )
41
40
self .assertAddRemoveIndex (editor , Article , index )
42
41
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
-
54
42
@skipIfDBFeature ("supports_atlas_search" )
55
43
def test_index_not_created (self ):
56
44
with connection .schema_editor () as editor :
@@ -110,39 +98,11 @@ def test_multiple_fields(self):
110
98
self .assertAddRemoveIndex (editor , Article , index )
111
99
112
100
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
+ """
146
106
147
107
@skipUnlessDBFeature ("supports_atlas_search" )
148
108
def test_deconstruct_default_similarity (self ):
0 commit comments