summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInada Naoki <songofacandy@gmail.com>2020-02-06 22:11:04 +0900
committerGitHub <noreply@github.com>2020-02-06 22:11:04 +0900
commit9d79351e99e435b8ca749d57a313441783f67133 (patch)
tree06d165456f90898aa74048ec86319213b35aff1f
parentff1f5f89d997b40e60472c2820ea55cab752c779 (diff)
downloadmsgpack-python-9d79351e99e435b8ca749d57a313441783f67133.tar.gz
Add some test for timestamp (#403)
-rw-r--r--.github/workflows/black.yaml2
-rw-r--r--msgpack/fallback.py8
-rw-r--r--test/test_format.py19
-rw-r--r--test/test_pack.py14
-rw-r--r--test/test_stricttype.py5
-rw-r--r--test/test_timestamp.py27
6 files changed, 40 insertions, 35 deletions
diff --git a/.github/workflows/black.yaml b/.github/workflows/black.yaml
index cabd0cc..eda8d07 100644
--- a/.github/workflows/black.yaml
+++ b/.github/workflows/black.yaml
@@ -18,4 +18,4 @@ jobs:
- name: Black Code Formatter
run: |
pip install black
- black --diff --check msgpack/ test/
+ black --diff --check msgpack/ test/ setup.py
diff --git a/msgpack/fallback.py b/msgpack/fallback.py
index 263e74e..9f6665b 100644
--- a/msgpack/fallback.py
+++ b/msgpack/fallback.py
@@ -77,13 +77,7 @@ else:
newlist_hint = lambda size: []
-from .exceptions import (
- BufferFull,
- OutOfData,
- ExtraData,
- FormatError,
- StackError,
-)
+from .exceptions import BufferFull, OutOfData, ExtraData, FormatError, StackError
from .ext import ExtType, Timestamp
diff --git a/test/test_format.py b/test/test_format.py
index d455f7c..fbbc3f9 100644
--- a/test/test_format.py
+++ b/test/test_format.py
@@ -9,29 +9,24 @@ def check(src, should, use_list=0, raw=True):
def testSimpleValue():
- check(b"\x93\xc0\xc2\xc3", (None, False, True,))
+ check(b"\x93\xc0\xc2\xc3", (None, False, True))
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():
- check(
- b"\x92\x90\x91\x91\xc0", ((), ((None,),),),
- )
+ check(b"\x92\x90\x91\x91\xc0", ((), ((None,),)))
def testFixRaw():
- check(
- b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def",),
- )
+ check(b"\x94\xa0\xa1a\xa2bc\xa3def", (b"", b"a", b"bc", b"def"))
def testFixMap():
check(
- b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
- {False: {None: None}, True: {None: {}}},
+ b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80", {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"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
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"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
b"\xd2\xff\xff\xff\xff",
- (0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,),
+ (0, -128, -1, 0, -32768, -1, 0, -2147483648, -1),
)
diff --git a/test/test_pack.py b/test/test_pack.py
index 932f760..a51d84c 100644
--- a/test/test_pack.py
+++ b/test/test_pack.py
@@ -49,7 +49,7 @@ def testPack():
False,
(),
((),),
- ((), None,),
+ ((), None),
{None: 0},
(1 << 23),
]
@@ -69,21 +69,13 @@ def testPackUnicode():
def testPackBytes():
- test_data = [
- b"",
- b"abcd",
- (b"defgh",),
- ]
+ test_data = [b"", b"abcd", (b"defgh",)]
for td in test_data:
check(td)
def testPackByteArrays():
- test_data = [
- bytearray(b""),
- bytearray(b"abcd"),
- (bytearray(b"defgh"),),
- ]
+ test_data = [bytearray(b""), bytearray(b"abcd"), (bytearray(b"defgh"),)]
for td in test_data:
check(td)
diff --git a/test/test_stricttype.py b/test/test_stricttype.py
index 78e1723..fe9ec6c 100644
--- a/test/test_stricttype.py
+++ b/test/test_stricttype.py
@@ -22,10 +22,7 @@ def test_tuple():
def default(o):
if isinstance(o, tuple):
- return {
- "__type__": "tuple",
- "value": list(o),
- }
+ return {"__type__": "tuple", "value": list(o)}
raise TypeError("Unsupported type %s" % (type(o),))
def convert(o):
diff --git a/test/test_timestamp.py b/test/test_timestamp.py
index ba5611c..823fe04 100644
--- a/test/test_timestamp.py
+++ b/test/test_timestamp.py
@@ -46,6 +46,33 @@ def test_timestamp():
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():
t = Timestamp(42, 14000)
assert Timestamp.from_unix(42.000014) == t