Skip to content

Commit 9b632c7

Browse files
stephan-hofbehackett
authored andcommitted
Refactor the the assert into a separate function.
1 parent e113a33 commit 9b632c7

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

test/test_bson.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -652,51 +652,39 @@ def test_utf8(self):
652652

653653
# Verify that python and bson have the same understanding of
654654
# legal utf-8 if the first byte is 0xf4 (244)
655-
@staticmethod
656-
def _py_is_legal_utf8(x):
655+
def _assert_same_utf8_validation(self, data):
657656
try:
658-
x.decode('utf-8')
659-
return True
657+
data.decode('utf-8')
658+
py_is_legal = True
660659
except UnicodeDecodeError:
661-
return False
660+
py_is_legal = False
662661

663-
@staticmethod
664-
def _bson_is_legal_utf8(x):
665662
try:
666-
BSON.encode({'x': x})
667-
return True
663+
BSON.encode({'x': data})
664+
bson_is_legal = True
668665
except InvalidStringData:
669-
return False
666+
bson_is_legal = False
667+
668+
self.assertEqual(py_is_legal, bson_is_legal, data)
670669

671670
@unittest.skipIf(PY3, "python3 has strong separation between bytes/unicode")
672671
def test_legal_utf8_full_coverage(self):
673672
# this tests takes 400 seconds. Which is too long to run each time.
674673
# However it is the only one which covers all possible bit combinations
675674
# in the 244 space.
676-
677675
b1 = chr(0xf4)
678676

679677
for b2 in map(chr, range(255)):
680678
m2 = b1 + b2
681-
self.assertEqual(
682-
self._py_is_legal_utf8(m2),
683-
self._bson_is_legal_utf8(m2)
684-
)
679+
self._assert_same_utf8_validation(m2)
685680

686681
for b3 in map(chr, range(255)):
687682
m3 = m2 + b3
688-
self.assertEqual(
689-
self._py_is_legal_utf8(m3),
690-
self._bson_is_legal_utf8(m3)
691-
)
683+
self._assert_same_utf8_validation(m3)
692684

693685
for b4 in map(chr, range(255)):
694686
m4 = m3 + b4
695-
696-
self.assertEqual(
697-
self._py_is_legal_utf8(m4),
698-
self._bson_is_legal_utf8(m4)
699-
)
687+
self._assert_same_utf8_validation(m4)
700688

701689
# In python3:
702690
# - 'bytes' are not checked with isLegalutf
@@ -712,10 +700,7 @@ def test_legal_utf8_few_samples(self):
712700
]
713701

714702
for data in good_samples:
715-
self.assertEqual(
716-
self._py_is_legal_utf8(data),
717-
self._bson_is_legal_utf8(data)
718-
)
703+
self._assert_same_utf8_validation(data)
719704

720705
bad_samples = [
721706
'\xf4\x00\x80\x80',
@@ -726,12 +711,7 @@ def test_legal_utf8_few_samples(self):
726711
]
727712

728713
for data in bad_samples:
729-
self.assertEqual(
730-
self._py_is_legal_utf8(data),
731-
self._bson_is_legal_utf8(data),
732-
data
733-
)
734-
714+
self._assert_same_utf8_validation(data)
735715

736716
def test_null_character(self):
737717
doc = {"a": "\x00"}

0 commit comments

Comments
 (0)