-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_xid.py
71 lines (46 loc) · 1.55 KB
/
test_xid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import Union, Optional
from epyxid import xid_from_bytes, xid_create, XID, XIDError, xid_from_str
from pytest import raises, param, mark
XID_STR = '9m4e2mr0ui3e8a215n4g'
XID_BYTES = bytes([0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9])
def test_create_func() -> None:
xid = xid_create()
assert xid is not None
def test_create_cls() -> None:
xid = XID()
assert xid is not None
@mark.parametrize(
('value',),
[
param(None),
param(XID_BYTES),
param(XID_STR),
],
)
def test_create_cls_param(value: Optional[Union[str, bytes]], ) -> None:
xid = XID(value)
assert xid is not None
def test_create_cls_type_error() -> None:
with raises(TypeError):
xid = XID(42)
def test_create_cls_xid_error() -> None:
with raises(XIDError):
xid = XID('42')
def test_from_str_valid() -> None:
xid = xid_from_str(XID_STR)
assert bytes(xid) == XID_BYTES
def test_from_str_invalid_length() -> None:
with raises(ValueError):
xid_from_str('9m4e2mr0ui3e8a215n4')
def test_from_str_invalid_char() -> None:
with raises(ValueError):
xid_from_str('9z4e2mr0ui3e8a215n4g')
def test_from_bytes_valid() -> None:
xid = xid_from_bytes(XID_BYTES)
assert str(xid) == XID_STR
def test_hash() -> None:
xid = xid_from_bytes(XID_BYTES)
assert isinstance(hash(xid), int)
def test_from_bytes_invalid_length() -> None:
with raises(ValueError):
xid_from_bytes(bytes([0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d]))