File: test_progressbar.py

package info (click to toggle)
python-glareclient 0.5.3-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 504 kB
  • sloc: python: 3,857; sh: 57; makefile: 21
file content (77 lines) | stat: -rw-r--r-- 2,924 bytes parent folder | download | duplicates (2)
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
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock
from six import StringIO
import testtools

from glareclient.common import progressbar

MOD = 'glareclient.common.progressbar'


class TestProgressBar(testtools.TestCase):

    @mock.patch(MOD + '.os')
    def test_totalsize_fileno(self, mock_os):
        mock_os.fstat.return_value.st_size = 43
        fake_file = mock.Mock()
        del fake_file.len
        fake_file.fileno.return_value = 42
        pb = progressbar.VerboseFileWrapper(fake_file)
        self.assertEqual(43, pb._totalsize)
        mock_os.fstat.assert_called_once_with(42)

    @mock.patch(MOD + '.sys')
    def test__display_progress_bar(self, mock_sys):
        fake_file = StringIO('test')  # 4 bytes
        fake_file.len = 4
        pb = progressbar.VerboseFileWrapper(fake_file)
        pb._display_progress_bar(2)  # 2 of 4 bytes = 50%
        pb._display_progress_bar(1)  # 3 of 4 bytes = 75%
        pb._display_progress_bar(1)  # 4 of 4 bytes = 100%
        expected = [
            mock.call('\r[===============>              ] 50%'),
            mock.call('\r[======================>       ] 75%'),
            mock.call('\r[=============================>] 100%'),
        ]
        self.assertEqual(expected, mock_sys.stdout.write.mock_calls)

    @mock.patch(MOD + '.sys')
    def test__display_progress_bar_unknown_len(self, mock_sys):
        fake_file = StringIO('')
        fake_file.len = 0
        pb = progressbar.VerboseFileWrapper(fake_file)
        for i in range(6):
            pb._display_progress_bar(1)
        expected = [
            mock.call('\r[-] 1 bytes'),
            mock.call('\r[\\] 2 bytes'),
            mock.call('\r[|] 3 bytes'),
            mock.call('\r[/] 4 bytes'),
            mock.call('\r[-] 5 bytes'),
            mock.call('\r[\\] 6 bytes'),
        ]
        self.assertEqual(expected, mock_sys.stdout.write.mock_calls)

    @mock.patch(MOD + '._ProgressBarBase.__init__')
    @mock.patch(MOD + '._ProgressBarBase._display_progress_bar')
    def test_read(self, mock_display_progress_bar, mock_init):
        mock_init.return_value = None
        pb = progressbar.VerboseFileWrapper()
        pb._wrapped = mock.Mock(len=42)
        pb._wrapped.read.return_value = 'ok'
        pb.read(2)
        mock_display_progress_bar.assert_called_once_with(2)