Skip to content

Commit 240536a

Browse files
committed
Split test_parse.py into strict and unsafe halves
The parse_string refactor made the split obvious. TestParseStrict covers the default strict=True path plus public entry-point tests (including parse_string_unsafe's deprecation shim). TestParseUnsafe covers strict=False. 314 passing, 2 skipped. Up from 303 before this branch.
1 parent d024ad9 commit 240536a

2 files changed

Lines changed: 181 additions & 145 deletions

File tree

tests/test_parse_strict.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# -*- coding: utf-8 -*-
2+
# SPDX-License-Identifier: MIT
3+
# The MIT License (MIT)
4+
#
5+
# Copyright © 2014 Tim Case <timbielawa@gmail.com>
6+
#
7+
# Permission is hereby granted, free of charge, to any person
8+
# obtaining a copy of this software and associated documentation files
9+
# (the "Software"), to deal in the Software without restriction,
10+
# including without limitation the rights to use, copy, modify, merge,
11+
# publish, distribute, sublicense, and/or sell copies of the Software,
12+
# and to permit persons to whom the Software is furnished to do so,
13+
# subject to the following conditions:
14+
#
15+
# The above copyright notice and this permission notice shall be
16+
# included in all copies or substantial portions of the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
27+
28+
"""
29+
Tests for parse_string (strict=True, the default) and public entry-point behaviour.
30+
"""
31+
32+
import warnings
33+
34+
from . import TestCase
35+
import bitmath
36+
37+
38+
class TestParseStrict(TestCase):
39+
def test_parse_b(self):
40+
"""parse_string works on bit strings"""
41+
self.assertEqual(
42+
bitmath.parse_string("123b"),
43+
bitmath.Bit(123))
44+
45+
def test_parse_B(self):
46+
"""parse_string works on byte strings"""
47+
self.assertEqual(
48+
bitmath.parse_string("321B"),
49+
bitmath.Byte(321))
50+
51+
def test_parse_Gb(self):
52+
"""parse_string works on gigabit strings"""
53+
self.assertEqual(
54+
bitmath.parse_string("456Gb"),
55+
bitmath.Gb(456))
56+
57+
def test_parse_MiB(self):
58+
"""parse_string works on mebibyte strings"""
59+
self.assertEqual(
60+
bitmath.parse_string("654 MiB"),
61+
bitmath.MiB(654))
62+
63+
######################################################################
64+
# NIST 'octet' based units
65+
def test_parse_Mio(self):
66+
"""parse_string works on mebioctet strings"""
67+
self.assertEqual(
68+
bitmath.parse_string("654 Mio"),
69+
bitmath.MiB(654))
70+
71+
def test_parse_Eio(self):
72+
"""parse_string works on exbioctet strings"""
73+
self.assertEqual(
74+
bitmath.parse_string("654 Eio"),
75+
bitmath.EiB(654))
76+
77+
def test_parse_Zio(self):
78+
"""parse_string works on zebioctet strings"""
79+
self.assertEqual(
80+
bitmath.parse_string("654 Zio"),
81+
bitmath.ZiB(654))
82+
83+
def test_parse_Yio(self):
84+
"""parse_string works on yobioctet strings"""
85+
self.assertEqual(
86+
bitmath.parse_string("654 Yio"),
87+
bitmath.YiB(654))
88+
89+
# SI 'octet' based units
90+
def test_parse_Mo(self):
91+
"""parse_string works on megaoctet strings"""
92+
self.assertEqual(
93+
bitmath.parse_string("654 Mo"),
94+
bitmath.MB(654))
95+
96+
def test_parse_Eo(self):
97+
"""parse_string works on exaoctet strings"""
98+
self.assertEqual(
99+
bitmath.parse_string("654 Eo"),
100+
bitmath.EB(654))
101+
102+
def test_parse_Zo(self):
103+
"""parse_string works on zettaoctet strings"""
104+
self.assertEqual(
105+
bitmath.parse_string("654 Zo"),
106+
bitmath.ZB(654))
107+
108+
def test_parse_Yo(self):
109+
"""parse_string works on yottaoctet strings"""
110+
self.assertEqual(
111+
bitmath.parse_string("654 Yo"),
112+
bitmath.YB(654))
113+
114+
######################################################################
115+
116+
def test_parse_bad_float(self):
117+
"""parse_string can identify invalid float values"""
118+
with self.assertRaises(ValueError):
119+
bitmath.parse_string("1.23.45 kb")
120+
121+
def test_parse_bad_unit(self):
122+
"""parse_string can identify invalid prefix units"""
123+
with self.assertRaises(ValueError):
124+
bitmath.parse_string("1.23 GIB")
125+
126+
def test_parse_bad_unit2(self):
127+
"""parse_string can identify other prefix units"""
128+
with self.assertRaises(ValueError):
129+
bitmath.parse_string("1.23 QB")
130+
131+
def test_parse_no_unit(self):
132+
"""parse_string can identify strings without units at all"""
133+
with self.assertRaises(ValueError):
134+
bitmath.parse_string("12345")
135+
136+
def test_parse_string_non_string_input(self):
137+
"""parse_string can identify a non-string input"""
138+
with self.assertRaises(ValueError):
139+
bitmath.parse_string(12345)
140+
141+
def test_parse_string_unicode(self):
142+
"""parse_string can handle a unicode string"""
143+
self.assertEqual(
144+
bitmath.parse_string(u"750 GiB"),
145+
bitmath.GiB(750))
146+
147+
######################################################################
148+
# Deprecated public entry point tests
149+
150+
def test_parse_string_unsafe_deprecation_warning(self):
151+
"""parse_string_unsafe emits DeprecationWarning as of 2.0.0"""
152+
with warnings.catch_warnings(record=True) as w:
153+
warnings.simplefilter("always")
154+
bitmath.parse_string_unsafe("100 GiB")
155+
self.assertEqual(len(w), 1)
156+
self.assertTrue(issubclass(w[0].category, DeprecationWarning))
157+
self.assertIn("2.0.0", str(w[0].message))
158+
self.assertIn("parse_string", str(w[0].message))
159+
160+
def test_parse_string_unsafe_request_NIST(self):
161+
"""parse_string_unsafe still delegates correctly with explicit system"""
162+
with warnings.catch_warnings(record=True):
163+
warnings.simplefilter("always")
164+
165+
_parsed = bitmath.parse_string_unsafe("100M", system=bitmath.NIST)
166+
self.assertEqual(_parsed, bitmath.MiB(100))
167+
self.assertIs(type(_parsed), bitmath.MiB)
168+
169+
_parsed2 = bitmath.parse_string_unsafe("100k", system=bitmath.NIST)
170+
self.assertEqual(_parsed2, bitmath.KiB(100))
171+
self.assertIs(type(_parsed2), bitmath.KiB)
172+
173+
_parsed3 = bitmath.parse_string_unsafe("100", system=bitmath.NIST)
174+
self.assertEqual(_parsed3, bitmath.Byte(100))
175+
self.assertIs(type(_parsed3), bitmath.Byte)
176+
177+
_parsed4 = bitmath.parse_string_unsafe("100kb", system=bitmath.NIST)
178+
self.assertEqual(_parsed4, bitmath.KiB(100))
179+
self.assertIs(type(_parsed4), bitmath.KiB)
Lines changed: 2 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -26,124 +26,14 @@
2626

2727

2828
"""
29-
Test parsing strings into bitmath objects
29+
Tests for parse_string with strict=False (the non-strict / formerly unsafe path).
3030
"""
3131

3232
from . import TestCase
3333
import bitmath
3434

3535

36-
class TestParse(TestCase):
37-
def test_parse_b(self):
38-
"""parse_string works on bit strings"""
39-
self.assertEqual(
40-
bitmath.parse_string("123b"),
41-
bitmath.Bit(123))
42-
43-
def test_parse_B(self):
44-
"""parse_string works on byte strings"""
45-
self.assertEqual(
46-
bitmath.parse_string("321B"),
47-
bitmath.Byte(321))
48-
49-
def test_parse_Gb(self):
50-
"""parse_string works on gigabit strings"""
51-
self.assertEqual(
52-
bitmath.parse_string("456Gb"),
53-
bitmath.Gb(456))
54-
55-
def test_parse_MiB(self):
56-
"""parse_string works on mebibyte strings"""
57-
self.assertEqual(
58-
bitmath.parse_string("654 MiB"),
59-
bitmath.MiB(654))
60-
61-
######################################################################
62-
# NIST 'octet' based units
63-
def test_parse_Mio(self):
64-
"""parse_string works on mebioctet strings"""
65-
self.assertEqual(
66-
bitmath.parse_string("654 Mio"),
67-
bitmath.MiB(654))
68-
69-
def test_parse_Eio(self):
70-
"""parse_string works on exbioctet strings"""
71-
self.assertEqual(
72-
bitmath.parse_string("654 Eio"),
73-
bitmath.EiB(654))
74-
75-
def test_parse_Zio(self):
76-
"""parse_string works on zebioctet strings"""
77-
self.assertEqual(
78-
bitmath.parse_string("654 Zio"),
79-
bitmath.ZiB(654))
80-
81-
def test_parse_Yio(self):
82-
"""parse_string works on yobioctet strings"""
83-
self.assertEqual(
84-
bitmath.parse_string("654 Yio"),
85-
bitmath.YiB(654))
86-
87-
# SI 'octet' based units
88-
def test_parse_Mo(self):
89-
"""parse_string works on megaoctet strings"""
90-
self.assertEqual(
91-
bitmath.parse_string("654 Mo"),
92-
bitmath.MB(654))
93-
94-
def test_parse_Eo(self):
95-
"""parse_string works on exaoctet strings"""
96-
self.assertEqual(
97-
bitmath.parse_string("654 Eo"),
98-
bitmath.EB(654))
99-
100-
def test_parse_Zo(self):
101-
"""parse_string works on zettaoctet strings"""
102-
self.assertEqual(
103-
bitmath.parse_string("654 Zo"),
104-
bitmath.ZB(654))
105-
106-
def test_parse_Yo(self):
107-
"""parse_string works on yottaoctet strings"""
108-
self.assertEqual(
109-
bitmath.parse_string("654 Yo"),
110-
bitmath.YB(654))
111-
112-
######################################################################
113-
114-
def test_parse_bad_float(self):
115-
"""parse_string can identify invalid float values"""
116-
with self.assertRaises(ValueError):
117-
bitmath.parse_string("1.23.45 kb")
118-
119-
def test_parse_bad_unit(self):
120-
"""parse_string can identify invalid prefix units"""
121-
with self.assertRaises(ValueError):
122-
bitmath.parse_string("1.23 GIB")
123-
124-
def test_parse_bad_unit2(self):
125-
"""parse_string can identify other prefix units"""
126-
with self.assertRaises(ValueError):
127-
bitmath.parse_string("1.23 QB")
128-
129-
def test_parse_no_unit(self):
130-
"""parse_string can identify strings without units at all"""
131-
with self.assertRaises(ValueError):
132-
bitmath.parse_string("12345")
133-
134-
def test_parse_string_non_string_input(self):
135-
"""parse_string can identify a non-string input"""
136-
with self.assertRaises(ValueError):
137-
bitmath.parse_string(12345)
138-
139-
def test_parse_string_unicode(self):
140-
"""parse_string can handle a unicode string"""
141-
self.assertEqual(
142-
bitmath.parse_string(u"750 GiB"),
143-
bitmath.GiB(750))
144-
145-
######################################################################
146-
36+
class TestParseUnsafe(TestCase):
14737
def test_parse_non_strict_bad_input_type(self):
14838
"""parse_string strict=False can identify invalid input types"""
14939
with self.assertRaises(ValueError):
@@ -324,36 +214,3 @@ def test_parse_non_strict_byte_word_forms(self):
324214
result = bitmath.parse_string(f"42 {unit}", strict=False)
325215
self.assertEqual(result, expected, msg=f"Failed for unit '{unit}'")
326216
self.assertIs(type(result), bitmath.Byte, msg=f"Wrong type for unit '{unit}'")
327-
328-
def test_parse_string_unsafe_deprecation_warning(self):
329-
"""parse_string_unsafe emits DeprecationWarning as of 2.0.0"""
330-
import warnings
331-
with warnings.catch_warnings(record=True) as w:
332-
warnings.simplefilter("always")
333-
bitmath.parse_string_unsafe("100 GiB")
334-
self.assertEqual(len(w), 1)
335-
self.assertTrue(issubclass(w[0].category, DeprecationWarning))
336-
self.assertIn("2.0.0", str(w[0].message))
337-
self.assertIn("parse_string", str(w[0].message))
338-
339-
def test_parse_string_unsafe_request_NIST(self):
340-
"""parse_string_unsafe still delegates correctly with explicit system"""
341-
import warnings
342-
with warnings.catch_warnings(record=True):
343-
warnings.simplefilter("always")
344-
345-
_parsed = bitmath.parse_string_unsafe("100M", system=bitmath.NIST)
346-
self.assertEqual(_parsed, bitmath.MiB(100))
347-
self.assertIs(type(_parsed), bitmath.MiB)
348-
349-
_parsed2 = bitmath.parse_string_unsafe("100k", system=bitmath.NIST)
350-
self.assertEqual(_parsed2, bitmath.KiB(100))
351-
self.assertIs(type(_parsed2), bitmath.KiB)
352-
353-
_parsed3 = bitmath.parse_string_unsafe("100", system=bitmath.NIST)
354-
self.assertEqual(_parsed3, bitmath.Byte(100))
355-
self.assertIs(type(_parsed3), bitmath.Byte)
356-
357-
_parsed4 = bitmath.parse_string_unsafe("100kb", system=bitmath.NIST)
358-
self.assertEqual(_parsed4, bitmath.KiB(100))
359-
self.assertIs(type(_parsed4), bitmath.KiB)

0 commit comments

Comments
 (0)