summaryrefslogtreecommitdiffstats
path: root/tests/test_progress.py
blob: 1f609d58e0893dffe07a7757fd97ce989b66d595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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))