summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/common/text/formatters
diff options
context:
space:
mode:
Diffstat (limited to 'test/units/module_utils/common/text/formatters')
-rw-r--r--test/units/module_utils/common/text/formatters/test_bytes_to_human.py116
-rw-r--r--test/units/module_utils/common/text/formatters/test_human_to_bytes.py185
-rw-r--r--test/units/module_utils/common/text/formatters/test_lenient_lowercase.py68
3 files changed, 369 insertions, 0 deletions
diff --git a/test/units/module_utils/common/text/formatters/test_bytes_to_human.py b/test/units/module_utils/common/text/formatters/test_bytes_to_human.py
new file mode 100644
index 0000000..41475f5
--- /dev/null
+++ b/test/units/module_utils/common/text/formatters/test_bytes_to_human.py
@@ -0,0 +1,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)
diff --git a/test/units/module_utils/common/text/formatters/test_human_to_bytes.py b/test/units/module_utils/common/text/formatters/test_human_to_bytes.py
new file mode 100644
index 0000000..d02699a
--- /dev/null
+++ b/test/units/module_utils/common/text/formatters/test_human_to_bytes.py
@@ -0,0 +1,185 @@
+# -*- coding: utf-8 -*-
+# Copyright 2019, Andrew Klychkov @Andersson007 <aaklychkov@mail.ru>
+# Copyright 2019, Sviatoslav Sydorenko <webknjaz@redhat.com>
+# 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 human_to_bytes
+
+
+NUM_IN_METRIC = {
+ 'K': 2 ** 10,
+ 'M': 2 ** 20,
+ 'G': 2 ** 30,
+ 'T': 2 ** 40,
+ 'P': 2 ** 50,
+ 'E': 2 ** 60,
+ 'Z': 2 ** 70,
+ 'Y': 2 ** 80,
+}
+
+
+@pytest.mark.parametrize(
+ 'input_data,expected',
+ [
+ (0, 0),
+ (u'0B', 0),
+ (1024, NUM_IN_METRIC['K']),
+ (u'1024B', NUM_IN_METRIC['K']),
+ (u'1K', NUM_IN_METRIC['K']),
+ (u'1KB', NUM_IN_METRIC['K']),
+ (u'1M', NUM_IN_METRIC['M']),
+ (u'1MB', NUM_IN_METRIC['M']),
+ (u'1G', NUM_IN_METRIC['G']),
+ (u'1GB', NUM_IN_METRIC['G']),
+ (u'1T', NUM_IN_METRIC['T']),
+ (u'1TB', NUM_IN_METRIC['T']),
+ (u'1P', NUM_IN_METRIC['P']),
+ (u'1PB', NUM_IN_METRIC['P']),
+ (u'1E', NUM_IN_METRIC['E']),
+ (u'1EB', NUM_IN_METRIC['E']),
+ (u'1Z', NUM_IN_METRIC['Z']),
+ (u'1ZB', NUM_IN_METRIC['Z']),
+ (u'1Y', NUM_IN_METRIC['Y']),
+ (u'1YB', NUM_IN_METRIC['Y']),
+ ]
+)
+def test_human_to_bytes_number(input_data, expected):
+ """Test of human_to_bytes function, only number arg is passed."""
+ assert human_to_bytes(input_data) == expected
+
+
+@pytest.mark.parametrize(
+ 'input_data,unit',
+ [
+ (u'1024', 'B'),
+ (1, u'K'),
+ (1, u'KB'),
+ (u'1', u'M'),
+ (u'1', u'MB'),
+ (1, u'G'),
+ (1, u'GB'),
+ (1, u'T'),
+ (1, u'TB'),
+ (u'1', u'P'),
+ (u'1', u'PB'),
+ (u'1', u'E'),
+ (u'1', u'EB'),
+ (u'1', u'Z'),
+ (u'1', u'ZB'),
+ (u'1', u'Y'),
+ (u'1', u'YB'),
+ ]
+)
+def test_human_to_bytes_number_unit(input_data, unit):
+ """Test of human_to_bytes function, number and default_unit args are passed."""
+ assert human_to_bytes(input_data, default_unit=unit) == NUM_IN_METRIC.get(unit[0], 1024)
+
+
+@pytest.mark.parametrize('test_input', [u'1024s', u'1024w', ])
+def test_human_to_bytes_wrong_unit(test_input):
+ """Test of human_to_bytes function, wrong units."""
+ with pytest.raises(ValueError, match="The suffix must be one of"):
+ human_to_bytes(test_input)
+
+
+@pytest.mark.parametrize('test_input', [u'b1bbb', u'm2mmm', u'', u' ', -1])
+def test_human_to_bytes_wrong_number(test_input):
+ """Test of human_to_bytes function, number param is invalid string / number."""
+ with pytest.raises(ValueError, match="can't interpret"):
+ human_to_bytes(test_input)
+
+
+@pytest.mark.parametrize(
+ 'input_data,expected',
+ [
+ (0, 0),
+ (u'0B', 0),
+ (u'1024b', 1024),
+ (u'1024B', 1024),
+ (u'1K', NUM_IN_METRIC['K']),
+ (u'1Kb', NUM_IN_METRIC['K']),
+ (u'1M', NUM_IN_METRIC['M']),
+ (u'1Mb', NUM_IN_METRIC['M']),
+ (u'1G', NUM_IN_METRIC['G']),
+ (u'1Gb', NUM_IN_METRIC['G']),
+ (u'1T', NUM_IN_METRIC['T']),
+ (u'1Tb', NUM_IN_METRIC['T']),
+ (u'1P', NUM_IN_METRIC['P']),
+ (u'1Pb', NUM_IN_METRIC['P']),
+ (u'1E', NUM_IN_METRIC['E']),
+ (u'1Eb', NUM_IN_METRIC['E']),
+ (u'1Z', NUM_IN_METRIC['Z']),
+ (u'1Zb', NUM_IN_METRIC['Z']),
+ (u'1Y', NUM_IN_METRIC['Y']),
+ (u'1Yb', NUM_IN_METRIC['Y']),
+ ]
+)
+def test_human_to_bytes_isbits(input_data, expected):
+ """Test of human_to_bytes function, isbits = True."""
+ assert human_to_bytes(input_data, isbits=True) == expected
+
+
+@pytest.mark.parametrize(
+ 'input_data,unit',
+ [
+ (1024, 'b'),
+ (1024, 'B'),
+ (1, u'K'),
+ (1, u'Kb'),
+ (u'1', u'M'),
+ (u'1', u'Mb'),
+ (1, u'G'),
+ (1, u'Gb'),
+ (1, u'T'),
+ (1, u'Tb'),
+ (u'1', u'P'),
+ (u'1', u'Pb'),
+ (u'1', u'E'),
+ (u'1', u'Eb'),
+ (u'1', u'Z'),
+ (u'1', u'Zb'),
+ (u'1', u'Y'),
+ (u'1', u'Yb'),
+ ]
+)
+def test_human_to_bytes_isbits_default_unit(input_data, unit):
+ """Test of human_to_bytes function, isbits = True and default_unit args are passed."""
+ assert human_to_bytes(input_data, default_unit=unit, isbits=True) == NUM_IN_METRIC.get(unit[0], 1024)
+
+
+@pytest.mark.parametrize(
+ 'test_input,isbits',
+ [
+ ('1024Kb', False),
+ ('10Mb', False),
+ ('1Gb', False),
+ ('10MB', True),
+ ('2KB', True),
+ ('4GB', True),
+ ]
+)
+def test_human_to_bytes_isbits_wrong_unit(test_input, isbits):
+ """Test of human_to_bytes function, unit identifier is in an invalid format for isbits value."""
+ with pytest.raises(ValueError, match="Value is not a valid string"):
+ human_to_bytes(test_input, isbits=isbits)
+
+
+@pytest.mark.parametrize(
+ 'test_input,unit,isbits',
+ [
+ (1024, 'Kb', False),
+ ('10', 'Mb', False),
+ ('10', 'MB', True),
+ (2, 'KB', True),
+ ('4', 'GB', True),
+ ]
+)
+def test_human_to_bytes_isbits_wrong_default_unit(test_input, unit, isbits):
+ """Test of human_to_bytes function, default_unit is in an invalid format for isbits value."""
+ with pytest.raises(ValueError, match="Value is not a valid string"):
+ human_to_bytes(test_input, default_unit=unit, isbits=isbits)
diff --git a/test/units/module_utils/common/text/formatters/test_lenient_lowercase.py b/test/units/module_utils/common/text/formatters/test_lenient_lowercase.py
new file mode 100644
index 0000000..1ecc013
--- /dev/null
+++ b/test/units/module_utils/common/text/formatters/test_lenient_lowercase.py
@@ -0,0 +1,68 @@
+# -*- 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
+
+from datetime import datetime
+
+import pytest
+
+from ansible.module_utils.common.text.formatters import lenient_lowercase
+
+
+INPUT_LIST = [
+ u'HELLO',
+ u'Ёлка',
+ u'cafÉ',
+ u'くらとみ',
+ b'HELLO',
+ 1,
+ {1: 'Dict'},
+ True,
+ [1],
+ 3.14159,
+]
+
+EXPECTED_LIST = [
+ u'hello',
+ u'ёлка',
+ u'café',
+ u'くらとみ',
+ b'hello',
+ 1,
+ {1: 'Dict'},
+ True,
+ [1],
+ 3.14159,
+]
+
+result_list = lenient_lowercase(INPUT_LIST)
+
+
+@pytest.mark.parametrize(
+ 'input_value,expected_outcome',
+ [
+ (result_list[0], EXPECTED_LIST[0]),
+ (result_list[1], EXPECTED_LIST[1]),
+ (result_list[2], EXPECTED_LIST[2]),
+ (result_list[3], EXPECTED_LIST[3]),
+ (result_list[4], EXPECTED_LIST[4]),
+ (result_list[5], EXPECTED_LIST[5]),
+ (result_list[6], EXPECTED_LIST[6]),
+ (result_list[7], EXPECTED_LIST[7]),
+ (result_list[8], EXPECTED_LIST[8]),
+ (result_list[9], EXPECTED_LIST[9]),
+ ]
+)
+def test_lenient_lowercase(input_value, expected_outcome):
+ """Test that lenient_lowercase() proper results."""
+ assert input_value == expected_outcome
+
+
+@pytest.mark.parametrize('input_data', [1, False, 1.001, 1j, datetime.now(), ])
+def test_lenient_lowercase_illegal_data_type(input_data):
+ """Test passing objects of illegal types to lenient_lowercase()."""
+ with pytest.raises(TypeError, match='object is not iterable'):
+ lenient_lowercase(input_data)