Skip to content

Commit 18757e1

Browse files
authored
PYTHON-5728 Improve bson code coverage (mongodb#2880)
1 parent 7923d29 commit 18757e1

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

test/test_bson.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,18 @@ def test_binary_length_accounts_for_header(self):
12751275
with self.assertRaises(InvalidBSON):
12761276
decode(payload)
12771277

1278+
def test_large_list_encoding(self):
1279+
# gen_list_name yields pre-cached names for indices 0-999 then
1280+
# generates on the fly for 1000+. Encode a list that crosses that
1281+
# boundary and verify the round-trip is correct.
1282+
values = list(range(1002))
1283+
decoded = decode(encode({"a": values}))
1284+
self.assertEqual(decoded["a"], values)
1285+
# Spot-check elements on both sides of the cache/on-demand boundary.
1286+
self.assertEqual(decoded["a"][999], 999)
1287+
self.assertEqual(decoded["a"][1000], 1000)
1288+
self.assertEqual(decoded["a"][1001], 1001)
1289+
12781290

12791291
class TestCodecOptions(unittest.TestCase):
12801292
def test_document_class(self):
@@ -1746,6 +1758,40 @@ def test_array_of_documents_to_buffer(self):
17461758
with self.assertRaises(InvalidBSON):
17471759
_array_of_documents_to_buffer(buf)
17481760

1761+
def test_datetime_ms_hash(self):
1762+
# Equal values must have equal hashes.
1763+
self.assertEqual(hash(DatetimeMS(0)), hash(DatetimeMS(0)))
1764+
self.assertEqual(hash(DatetimeMS(-1)), hash(DatetimeMS(-1)))
1765+
self.assertEqual(hash(DatetimeMS(2**62)), hash(DatetimeMS(2**62)))
1766+
# Usable as a dict key.
1767+
d = {DatetimeMS(0): "epoch", DatetimeMS(1): "one"}
1768+
self.assertEqual(d[DatetimeMS(0)], "epoch")
1769+
self.assertEqual(d[DatetimeMS(1)], "one")
1770+
# Usable in a set.
1771+
s = {DatetimeMS(0), DatetimeMS(1), DatetimeMS(0)}
1772+
self.assertEqual(len(s), 2)
1773+
1774+
def test_datetime_ms_repr(self):
1775+
self.assertEqual(repr(DatetimeMS(0)), "DatetimeMS(0)")
1776+
self.assertEqual(repr(DatetimeMS(-1)), "DatetimeMS(-1)")
1777+
self.assertEqual(repr(DatetimeMS(2**62)), f"DatetimeMS({2**62})")
1778+
# repr round-trips through eval.
1779+
for value in (0, 1, -1, 2**32):
1780+
obj = DatetimeMS(value)
1781+
self.assertEqual(eval(repr(obj)), obj)
1782+
1783+
def test_datetime_ms_invalid_type(self):
1784+
# Non-int, non-datetime arguments must raise TypeError.
1785+
for bad in ("2024-01-01", 3.14, [], None, b"bytes"):
1786+
with self.assertRaises(TypeError):
1787+
DatetimeMS(bad) # type: ignore[arg-type]
1788+
# Out-of-range int must raise OverflowError directly, not only
1789+
# when encoding.
1790+
with self.assertRaises(OverflowError):
1791+
DatetimeMS(2**63)
1792+
with self.assertRaises(OverflowError):
1793+
DatetimeMS(-(2**63) - 1)
1794+
17491795

17501796
class TestLongLongToString(unittest.TestCase):
17511797
def test_long_long_to_string(self):

0 commit comments

Comments
 (0)