summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/common/text/formatters/test_bytes_to_human.py
blob: 41475f56cf065559bac0946af52dd35571a5d7a3 (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
113
114
115
116
# -*- coding: utf-8 -*-
# Copyright 2019, Andrew Klychkov @Andersson007 <aaklychkov@mail.ru>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

import pytest

from ansible.module_utils.common.text.formatters import bytes_to_human


@pytest.mark.parametrize(
    'input_data,expected',
    [
        (0, u'0.00 Bytes'),
        (0.5, u'0.50 Bytes'),
        (0.54, u'0.54 Bytes'),
        (1024, u'1.00 KB'),
        (1025, u'1.00 KB'),
        (1536, u'1.50 KB'),
        (1790, u'1.75 KB'),
        (1048576, u'1.00 MB'),
        (1073741824, u'1.00 GB'),
        (1099511627776, u'1.00 TB'),
        (1125899906842624, u'1.00 PB'),
        (1152921504606846976, u'1.00 EB'),
        (1180591620717411303424, u'1.00 ZB'),
        (1208925819614629174706176, u'1.00 YB'),
    ]
)
def test_bytes_to_human(input_data, expected):
    """Test of bytes_to_human function, only proper numbers are passed."""
    assert bytes_to_human(input_data) == expected


@pytest.mark.parametrize(
    'input_data,expected',
    [
        (0, u'0.00 bits'),
        (0.5, u'0.50 bits'),
        (0.54, u'0.54 bits'),
        (1024, u'1.00 Kb'),
        (1025, u'1.00 Kb'),
        (1536, u'1.50 Kb'),
        (1790, u'1.75 Kb'),
        (1048576, u'1.00 Mb'),
        (1073741824, u'1.00 Gb'),
        (1099511627776, u'1.00 Tb'),
        (1125899906842624, u'1.00 Pb'),
        (1152921504606846976, u'1.00 Eb'),
        (1180591620717411303424, u'1.00 Zb'),
        (1208925819614629174706176, u'1.00 Yb'),
    ]
)
def test_bytes_to_human_isbits(input_data, expected):
    """Test of bytes_to_human function with isbits=True proper results."""
    assert bytes_to_human(input_data, isbits=True) == expected


@pytest.mark.parametrize(
    'input_data,unit,expected',
    [
        (0, u'B', u'0.00 Bytes'),
        (0.5, u'B', u'0.50 Bytes'),
        (0.54, u'B', u'0.54 Bytes'),
        (1024, u'K', u'1.00 KB'),
        (1536, u'K', u'1.50 KB'),
        (1790, u'K', u'1.75 KB'),
        (1048576, u'M', u'1.00 MB'),
        (1099511627776, u'T', u'1.00 TB'),
        (1152921504606846976, u'E', u'1.00 EB'),
        (1180591620717411303424, u'Z', u'1.00 ZB'),
        (1208925819614629174706176, u'Y', u'1.00 YB'),
        (1025, u'KB', u'1025.00 Bytes'),
        (1073741824, u'Gb', u'1073741824.00 Bytes'),
        (1125899906842624, u'Pb', u'1125899906842624.00 Bytes'),
    ]
)
def test_bytes_to_human_unit(input_data, unit, expected):
    """Test unit argument of bytes_to_human function proper results."""
    assert bytes_to_human(input_data, unit=unit) == expected


@pytest.mark.parametrize(
    'input_data,unit,expected',
    [
        (0, u'B', u'0.00 bits'),
        (0.5, u'B', u'0.50 bits'),
        (0.54, u'B', u'0.54 bits'),
        (1024, u'K', u'1.00 Kb'),
        (1536, u'K', u'1.50 Kb'),
        (1790, u'K', u'1.75 Kb'),
        (1048576, u'M', u'1.00 Mb'),
        (1099511627776, u'T', u'1.00 Tb'),
        (1152921504606846976, u'E', u'1.00 Eb'),
        (1180591620717411303424, u'Z', u'1.00 Zb'),
        (1208925819614629174706176, u'Y', u'1.00 Yb'),
        (1025, u'KB', u'1025.00 bits'),
        (1073741824, u'Gb', u'1073741824.00 bits'),
        (1125899906842624, u'Pb', u'1125899906842624.00 bits'),
    ]
)
def test_bytes_to_human_unit_isbits(input_data, unit, expected):
    """Test unit argument of bytes_to_human function with isbits=True proper results."""
    assert bytes_to_human(input_data, isbits=True, unit=unit) == expected


@pytest.mark.parametrize('input_data', [0j, u'1B', [1], {1: 1}, None, b'1B'])
def test_bytes_to_human_illegal_size(input_data):
    """Test of bytes_to_human function, illegal objects are passed as a size."""
    e_regexp = (r'(no ordering relation is defined for complex numbers)|'
                r'(unsupported operand type\(s\) for /)|(unorderable types)|'
                r'(not supported between instances of)')
    with pytest.raises(TypeError, match=e_regexp):
        bytes_to_human(input_data)