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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
# -*- coding: utf-8 -*-
# The MIT License (MIT)
#
# Copyright © 2014 Tim Bielawa <timbielawa@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Test parsing strings into bitmath objects
"""
from . import TestCase
import bitmath
class TestParse(TestCase):
def test_parse_b(self):
"""parse_string works on bit strings"""
self.assertEqual(
bitmath.parse_string("123b"),
bitmath.Bit(123))
def test_parse_B(self):
"""parse_string works on byte strings"""
self.assertEqual(
bitmath.parse_string("321B"),
bitmath.Byte(321))
def test_parse_Gb(self):
"""parse_string works on gigabit strings"""
self.assertEqual(
bitmath.parse_string("456Gb"),
bitmath.Gb(456))
def test_parse_MiB(self):
"""parse_string works on mebibyte strings"""
self.assertEqual(
bitmath.parse_string("654 MiB"),
bitmath.MiB(654))
######################################################################
# NIST 'octet' based units
def test_parse_Mio(self):
"""parse_string works on mebioctet strings"""
self.assertEqual(
bitmath.parse_string("654 Mio"),
bitmath.MiB(654))
def test_parse_Eio(self):
"""parse_string works on exbioctet strings"""
self.assertEqual(
bitmath.parse_string("654 Eio"),
bitmath.EiB(654))
# SI 'octet' based units
def test_parse_Mo(self):
"""parse_string works on megaoctet strings"""
self.assertEqual(
bitmath.parse_string("654 Mo"),
bitmath.MB(654))
def test_parse_Eo(self):
"""parse_string works on exaoctet strings"""
self.assertEqual(
bitmath.parse_string("654 Eo"),
bitmath.EB(654))
######################################################################
def test_parse_bad_float(self):
"""parse_string can identify invalid float values"""
with self.assertRaises(ValueError):
bitmath.parse_string("1.23.45 kb")
def test_parse_bad_unit(self):
"""parse_string can identify invalid prefix units"""
with self.assertRaises(ValueError):
bitmath.parse_string("1.23 GIB")
def test_parse_bad_unit2(self):
"""parse_string can identify other prefix units"""
with self.assertRaises(ValueError):
bitmath.parse_string("1.23 QB")
def test_parse_no_unit(self):
"""parse_string can identify strings without units at all"""
with self.assertRaises(ValueError):
bitmath.parse_string("12345")
def test_parse_string_non_string_input(self):
"""parse_string can identify a non-string input"""
with self.assertRaises(ValueError):
bitmath.parse_string(12345)
def test_parse_string_unicode(self):
"""parse_string can handle a unicode string"""
self.assertEqual(
bitmath.parse_string(u"750 GiB"),
bitmath.GiB(750))
######################################################################
def test_parse_unsafe_bad_input_type(self):
"""parse_string_unsafe can identify invalid input types"""
with self.assertRaises(ValueError):
invalid_input = {'keyvalue': 'store'}
bitmath.parse_string_unsafe(invalid_input)
def test_parse_unsafe_invalid_input(self):
"""parse_string_unsafe explodes when given invalid units"""
invalid_input_str = "kitties!"
with self.assertRaises(ValueError):
bitmath.parse_string_unsafe(invalid_input_str)
with self.assertRaises(ValueError):
bitmath.parse_string_unsafe('100 CiB')
with self.assertRaises(ValueError):
bitmath.parse_string_unsafe('100 J')
def test_parse_unsafe_good_number_input(self):
"""parse_string_unsafe can parse unitless number inputs"""
number_input = 100
string_input = "100"
expected_result = bitmath.Byte(100)
self.assertEqual(
bitmath.parse_string_unsafe(number_input),
expected_result)
self.assertEqual(
bitmath.parse_string_unsafe(string_input),
expected_result)
def test_parse_unsafe_handles_SI_K_unit(self):
"""parse_string_unsafe can parse the upper/lowercase SI 'thousand' (k)"""
thousand_lower = "100k"
thousand_upper = "100K"
expected_result = bitmath.kB(100)
self.assertEqual(
bitmath.parse_string_unsafe(thousand_lower),
expected_result)
self.assertEqual(
bitmath.parse_string_unsafe(thousand_upper),
expected_result)
def test_parse_unsafe_NIST_units(self):
"""parse_string_unsafe can parse abbreviated NIST units (Gi, Ki, ...)"""
nist_input = "100 Gi"
expected_result = bitmath.GiB(100)
self.assertEqual(
bitmath.parse_string_unsafe(nist_input),
expected_result)
def test_parse_unsafe_SI(self):
"""parse_string_unsafe can parse all accepted SI inputs"""
# Begin with the kilo unit because it's the most tricky (SI
# defines the unit as a lower-case 'k')
kilo_inputs = [
'100k',
'100K',
'100kb',
'100KB',
'100kB'
]
expected_kilo_result = bitmath.kB(100)
for ki in kilo_inputs:
_parsed = bitmath.parse_string_unsafe(ki)
self.assertEqual(_parsed, expected_kilo_result)
self.assertIs(type(_parsed), type(expected_kilo_result))
# Now check for other easier to parse prefixes
other_inputs = [
'100g',
'100G',
'100gb',
'100gB',
'100GB'
]
expected_gig_result = bitmath.GB(100)
for gi in other_inputs:
_parsed = bitmath.parse_string_unsafe(gi)
self.assertEqual(_parsed, expected_gig_result)
self.assertIs(type(_parsed), type(expected_gig_result))
def test_parse_unsafe_NIST(self):
"""parse_string_unsafe can parse all accepted NIST inputs"""
# Begin with the kilo unit because it's the most tricky (SI
# defines the unit as a lower-case 'k')
kilo_inputs = [
'100ki',
'100Ki',
'100kib',
'100KiB',
'100kiB'
]
expected_kilo_result = bitmath.KiB(100)
for ki in kilo_inputs:
_parsed = bitmath.parse_string_unsafe(ki)
self.assertEqual(_parsed, expected_kilo_result)
self.assertIs(type(_parsed), type(expected_kilo_result))
# Now check for other easier to parse prefixes
other_inputs = [
'100gi',
'100Gi',
'100gib',
'100giB',
'100GiB'
]
expected_gig_result = bitmath.GiB(100)
for gi in other_inputs:
_parsed = bitmath.parse_string_unsafe(gi)
self.assertEqual(_parsed, expected_gig_result)
self.assertIs(type(_parsed), type(expected_gig_result))
def test_parse_string_unsafe_request_NIST(self):
"""parse_string_unsafe can convert to NIST on request"""
unsafe_input = "100M"
_parsed = bitmath.parse_string_unsafe(unsafe_input, system=bitmath.NIST)
expected = bitmath.MiB(100)
self.assertEqual(_parsed, expected)
self.assertIs(type(_parsed), type(expected))
unsafe_input2 = "100k"
_parsed2 = bitmath.parse_string_unsafe(unsafe_input2, system=bitmath.NIST)
expected2 = bitmath.KiB(100)
self.assertEqual(_parsed2, expected2)
self.assertIs(type(_parsed2), type(expected2))
unsafe_input3 = "100"
_parsed3 = bitmath.parse_string_unsafe(unsafe_input3, system=bitmath.NIST)
expected3 = bitmath.Byte(100)
self.assertEqual(_parsed3, expected3)
self.assertIs(type(_parsed3), type(expected3))
unsafe_input4 = "100kb"
_parsed4 = bitmath.parse_string_unsafe(unsafe_input4, system=bitmath.NIST)
expected4 = bitmath.KiB(100)
self.assertEqual(_parsed4, expected4)
self.assertIs(type(_parsed4), type(expected4))
######################################################################
def test_parse_string_unsafe_github_issue_60(self):
"""parse_string_unsafe can parse the examples reported in issue #60
https://github.com/tbielawa/bitmath/issues/60
"""
issue_input1 = '7.5KB'
_parsed1 = bitmath.parse_string_unsafe(issue_input1)
expected_result1 = bitmath.kB(7.5)
self.assertEqual(
_parsed1,
expected_result1)
issue_input2 = '4.7MB'
_parsed2 = bitmath.parse_string_unsafe(issue_input2)
expected_result2 = bitmath.MB(4.7)
self.assertEqual(
_parsed2,
expected_result2)
issue_input3 = '4.7M'
_parsed3 = bitmath.parse_string_unsafe(issue_input3)
expected_result3 = bitmath.MB(4.7)
self.assertEqual(
_parsed3,
expected_result3)
|