-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_simple.py
50 lines (42 loc) · 1.49 KB
/
test_simple.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
import re
import uuid
import pytest
from reactpy_router.simple import parse_path
def test_parse_path():
assert parse_path("/a/b/c") == (re.compile("^/a/b/c$"), {})
assert parse_path("/a/{b}/c") == (
re.compile(r"^/a/(?P<b>[^/]+)/c$"),
{"b": str},
)
assert parse_path("/a/{b:int}/c") == (
re.compile(r"^/a/(?P<b>\d+)/c$"),
{"b": int},
)
assert parse_path("/a/{b:int}/{c:float}/c") == (
re.compile(r"^/a/(?P<b>\d+)/(?P<c>\d+(\.\d+)?)/c$"),
{"b": int, "c": float},
)
assert parse_path("/a/{b:int}/{c:float}/{d:uuid}/c") == (
re.compile(
r"^/a/(?P<b>\d+)/(?P<c>\d+(\.\d+)?)/(?P<d>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-["
r"0-9a-f]{4}-[0-9a-f]{12})/c$"
),
{"b": int, "c": float, "d": uuid.UUID},
)
assert parse_path("/a/{b:int}/{c:float}/{d:uuid}/{e:path}/c") == (
re.compile(
r"^/a/(?P<b>\d+)/(?P<c>\d+(\.\d+)?)/(?P<d>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-["
r"0-9a-f]{4}-[0-9a-f]{12})/(?P<e>.+)/c$"
),
{"b": int, "c": float, "d": uuid.UUID, "e": str},
)
def test_parse_path_unkown_conversion():
with pytest.raises(ValueError):
parse_path("/a/{b:unknown}/c")
def test_parse_path_re_escape():
"""Check that we escape regex characters in the path"""
assert parse_path("/a/{b:int}/c.d") == (
# ^ regex character
re.compile(r"^/a/(?P<b>\d+)/c\.d$"),
{"b": int},
)