5
5
from django .core .management import call_command
6
6
from django .utils .six import StringIO
7
7
8
+ from django_elasticsearch_dsl import Index
8
9
from django_elasticsearch_dsl .management .commands .search_index import Command
9
10
from django_elasticsearch_dsl .registries import DocumentRegistry
10
11
11
12
from .fixtures import WithFixturesMixin
12
13
13
14
14
15
class SearchIndexTestCase (WithFixturesMixin , TestCase ):
16
+ def _mock_setup (self ):
17
+ # Mock Patch object
18
+ patch_registry = patch (
19
+ 'django_elasticsearch_dsl.management.commands.search_index.registry' , self .registry )
20
+
21
+ patch_registry .start ()
22
+
23
+ methods = ['delete' , 'create' ]
24
+ for index in [self .index_a , self .index_b ]:
25
+ for method in methods :
26
+ obj_patch = patch .object (index , method )
27
+ obj_patch .start ()
28
+
29
+ self .addCleanup (patch .stopall )
30
+
15
31
def setUp (self ):
16
32
self .out = StringIO ()
17
33
self .registry = DocumentRegistry ()
18
- self .index_a = Mock ( )
19
- self .index_b = Mock ( )
34
+ self .index_a = Index ( 'foo' )
35
+ self .index_b = Index ( 'bar' )
20
36
21
37
self .doc_a1_qs = Mock ()
22
38
self .doc_a1 = self ._generate_doc_mock (
@@ -38,6 +54,8 @@ def setUp(self):
38
54
self .ModelC , self .index_b , self .doc_c1_qs
39
55
)
40
56
57
+ self ._mock_setup ()
58
+
41
59
def test_get_models (self ):
42
60
cmd = Command ()
43
61
with patch (
@@ -97,15 +115,11 @@ def test_force_delete_all_indices(self):
97
115
self .index_b .delete .assert_called_once ()
98
116
99
117
def test_force_delete_bar_model_c_index (self ):
100
-
101
- with patch (
102
- 'django_elasticsearch_dsl.management.commands.'
103
- 'search_index.registry' ,
104
- self .registry
105
- ):
118
+ with patch .object (self .index_b , 'delete' ):
106
119
call_command ('search_index' , stdout = self .out ,
107
- models = ['bar .ModelC' ],
120
+ models = [self .ModelC . _meta . label ],
108
121
action = 'delete' , force = True )
122
+ print (dir (self .index_b .delete ))
109
123
self .index_b .delete .assert_called_once ()
110
124
self .assertFalse (self .index_a .delete .called )
111
125
0 commit comments