Skip to content

Commit

Permalink
Update unit tests to fix coverage
Browse files Browse the repository at this point in the history
Increase the coverage of all code changed in this patch series to
100%. Move tests to async tests to ensure that the actual tests are
working as expected.

Signed-off-by: Billy Olsen <[email protected]>
  • Loading branch information
Billy Olsen authored and wolsen committed Mar 4, 2024
1 parent ae61814 commit a3e18a9
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions unit_tests/test_zaza_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,8 @@ def test_run_on_leader(self):
self.cmd = cmd = 'somecommand someargument'
self.patch_object(model, 'Model')
self.Model.return_value = self.Model_mock
self.patch('inspect.isawaitable', return_value=True,
name='isawaitable')
self.assertEqual(model.run_on_leader('app', cmd),
expected)
self.unit2.run.assert_called_once_with(cmd, timeout=None)
Expand Down Expand Up @@ -1027,31 +1029,6 @@ def test_get_relation_id_interface(self):
remote_interface_name='interface'),
51)

def test_update_unknown_action_status_invalid_params(self):
"""Test update unknown action status invalid params."""
self.assertRaises(ValueError, model.update_unknown_action_status,
None, mock.MagicMock())
self.assertRaises(ValueError, model.update_unknown_action_status,
mock.MagicMock(), None)

def test_update_unknown_action_status_not_unknown(self):
"""Test update unknown action status with status not unknown."""
model = mock.MagicMock()
action_obj = mock.MagicMock()
action_obj.data.return_value = {"status": "completed"}

model.update_unknown_action_status(model, action_obj)
model.get_action_status.assert_not_called()

def test_update_unknown_action_status_no_completion_timestamp(self):
"""Test update unknown action status without a completion timestamp."""
model = mock.MagicMock()
action_obj = mock.MagicMock()
action_obj.data.return_value = {"status": "running", "completed": ""}

model.update_unknown_action_status(model, action_obj)
model.get_action_status.assert_not_called()

def test_run_action(self):
self.patch_object(model, 'get_juju_model', return_value='mname')
self.patch_object(model, 'Model')
Expand Down Expand Up @@ -1731,6 +1708,8 @@ def test_block_until_file_has_contents(self):
_fileobj = mock.MagicMock()
_fileobj.__enter__().read.return_value = "somestring"
self._open.return_value = _fileobj
self.patch('inspect.isawaitable', return_value=True,
name='isawaitable')
model.block_until_file_has_contents(
'app',
'/tmp/src/myfile.txt',
Expand All @@ -1756,6 +1735,8 @@ def test_block_until_file_has_contents_juju2_x(self):
_fileobj = mock.MagicMock()
_fileobj.__enter__().read.return_value = "somestring"
self._open.return_value = _fileobj
self.patch('inspect.isawaitable', return_value=False,
name='isawaitable')
model.block_until_file_has_contents(
'app',
'/tmp/src/myfile.txt',
Expand Down Expand Up @@ -1836,6 +1817,8 @@ def test_block_until_file_missing(self):
self.Model.return_value = self.Model_mock
self.patch_object(model, 'get_juju_model', return_value='mname')
self.action.results = {'stdout': "1"}
self.patch('inspect.isawaitable', return_value=True,
name='isawaitable')
model.block_until_file_missing(
'app',
'/tmp/src/myfile.txt',
Expand Down Expand Up @@ -2807,6 +2790,31 @@ async def _g():

await model.async_block_until(_f, _g, timeout=0.1)

async def test_update_unknown_action_status_invalid_params(self):
"""Test update unknown action status invalid params."""
self.assertRaises(ValueError, model.update_unknown_action_status,
None, mock.MagicMock())
self.assertRaises(ValueError, model.update_unknown_action_status,
mock.MagicMock(), None)

async def test_update_unknown_action_status_not_unknown(self):
"""Test update unknown action status with status not unknown."""
mock_model = mock.AsyncMock()
action_obj = mock.AsyncMock()
action_obj.data = {"status": "running"}

await model.async_update_unknown_action_status(model, action_obj)
mock_model.get_action_status.assert_not_called()

async def test_update_unknown_action_status_no_completion_timestamp(self):
"""Test update unknown action status without a completion timestamp."""
model_mock = mock.AsyncMock()
action_obj = mock.MagicMock()
action_obj.data = {"status": "unknown", "completed": ""}

await model.async_update_unknown_action_status(model_mock, action_obj)
model_mock.get_action_status.assert_not_called()

async def test_update_unknown_action_status(self):
"""Test update unknown action status updates status."""
mock_model = mock.AsyncMock()
Expand Down

0 comments on commit a3e18a9

Please sign in to comment.