diff options
author | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-07-24 09:14:10 +0100 |
---|---|---|
committer | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-07-24 15:36:57 +0100 |
commit | abc2776cc3f7b1939b8b795c77cabec4448bdd31 (patch) | |
tree | 0980d67e2b38eac581c257daa2223772e4a24574 /tests/test_progress.py | |
parent | 34dbb7cee5944ed93db715d450b0c89e276f2674 (diff) | |
download | virt-bootstrap.git-abc2776cc3f7b1939b8b795c77cabec4448bdd31.tar.gz virt-bootstrap.git-abc2776cc3f7b1939b8b795c77cabec4448bdd31.tar.xz virt-bootstrap.git-abc2776cc3f7b1939b8b795c77cabec4448bdd31.zip |
tests: Add unit tests for "progress" module
Diffstat (limited to 'tests/test_progress.py')
-rw-r--r-- | tests/test_progress.py | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/test_progress.py b/tests/test_progress.py new file mode 100644 index 0000000..1f609d5 --- /dev/null +++ b/tests/test_progress.py @@ -0,0 +1,112 @@ +# Authors: Radostin Stoyanov <rstoyanov1@gmail.com> +# +# Copyright (C) 2017 Radostin Stoyanov +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +""" +Unit tests for methods defined in virtBootstrap.progress +""" + +from tests import unittest +from tests import mock +from tests import progress + + +# pylint: disable=invalid-name +class TestFileSource(unittest.TestCase): + """ + Test cases for Progress module + """ + + ################################### + # Tests for: __init__() + ################################### + def test_progress_init(self): + """ + Ensures that __init__() assigns the collback value to instance + variable and creates dictionary with 'status', 'value' keys. + """ + callback = mock.Mock() + test_instance = progress.Progress(callback) + for key in ['status', 'value']: + self.assertIn(key, test_instance.progress) + self.assertIs(callback, test_instance.callback) + + ################################### + # Tests for: get_progress() + ################################### + def test_get_progress(self): + """ + Ensures that get_progress() returns copy of the progress dictionary + which has the same keys and values. + """ + test_instance = progress.Progress() + test_result = test_instance.get_progress() + self.assertIsNot(test_instance.progress, test_result) + self.assertDictEqual(test_instance.progress, test_result) + + ################################### + # Tests for: update_progress() + ################################### + def test_update_progress_creates_log_record(self): + """ + Ensures that update_progress() creates log record with info level + and pass the status value as message. + """ + test_instance = progress.Progress() + logger = mock.Mock() + status = "Test" + test_instance.update_progress(status=status, logger=logger) + logger.info.assert_called_once_with(status) + + def test_update_progress_update_status_and_value(self): + """ + Ensures that update_progress() creates log record with info level + and pass the status value as message. + """ + test_instance = progress.Progress() + test_instance.progress = {'status': '', 'value': 0} + new_status = 'Test' + new_value = 100 + new_progress = {'status': new_status, 'value': new_value} + test_instance.update_progress(status=new_status, value=new_value) + self.assertDictEqual(test_instance.progress, new_progress) + + def test_update_progress_update_raise_logger_error(self): + """ + Ensures that update_progress() raise ValueError when creating + log record has failed. + """ + msg = 'test' + test_instance = progress.Progress() + logger = mock.Mock() + logger.info.side_effect = Exception(msg) + with self.assertRaises(ValueError) as err: + test_instance.update_progress(logger=logger) + self.assertIn(msg, str(err.exception)) + + def test_update_progress_update_raise_callback_error(self): + """ + Ensures that update_progress() raise ValueError when calling + callback failed. + """ + msg = 'test' + callback = mock.Mock() + callback.side_effect = Exception(msg) + test_instance = progress.Progress(callback) + with self.assertRaises(ValueError) as err: + test_instance.update_progress('foo', 'bar') + self.assertIn(msg, str(err.exception)) |