77from ansible_collections .community .sap_libs .tests .unit .compat .mock import patch , MagicMock
88from ansible_collections .community .sap_libs .tests .unit .plugins .modules .utils import AnsibleExitJson , AnsibleFailJson , ModuleTestCase , set_module_args
99
10- sys .modules ['pyrfc' ] = MagicMock ()
11- sys .modules ['pyrfc.Connection' ] = MagicMock ()
12-
13- from ansible_collections .community .sap_libs .plugins .modules import sap_user
14-
1510
1611class TestSAPRfcModule (ModuleTestCase ):
1712
1813 def setUp (self ):
14+ self .pyrfc_mock = {
15+ 'pyrfc' : MagicMock (),
16+ 'pyrfc.Connection' : MagicMock ()
17+ }
18+ self .patcher = patch .dict ('sys.modules' , self .pyrfc_mock )
19+ self .patcher .start ()
1920 super (TestSAPRfcModule , self ).setUp ()
21+ from ansible_collections .community .sap_libs .plugins .modules import sap_user
2022 self .module = sap_user
2123
2224 def tearDown (self ):
25+ self .patcher .stop ()
2326 super (TestSAPRfcModule , self ).tearDown ()
2427
2528 def define_rfc_connect (self , mocker ):
@@ -57,7 +60,7 @@ def test_error_user_create(self):
5760 'PARAMETER' : '' , 'ROW' : 0 , 'SYSTEM' : '' , 'TYPE' : 'E' }]}
5861
5962 with self .assertRaises (AnsibleFailJson ) as result :
60- sap_user .main ()
63+ self . module .main ()
6164 self .assertEqual (result .exception .args [0 ]['msg' ], 'Something went wrong' )
6265
6366 def test_success (self ):
@@ -85,7 +88,7 @@ def test_success(self):
8588 'PARAMETER' : '' , 'ROW' : 0 , 'SYSTEM' : '' , 'TYPE' : 'S' }]}
8689
8790 with self .assertRaises (AnsibleExitJson ) as result :
88- sap_user .main ()
91+ self . module .main ()
8992 self .assertEqual (result .exception .args [0 ]['msg' ], 'User ADMIN created' )
9093
9194 def test_no_changes (self ):
@@ -116,7 +119,7 @@ def test_no_changes(self):
116119 DETAIL .return_value = True
117120
118121 with self .assertRaises (AnsibleExitJson ) as result :
119- sap_user .main ()
122+ self . module .main ()
120123 self .assertEqual (result .exception .args [0 ]['msg' ], 'No changes where made.' )
121124
122125 def test_absent (self ):
@@ -139,7 +142,7 @@ def test_absent(self):
139142 'PARAMETER' : '' , 'ROW' : 0 , 'SYSTEM' : '' , 'TYPE' : 'S' }]}
140143
141144 with self .assertRaises (AnsibleExitJson ) as result :
142- sap_user .main ()
145+ self . module .main ()
143146 self .assertEqual (result .exception .args [0 ]['msg' ], 'User ADMIN deleted' )
144147
145148 def test_lock (self ):
@@ -162,7 +165,7 @@ def test_lock(self):
162165 'PARAMETER' : '' , 'ROW' : 0 , 'SYSTEM' : '' , 'TYPE' : 'S' }]}
163166
164167 with self .assertRaises (AnsibleExitJson ) as result :
165- sap_user .main ()
168+ self . module .main ()
166169 self .assertEqual (result .exception .args [0 ]['msg' ], 'User ADMIN locked' )
167170
168171 def test_unlock (self ):
@@ -185,5 +188,5 @@ def test_unlock(self):
185188 'PARAMETER' : '' , 'ROW' : 0 , 'SYSTEM' : '' , 'TYPE' : 'S' }]}
186189
187190 with self .assertRaises (AnsibleExitJson ) as result :
188- sap_user .main ()
191+ self . module .main ()
189192 self .assertEqual (result .exception .args [0 ]['msg' ], 'User ADMIN unlocked' )
0 commit comments