8
8
9
9
10
10
class PartialIndexTests (TestCase ):
11
+ def assertAddRemoveIndex (self , editor , model , index ):
12
+ editor .add_index (index = index , model = model )
13
+ self .assertIn (
14
+ index .name ,
15
+ connection .introspection .get_constraints (
16
+ cursor = None ,
17
+ table_name = model ._meta .db_table ,
18
+ ),
19
+ )
20
+ editor .remove_index (index = index , model = model )
21
+
11
22
def test_not_supported (self ):
12
23
msg = "MongoDB does not support the 'isnull' lookup in indexes."
13
24
with connection .schema_editor () as editor , self .assertRaisesMessage (NotSupportedError , msg ):
14
25
Index (
15
- name = "not_supported " ,
26
+ name = "test " ,
16
27
fields = ["headline" ],
17
- condition = Q (pk__isnull = True ) & Q ( pk__contains = "test1" ) ,
28
+ condition = Q (pk__isnull = True ),
18
29
)._get_condition_mql (Article , schema_editor = editor )
19
30
20
31
def test_negated_not_supported (self ):
@@ -35,7 +46,7 @@ def test_xor_not_supported(self):
35
46
condition = Q (pk = True ) ^ Q (pk = False ),
36
47
)._get_condition_mql (Article , schema_editor = editor )
37
48
38
- @skipIfDBFeature ("supports_in_index_operator " )
49
+ @skipIfDBFeature ("supports_or_index_operator " )
39
50
def test_or_not_supported (self ):
40
51
msg = "MongoDB < 6.0 does not support the '|' operator in indexes."
41
52
with self .assertRaisesMessage (NotSupportedError , msg ), connection .schema_editor () as editor :
@@ -59,11 +70,10 @@ def test_in_not_supported(self):
59
70
def test_composite_index (self ):
60
71
with connection .schema_editor () as editor :
61
72
index = Index (
62
- name = "composite_proposition " ,
73
+ name = "test " ,
63
74
fields = ["headline" ],
64
75
condition = Q (number__gte = 3 ) & (Q (body__gt = "test1" ) | Q (body__in = ["A" , "B" ])),
65
76
)
66
- index ._get_condition_mql (Article , schema_editor = editor )
67
77
self .assertEqual (
68
78
index ._get_condition_mql (Article , schema_editor = editor ),
69
79
{
@@ -73,78 +83,44 @@ def test_composite_index(self):
73
83
]
74
84
},
75
85
)
76
- editor .add_index (index = index , model = Article )
77
- with connection .cursor () as cursor :
78
- self .assertIn (
79
- index .name ,
80
- connection .introspection .get_constraints (
81
- cursor = cursor ,
82
- table_name = Article ._meta .db_table ,
83
- ),
84
- )
85
- editor .remove_index (index = index , model = Article )
86
+ self .assertAddRemoveIndex (editor , Article , index )
86
87
87
88
def test_composite_op_index (self ):
88
89
operators = (
89
- (
90
- (operator .or_ , "$or" ),
91
- (operator .and_ , "$and" ),
92
- )
93
- if connection .features .supports_in_index_operator
94
- else ((operator .and_ , "$and" ),)
90
+ (operator .or_ , "$or" ),
91
+ (operator .and_ , "$and" ),
95
92
)
93
+ if not connection .features .supports_or_index_operator :
94
+ operators = operators [1 :]
96
95
for op , mongo_operator in operators :
97
96
with self .subTest (operator = op ), connection .schema_editor () as editor :
98
97
index = Index (
99
- name = "composite_proposition " ,
98
+ name = "test " ,
100
99
fields = ["headline" ],
101
100
condition = op (Q (number__gte = 3 ), Q (body__gt = "test1" )),
102
101
)
103
- index ._get_condition_mql (Article , schema_editor = editor )
104
- target = {mongo_operator : [{"number" : {"$gte" : 3 }}, {"body" : {"$gt" : "test1" }}]}
105
102
self .assertEqual (
106
- target ,
103
+ { mongo_operator : [{ "number" : { "$gte" : 3 }}, { "body" : { "$gt" : "test1" }}]} ,
107
104
index ._get_condition_mql (Article , schema_editor = editor ),
108
105
)
109
- editor .add_index (index = index , model = Article )
110
- with connection .cursor () as cursor :
111
- self .assertIn (
112
- index .name ,
113
- connection .introspection .get_constraints (
114
- cursor = cursor ,
115
- table_name = Article ._meta .db_table ,
116
- ),
117
- )
118
- editor .remove_index (index = index , model = Article )
106
+ self .assertAddRemoveIndex (editor , Article , index )
119
107
120
108
def test_simple_operations (self ):
121
- mongo_operators_idx = (
109
+ operators = (
122
110
("gt" , "$gt" ),
123
111
("gte" , "$gte" ),
124
112
("lt" , "$lt" ),
125
113
("lte" , "$lte" ),
126
114
)
127
-
128
- for op , mongo_operator in mongo_operators_idx :
115
+ for op , mongo_operator in operators :
129
116
with self .subTest (operator = op ), connection .schema_editor () as editor :
130
117
index = Index (
131
- name = "composite_proposition " ,
118
+ name = "test " ,
132
119
fields = ["headline" ],
133
120
condition = Q (** {f"number__{ op } " : 3 }),
134
121
)
135
- index ._get_condition_mql (Article , schema_editor = editor )
136
- target = {"number" : {mongo_operator : 3 }}
137
122
self .assertEqual (
138
- target ,
123
+ { "number" : { mongo_operator : 3 }} ,
139
124
index ._get_condition_mql (Article , schema_editor = editor ),
140
125
)
141
- editor .add_index (index = index , model = Article )
142
- with connection .cursor () as cursor :
143
- self .assertIn (
144
- index .name ,
145
- connection .introspection .get_constraints (
146
- cursor = cursor ,
147
- table_name = Article ._meta .db_table ,
148
- ),
149
- )
150
- editor .remove_index (index = index , model = Article )
126
+ self .assertAddRemoveIndex (editor , Article , index )
0 commit comments