8000 Add some test for timestamp (#403) · Anthchirp/msgpack-python@9d79351 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d79351

Browse files
authored
Add some test for timestamp (msgpack#403)
1 parent ff1f5f8 commit 9d79351

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

.github/workflows/black.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
- name: Black Code Formatter
1919
run: |
2020
pip install black
21-
black --diff --check msgpack/ test/
21+
black --diff --check msgpack/ test/ setup.py

msgpack/fallback.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ def getvalue(self):
7777
newlist_hint = lambda size: []
7878

7979

80-
from .exceptions import (
81-
BufferFull,
82-
OutOfData,
83-
ExtraData,
84-
FormatError,
85-
StackError,
86-
)
80+
from .exceptions import BufferFull, OutOfData, ExtraData, FormatError, StackError
8781

8882
from .ext import ExtType, Timestamp
8983

test/test_format.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,24 @@ def check(src, should, use_list=0, raw=True):
99

1010

1111
def testSimpleValue():
12-
check(b"\x93\xc0\xc2\xc3", (None, False, True,))
12+
check(b"\x93\xc0\xc2\xc3", (None, False, True))
1313

1414

1515
def testFixnum():
16-
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff", ((0, 64, 127,), (-32, -16, -1,),))
16+
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff", ((0, 64, 127), (-32, -16, -1)))
1717

1818

1919
def testFixArray():
20-
check(
21-
b"\x92\x90\x91\x91\xc0", ((), ((None,),),),
22-
)
20+
check(b"\x92\x90\x91\x91\xc0", ((), ((None,),)))
2321

2422

2523
def testFixRaw():
26-
check(
27-
b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def",),
28-
)
24+
check(b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def"))
2925

3026

3127
def testFixMap():
3228
check(
33-
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
34-
{False: {None: None}, True: {None: {}}},
29+
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80", {False: {None: None}, True: {None: {}}}
3530
)
3631

3732

@@ -40,7 +35,7 @@ def testUnsignedInt():
4035
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
4136
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
4237
b"\xce\xff\xff\xff\xff",
43-
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
38+
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295),
4439
)
4540

4641

@@ -49,7 +44,7 @@ def testSignedInt():
4944
b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
5045
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
5146
b"\xd2\xff\xff\xff\xff",
52-
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,),
47+
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1),
5348
)
5449

5550

test/test_pack.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def testPack():
4949
False,
5050
(),
5151
((),),
52-
((), None,),
52+
((), None),
5353
{None: 0},
5454
(1 << 23),
5555
]
@@ -69,21 +69,13 @@ def testPackUnicode():
6969

7070

7171
def testPackBytes():
72-
test_data = [
73-
b"",
74-
b"abcd",
75-
(b"defgh",),
76-
]
72+
test_data = [b"", b"abcd", (b"defgh",)]
7773
for td in test_data:
7874
check(td)
7975

8076

8177
def testPackByteArrays():
82-
test_data = [
83-
bytearray(b""),
84-
bytearray(b"abcd"),
85-
(bytearray(b"defgh"),),
86-
]
78+
test_data = [bytearray(b""), bytearray(b"abcd"), (bytearray(b"defgh"),)]
8779
for td in test_data:
8880
check(td)
8981

test/test_stricttype.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ def test_tuple():
2222

2323
def default(o):
2424
if isinstance(o, tuple):
25-
return {
26-
"__type__": "tuple",
27-
"value": list(o),
28-
}
25+
return {"__type__": "tuple", "value": list(o)}
2926
raise TypeError("Unsupported type %s" % (type(o),))
3027

3128
def convert(o):

test/test_timestamp.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ def test_timestamp():
4646
assert ts == unpacked
4747

4848

49+
def test_unpack_timestamp():
50+
# timestamp 32
51+
assert msgpack.unpackb(b"\xd6\xff\x00\x00\x00\x00") == Timestamp(0)
52+
53+
# timestamp 64
54+
assert msgpack.unpackb(b"\xd7\xff" + b"\x00" * 8) == Timestamp(0)
55+
with pytest.raises(ValueError):
56+
msgpack.unpackb(b"\xd7\xff" + b"\xff" * 8)
57+
58+
# timestamp 96
59+
assert msgpack.unpackb(b"\xc7\x0c\xff" + b"\x00" * 12) == Timestamp(0)
60+
with pytest.raises(ValueError):
61+
msgpack.unpackb(b"\xc7\x0c\xff" + b"\xff" * 12) == Timestamp(0)
62+
63+
# Undefined
64+
with pytest.raises(ValueError):
65+
msgpack.unpackb(b"\xd4\xff\x00") # fixext 1
66+
with pytest.raises(ValueError):
67+
msgpack.unpackb(b"\xd5\xff\x00\x00") # fixext 2
68+
with pytest.raises(ValueError):
69+
msgpack.unpackb(b"\xc7\x00\xff") # ext8 (len=0)
70+
with pytest.raises(ValueError):
71+
msgpack.unpackb(b"\xc7\x03\xff\0\0\0") # ext8 (len=3)
72+
with pytest.raises(ValueError):
73+
msgpack.unpackb(b"\xc7\x05\xff\0\0\0\0\0") # ext8 (len=5)
74+
75+
4976
def test_timestamp_from():
5077
t = Timestamp(42, 14000)
5178
assert Timestamp.from_unix(42.000014) == t

0 commit comments

Comments
 (0)
0