From a3e18a99d5a0ef9be78761b2ac03eb819047d59b Mon Sep 17 00:00:00 2001 From: Billy Olsen Date: Sun, 3 Mar 2024 19:46:47 -0700 Subject: [PATCH] Update unit tests to fix coverage 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 --- unit_tests/test_zaza_model.py | 58 ++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/unit_tests/test_zaza_model.py b/unit_tests/test_zaza_model.py index fedba2d8..f46d5158 100644 --- a/unit_tests/test_zaza_model.py +++ b/unit_tests/test_zaza_model.py @@ -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) @@ -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') @@ -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', @@ -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', @@ -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', @@ -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()