mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 12:23:16 +00:00
Add some test for timestamp (#403)
This commit is contained in:
parent
ff1f5f89d9
commit
9d79351e99
6 changed files with 40 additions and 35 deletions
2
.github/workflows/black.yaml
vendored
2
.github/workflows/black.yaml
vendored
|
@ -18,4 +18,4 @@ jobs:
|
||||||
- name: Black Code Formatter
|
- name: Black Code Formatter
|
||||||
run: |
|
run: |
|
||||||
pip install black
|
pip install black
|
||||||
black --diff --check msgpack/ test/
|
black --diff --check msgpack/ test/ setup.py
|
||||||
|
|
|
@ -77,13 +77,7 @@ else:
|
||||||
newlist_hint = lambda size: []
|
newlist_hint = lambda size: []
|
||||||
|
|
||||||
|
|
||||||
from .exceptions import (
|
from .exceptions import BufferFull, OutOfData, ExtraData, FormatError, StackError
|
||||||
BufferFull,
|
|
||||||
OutOfData,
|
|
||||||
ExtraData,
|
|
||||||
FormatError,
|
|
||||||
StackError,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .ext import ExtType, Timestamp
|
from .ext import ExtType, Timestamp
|
||||||
|
|
||||||
|
|
|
@ -9,29 +9,24 @@ def check(src, should, use_list=0, raw=True):
|
||||||
|
|
||||||
|
|
||||||
def testSimpleValue():
|
def testSimpleValue():
|
||||||
check(b"\x93\xc0\xc2\xc3", (None, False, True,))
|
check(b"\x93\xc0\xc2\xc3", (None, False, True))
|
||||||
|
|
||||||
|
|
||||||
def testFixnum():
|
def testFixnum():
|
||||||
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff", ((0, 64, 127,), (-32, -16, -1,),))
|
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff", ((0, 64, 127), (-32, -16, -1)))
|
||||||
|
|
||||||
|
|
||||||
def testFixArray():
|
def testFixArray():
|
||||||
check(
|
check(b"\x92\x90\x91\x91\xc0", ((), ((None,),)))
|
||||||
b"\x92\x90\x91\x91\xc0", ((), ((None,),),),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def testFixRaw():
|
def testFixRaw():
|
||||||
check(
|
check(b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def"))
|
||||||
b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def",),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def testFixMap():
|
def testFixMap():
|
||||||
check(
|
check(
|
||||||
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
|
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80", {False: {None: None}, True: {None: {}}}
|
||||||
{False: {None: None}, True: {None: {}}},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,7 +35,7 @@ def testUnsignedInt():
|
||||||
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
|
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
|
||||||
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
|
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
|
||||||
b"\xce\xff\xff\xff\xff",
|
b"\xce\xff\xff\xff\xff",
|
||||||
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
|
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,7 +44,7 @@ def testSignedInt():
|
||||||
b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
|
b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
|
||||||
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
|
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
|
||||||
b"\xd2\xff\xff\xff\xff",
|
b"\xd2\xff\xff\xff\xff",
|
||||||
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,),
|
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ def testPack():
|
||||||
False,
|
False,
|
||||||
(),
|
(),
|
||||||
((),),
|
((),),
|
||||||
((), None,),
|
((), None),
|
||||||
{None: 0},
|
{None: 0},
|
||||||
(1 << 23),
|
(1 << 23),
|
||||||
]
|
]
|
||||||
|
@ -69,21 +69,13 @@ def testPackUnicode():
|
||||||
|
|
||||||
|
|
||||||
def testPackBytes():
|
def testPackBytes():
|
||||||
test_data = [
|
test_data = [b"", b"abcd", (b"defgh",)]
|
||||||
b"",
|
|
||||||
b"abcd",
|
|
||||||
(b"defgh",),
|
|
||||||
]
|
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
check(td)
|
check(td)
|
||||||
|
|
||||||
|
|
||||||
def testPackByteArrays():
|
def testPackByteArrays():
|
||||||
test_data = [
|
test_data = [bytearray(b""), bytearray(b"abcd"), (bytearray(b"defgh"),)]
|
||||||
bytearray(b""),
|
|
||||||
bytearray(b"abcd"),
|
|
||||||
(bytearray(b"defgh"),),
|
|
||||||
]
|
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
check(td)
|
check(td)
|
||||||
|
|
||||||
|
|
|
@ -22,10 +22,7 @@ def test_tuple():
|
||||||
|
|
||||||
def default(o):
|
def default(o):
|
||||||
if isinstance(o, tuple):
|
if isinstance(o, tuple):
|
||||||
return {
|
return {"__type__": "tuple", "value": list(o)}
|
||||||
"__type__": "tuple",
|
|
||||||
"value": list(o),
|
|
||||||
}
|
|
||||||
raise TypeError("Unsupported type %s" % (type(o),))
|
raise TypeError("Unsupported type %s" % (type(o),))
|
||||||
|
|
||||||
def convert(o):
|
def convert(o):
|
||||||
|
|
|
@ -46,6 +46,33 @@ def test_timestamp():
|
||||||
assert ts == unpacked
|
assert ts == unpacked
|
||||||
|
|
||||||
|
|
||||||
|
def test_unpack_timestamp():
|
||||||
|
# timestamp 32
|
||||||
|
assert msgpack.unpackb(b"\xd6\xff\x00\x00\x00\x00") == Timestamp(0)
|
||||||
|
|
||||||
|
# timestamp 64
|
||||||
|
assert msgpack.unpackb(b"\xd7\xff" + b"\x00" * 8) == Timestamp(0)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xd7\xff" + b"\xff" * 8)
|
||||||
|
|
||||||
|
# timestamp 96
|
||||||
|
assert msgpack.unpackb(b"\xc7\x0c\xff" + b"\x00" * 12) == Timestamp(0)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xc7\x0c\xff" + b"\xff" * 12) == Timestamp(0)
|
||||||
|
|
||||||
|
# Undefined
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xd4\xff\x00") # fixext 1
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xd5\xff\x00\x00") # fixext 2
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xc7\x00\xff") # ext8 (len=0)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xc7\x03\xff\0\0\0") # ext8 (len=3)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
msgpack.unpackb(b"\xc7\x05\xff\0\0\0\0\0") # ext8 (len=5)
|
||||||
|
|
||||||
|
|
||||||
def test_timestamp_from():
|
def test_timestamp_from():
|
||||||
t = Timestamp(42, 14000)
|
t = Timestamp(42, 14000)
|
||||||
assert Timestamp.from_unix(42.000014) == t
|
assert Timestamp.from_unix(42.000014) == t
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue