1+ """Tests for gerrit account commands."""
2+
13import json
24from unittest import mock
35
6+ import pytest
7+
48from gerritclient .tests .unit .cli import clibase
59from gerritclient .tests .utils import fake_account , fake_sshkeyinfo
610
711
812class TestAccountCommand (clibase .BaseCLITest ):
913 """Tests for gerrit account * commands."""
1014
11- def setUp (self ):
12- super ().setUp ()
15+ @pytest .fixture (autouse = True )
16+ def setup_account_mocks (self , setup_client_mock ):
17+ """Set up account-specific mocks."""
1318 self .m_client .get_all .return_value = fake_account .get_fake_accounts (10 )
1419 self .m_client .get_by_id .return_value = fake_account .get_fake_account ()
1520
@@ -74,10 +79,9 @@ def test_account_show(self):
7479 @mock .patch ("sys.stderr" )
7580 def test_account_show_fail (self , mocked_stderr ):
7681 args = "account show"
77- self .assertRaises (SystemExit , self .exec_command , args )
78- self .assertIn (
79- "account show: error:" , mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
80- )
82+ with pytest .raises (SystemExit ):
83+ self .exec_command (args )
84+ assert "account show: error:" in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
8185
8286 def test_account_show_w_details (self ):
8387 account_id = "john"
@@ -125,19 +129,18 @@ def test_account_create_w_parameters_from_bad_file_format_fail(self, mocked_stde
125129 m_open = mock .mock_open (read_data = json .dumps (test_data ))
126130 with mock .patch ("gerritclient.common.utils.open" , m_open , create = True ):
127131 result = self .exec_command (args )
128- self . assertEqual ( 1 , result )
132+ assert result == 1
129133 stderr_output = "" .join (
130134 call [0 ][0 ] for call in mocked_stderr .write .call_args_list
131135 )
132- self . assertIn ( "Unsupported data format" , stderr_output )
136+ assert "Unsupported data format" in stderr_output
133137
134138 @mock .patch ("sys.stderr" )
135139 def test_account_create_fail (self , mocked_stderr ):
136140 args = "account create"
137- self .assertRaises (SystemExit , self .exec_command , args )
138- self .assertIn (
139- "account create: error:" , mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
140- )
141+ with pytest .raises (SystemExit ):
142+ self .exec_command (args )
143+ assert "account create: error:" in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
141144
142145 def test_account_set_fullname (self ):
143146 account_id = "69"
@@ -196,10 +199,11 @@ def test_account_status_show(self):
196199 @mock .patch ("sys.stderr" )
197200 def test_account_status_show_fail (self , mocked_stderr ):
198201 args = "account status show"
199- self .assertRaises (SystemExit , self .exec_command , args )
200- self .assertIn (
201- "run account status show: error:" ,
202- mocked_stderr .write .call_args_list [- 1 ][0 ][0 ],
202+ with pytest .raises (SystemExit ):
203+ self .exec_command (args )
204+ assert (
205+ "account status show: error:"
206+ in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
203207 )
204208
205209 def test_account_status_set (self ):
@@ -216,10 +220,11 @@ def test_account_status_set(self):
216220 @mock .patch ("sys.stderr" )
217221 def test_account_status_set_fail (self , mocked_stderr ):
218222 args = "account status set"
219- self .assertRaises (SystemExit , self .exec_command , args )
220- self .assertIn (
221- "run account status set: error:" ,
222- mocked_stderr .write .call_args_list [- 1 ][0 ][0 ],
223+ with pytest .raises (SystemExit ):
224+ self .exec_command (args )
225+ assert (
226+ "account status set: error:"
227+ in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
223228 )
224229
225230 def test_account_set_password (self ):
@@ -287,10 +292,9 @@ def test_account_ssh_key_show(self):
287292 def test_account_ssh_key_show_fail (self , mocked_stderr ):
288293 account_id = "69"
289294 args = f"account ssh-key show { account_id } "
290- self .assertRaises (SystemExit , self .exec_command , args )
291- self .assertIn (
292- "ssh-key show: error:" , mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
293- )
295+ with pytest .raises (SystemExit ):
296+ self .exec_command (args )
297+ assert "ssh-key show: error:" in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
294298
295299 def test_account_ssh_key_add (self ):
296300 account_id = "69"
@@ -337,8 +341,9 @@ def test_account_ssh_key_add_fail_with_mutually_exclusive_params(
337341 "YImydZAw\u003d \u003d john.doe@example.com"
338342 )
339343 args = f'account ssh-key add { account_id } --ssh-key "{ ssh_key } " --file { expected_path } '
340- self .assertRaises (SystemExit , self .exec_command , args )
341- self .assertIn ("not allowed" , mocked_stderr .write .call_args_list [- 1 ][0 ][0 ])
344+ with pytest .raises (SystemExit ):
345+ self .exec_command (args )
346+ assert "not allowed" in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
342347
343348 def test_account_ssh_key_delete (self ):
344349 account_id = "69"
@@ -403,7 +408,8 @@ def test_account_oauth_access_token_show(self):
403408 @mock .patch ("sys.stderr" )
404409 def test_account_oauth_access_token_fail (self , mocked_stderr ):
405410 args = "account oauth show"
406- self .assertRaises (SystemExit , self .exec_command , args )
407- self .assertIn (
408- "account oauth show: error:" , mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
411+ with pytest .raises (SystemExit ):
412+ self .exec_command (args )
413+ assert (
414+ "account oauth show: error:" in mocked_stderr .write .call_args_list [- 1 ][0 ][0 ]
409415 )
0 commit comments