To ensure that the "byt2str" module operates correctly, the following unit test has been devised. This recipe should ensure the correctness of the code and validate all corrections for accuracy when run. If you are not familiar with the "unittest" or "test" modules, this code may of be interest for the purpose of developing your own library validation suites. Testing code is important for providing a certain amount of assurance that the code being run is correct. If the code is changed incorrectly, a test like this should be able to detect a problem.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import byt2str
import unittest
from test import support
class ConvertTest(unittest.TestCase):
def test_min(self):
# Check that the "convert" min range is correct.
self.assertEqual(byt2str.convert(1), '1 Byte')
with self.assertRaises(AssertionError):
byt2str.convert(0)
def test_max(self):
# Test the max range for "convert" for errors.
prefix = ('geop', 'bronto', 'yotta', 'zetta', 'exa',
'peta', 'tera', 'giga', 'mega', 'kilo', '')
suffix = map(lambda s: '1023 ' + (s + 'bytes').capitalize(), prefix)
self.assertEqual(byt2str.convert((1 << 110) - 1), ', '.join(suffix))
with self.assertRaises(AssertionError):
byt2str.convert(1 << 110)
def test_ternary(self):
# Ensure that three separate components validate.
string = byt2str.convert((1 << 40) + (1 << 20) * 123 + 456)
self.assertEqual(string, '1 Terabyte, 123 Megabytes, 456 Bytes')
def test_binary(self):
# Observe if two non-contiguous numbers render well.
string = byt2str.convert(789 * (1 << 30) + (1 << 10))
self.assertEqual(string, '789 Gigabytes, 1 Kilobyte')
class PartitionTest(unittest.TestCase):
def setUp(self):
# Create test cases for child classes.
self.numbers = (1, 2, 4, 6, 8, 10, 16,
32, 36, 64, 100, 128,
216, 256, 512, 1000)
def test_base(self):
# Execute a series of tests for children.
for number, result in zip(self.numbers, self.results):
answer = list(byt2str.partition_number(number, self.pn_base))
self.assertListEqual(answer, result)
class BaseTwoTest(PartitionTest):
def setUp(self):
# Create variables to test against in base two.
super().setUp()
self.pn_base = 2
self.results = ([1],
[0, 1],
[0, 0, 1],
[0, 1, 1],
[0, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 1, 1, 1, 1])
class BaseSixTest(PartitionTest):
def setUp(self):
# Setup parameters for the base six test.
super().setUp()
self.pn_base = 6
self.results = ([1],
[2],
[4],
[0, 1],
[2, 1],
[4, 1],
[4, 2],
[2, 5],
[0, 0, 1],
[4, 4, 1],
[4, 4, 2],
[2, 3, 3],
[0, 0, 0, 1],
[4, 0, 1, 1],
[2, 1, 2, 2],
[4, 4, 3, 4])
class BaseTenTest(PartitionTest):
def setUp(self):
# Create some data needed for testing base ten.
super().setUp()
self.pn_base = 10
self.results = ([1],
[2],
[4],
[6],
[8],
[0, 1],
[6, 1],
[2, 3],
[6, 3],
[4, 6],
[0, 0, 1],
[8, 2, 1],
[6, 1, 2],
[6, 5, 2],
[2, 1, 5],
[0, 0, 0, 1])
class BytesTest(unittest.TestCase):
def test_ascending(self):
# See if bytes are computed correctly while ascending.
example = ('0 Bytes',
'1 Kilobyte', '2 Megabytes', '3 Gigabytes',
'4 Terabytes', '5 Petabytes', '6 Exabytes',
'7 Zettabytes', '8 Yottabytes', '9 Brontobytes',
'10 Geopbytes')
results = tuple(byt2str.format_bytes(range(0, 11, 1)))
self.assertTupleEqual(results, example)
def test_descending(self):
# Check that strings for descending numbers are valid.
example = ('10 Bytes',
'9 Kilobytes', '8 Megabytes', '7 Gigabytes',
'6 Terabytes', '5 Petabytes', '4 Exabytes',
'3 Zettabytes', '2 Yottabytes', '1 Brontobyte',
'0 Geopbytes')
results = tuple(byt2str.format_bytes(range(10, -1, -1)))
self.assertTupleEqual(results, example)
class SuffixTest(unittest.TestCase):
def test_prefix(self):
# Ensure that each prefix is selected correctly.
example = ['Byte', 'Kilobyte', 'Megabyte', 'Gigabyte',
'Terabyte', 'Petabyte', 'Exabyte', 'Zettabyte',
'Yottabyte', 'Brontobyte', 'Geopbyte']
results = [byt2str.format_suffix(power, 1) for power in range(11)]
self.assertListEqual(results, example)
def test_plural(self):
# Test a range of numbers for the number of the word.
example = ['Bytes', 'Bytes', 'Byte', 'Bytes', 'Bytes']
results = [byt2str.format_suffix(0, number) for number in range(-1, 4)]
self.assertListEqual(results, example)
def test_main():
support.run_unittest(ConvertTest, BytesTest, SuffixTest,
BaseTwoTest, BaseSixTest, BaseTenTest)
if __name__ == '__main__':
test_main()
|
Is this a bug with activestate code that new recipes get a -1 rating automatically ? I am seeing this happen regularly.