1
1
import operator
2
2
3
3
from 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
13
6
14
7
from .models import Article
15
8
@@ -23,8 +16,8 @@ def test_not_supported(self):
23
16
condition = Q (pk__isnull = True ) & Q (pk__contains = "test1" ),
24
17
)
25
18
self .assertEqual (
26
- "{}" ,
27
- str ( index . _get_condition_mql ( Article , schema_editor = editor )) ,
19
+ index . _get_condition_mql ( Article , schema_editor = editor ) ,
20
+ {} ,
28
21
)
29
22
editor .add_index (index = index , model = Article )
30
23
with connection .cursor () as cursor :
@@ -37,41 +30,33 @@ def test_not_supported(self):
37
30
)
38
31
editor .remove_index (index = index , model = Article )
39
32
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 :
44
36
Index (
45
- name = "raises_on_negated " ,
37
+ name = "test " ,
46
38
fields = ["headline" ],
47
39
condition = ~ Q (pk = True ),
48
40
)._get_condition_mql (Article , schema_editor = editor )
49
- self .assertEqual (
50
- context_manager .exception .args [0 ], "Negated field in indexes is not supported."
51
- )
52
41
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 :
57
45
Index (
58
- name = "raises_on_negated " ,
46
+ name = "test " ,
59
47
fields = ["headline" ],
60
48
condition = Q (pk = True ) ^ Q (pk = False ),
61
49
)._get_condition_mql (Article , schema_editor = editor )
62
- self .assertEqual (context_manager .exception .args [0 ], "Xor in indexes is not supported." )
63
50
64
51
@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 :
69
55
Index (
70
- name = "raises_on_negated " ,
56
+ name = "test " ,
71
57
fields = ["headline" ],
72
58
condition = Q (pk = True ) ^ Q (pk = False ),
73
59
)._get_condition_mql (Article , schema_editor = editor )
74
- self .assertEqual (context_manager .exception .args [0 ], "Or in indexes is not supported." )
75
60
76
61
@skipUnlessDBFeature ("is_mongodb_6_0" )
77
62
def test_composite_index (self ):
@@ -82,15 +67,14 @@ def test_composite_index(self):
82
67
condition = Q (number__gte = 3 ) & (Q (text__gt = "test1" ) | Q (text__in = ["A" , "B" ])),
83
68
)
84
69
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
- }
91
70
self .assertEqual (
92
- target ,
93
71
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
+ },
94
78
)
95
79
editor .add_index (index = index , model = Article )
96
80
with connection .cursor () as cursor :
0 commit comments