11import operator
22
33from django .db import NotSupportedError , connection
4- from django .db .models import (
5- Index ,
6- Q ,
7- )
8- from django .test import (
9- TestCase ,
10- skipIfDBFeature ,
11- skipUnlessDBFeature ,
12- )
4+ from django .db .models import Index , Q
5+ from django .test import TestCase , skipIfDBFeature , skipUnlessDBFeature
136
147from .models import Article
158
@@ -23,8 +16,8 @@ def test_not_supported(self):
2316 condition = Q (pk__isnull = True ) & Q (pk__contains = "test1" ),
2417 )
2518 self .assertEqual (
26- "{}" ,
27- str ( index . _get_condition_mql ( Article , schema_editor = editor )) ,
19+ index . _get_condition_mql ( Article , schema_editor = editor ) ,
20+ {} ,
2821 )
2922 editor .add_index (index = index , model = Article )
3023 with connection .cursor () as cursor :
@@ -37,41 +30,33 @@ def test_not_supported(self):
3730 )
3831 editor .remove_index (index = index , model = Article )
3932
40- def test_raises_on_negated (self ):
41- with self .assertRaises (
42- NotSupportedError
43- ) as context_manager , connection .schema_editor () as editor :
33+ def test_negated_not_supported (self ):
34+ msg = "Negated field in indexes is not supported."
35+ with self .assertRaisesMessage (NotSupportedError , msg ), connection .schema_editor () as editor :
4436 Index (
45- name = "raises_on_negated " ,
37+ name = "test " ,
4638 fields = ["headline" ],
4739 condition = ~ Q (pk = True ),
4840 )._get_condition_mql (Article , schema_editor = editor )
49- self .assertEqual (
50- context_manager .exception .args [0 ], "Negated field in indexes is not supported."
51- )
5241
53- def test_raises_on_xor (self ):
54- with self .assertRaises (
55- NotSupportedError
56- ) as context_manager , connection .schema_editor () as editor :
42+ def test_xor_not_supported (self ):
43+ msg = "Xor in indexes is not supported."
44+ with self .assertRaisesMessage (NotSupportedError , msg ), connection .schema_editor () as editor :
5745 Index (
58- name = "raises_on_negated " ,
46+ name = "test " ,
5947 fields = ["headline" ],
6048 condition = Q (pk = True ) ^ Q (pk = False ),
6149 )._get_condition_mql (Article , schema_editor = editor )
62- self .assertEqual (context_manager .exception .args [0 ], "Xor in indexes is not supported." )
6350
6451 @skipIfDBFeature ("is_mongodb_6_0" )
65- def test_raises_on_or (self ):
66- with self .assertRaises (
67- NotSupportedError
68- ) as context_manager , connection .schema_editor () as editor :
52+ def test_or_not_supported (self ):
53+ msg = "Or in indexes is not supported."
54+ with self .assertRaisesMessage (NotSupportedError , msg ), connection .schema_editor () as editor :
6955 Index (
70- name = "raises_on_negated " ,
56+ name = "test " ,
7157 fields = ["headline" ],
7258 condition = Q (pk = True ) ^ Q (pk = False ),
7359 )._get_condition_mql (Article , schema_editor = editor )
74- self .assertEqual (context_manager .exception .args [0 ], "Or in indexes is not supported." )
7560
7661 @skipUnlessDBFeature ("is_mongodb_6_0" )
7762 def test_composite_index (self ):
@@ -82,15 +67,14 @@ def test_composite_index(self):
8267 condition = Q (number__gte = 3 ) & (Q (text__gt = "test1" ) | Q (text__in = ["A" , "B" ])),
8368 )
8469 index ._get_condition_mql (Article , schema_editor = editor )
85- target = {
86- "$and" : [
87- {"number" : {"$gte" : 3 }},
88- {"$or" : [{"text" : {"$gt" : "test1" }}, {"text" : {"$in" : ["A" , "B" ]}}]},
89- ]
90- }
9170 self .assertEqual (
92- target ,
9371 index ._get_condition_mql (Article , schema_editor = editor ),
72+ {
73+ "$and" : [
74+ {"number" : {"$gte" : 3 }},
75+ {"$or" : [{"text" : {"$gt" : "test1" }}, {"text" : {"$in" : ["A" , "B" ]}}]},
76+ ]
77+ },
9478 )
9579 editor .add_index (index = index , model = Article )
9680 with connection .cursor () as cursor :
0 commit comments