/
test_uritemplate.py
210 lines (155 loc) · 6.14 KB
/
test_uritemplate.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
""" tests for webdispatch.uritemplate """
from testfixtures import compare, ShouldRaise
class TestPatternToRegex(object):
""" test webdispatch.uritemplate.pattern_to_regex"""
@staticmethod
def _call_fut(*args, **kwargs):
""" call function under test """
from webdispatch.uritemplate import pattern_to_regex
return pattern_to_regex(*args, **kwargs)
def test_empty(self):
""" test converting empty string """
pattern = ""
result = self._call_fut(pattern)
compare(result, "^$")
def test_regex_meta_chars(self):
""" test converting included meta characters """
pattern = "{name}.{suffix}"
result = self._call_fut(pattern)
compare(result, r"^(?P<name>[\w-]+)\.(?P<suffix>[\w-]+)$")
def test_open_path(self):
""" test converting having open suffix"""
pattern = "hoge*"
result = self._call_fut(pattern)
compare(result, "^hoge")
def test_close_path(self):
""" test converting not having open suffix"""
pattern = "hoge"
result = self._call_fut(pattern)
compare(result, "^hoge$")
def test_one_var(self):
""" test converting including one var"""
pattern = "{var1}"
result = self._call_fut(pattern)
compare(result, r"^(?P<var1>[\w-]+)$")
def test_one_typed_var(self):
""" test converting including one var"""
pattern = "{var1:int}"
result = self._call_fut(pattern)
compare(result, r"^(?P<var1>[\w-]+)$")
def test_two_vars(self):
""" test converting including two vers"""
pattern = "{var1}{var2}"
result = self._call_fut(pattern)
compare(result, r"^(?P<var1>[\w-]+)(?P<var2>[\w-]+)$")
def test_vars(self):
""" test converting separated two vars """
pattern = "/{var1}/{var2}"
result = self._call_fut(pattern)
compare(result, r"^/(?P<var1>[\w-]+)/(?P<var2>[\w-]+)$")
class TestDetectConverters(object):
""" test for webdispatch.urltemplate.detect_converters"""
@staticmethod
def _call_fut(*args, **kwargs):
""" call function under test """
from webdispatch.uritemplate import detect_converters
return detect_converters(*args, **kwargs)
def test_empty(self):
""" test with empty string """
pattern = ""
result = self._call_fut(pattern, {})
compare(result, {})
def test_no_type(self):
""" test without specified converter """
pattern = "{a}"
result = self._call_fut(pattern, {})
compare(result, {'a': str})
def test_type(self):
""" test with specified converter """
pattern = "{a:int}"
result = self._call_fut(pattern, {'int': int})
compare(result, {'a': int})
class TestURITemplate(object):
""" test for webdispatch.uritemplate.URITemplate """
@staticmethod
def _get_target():
""" get class under test"""
from webdispatch.uritemplate import URITemplate
return URITemplate
def _make_one(self, *args, **kwargs):
""" create object under test"""
return self._get_target()(*args, **kwargs)
def test_bad_format(self):
""" test bad format template"""
from webdispatch.uritemplate import URITemplateFormatException
path = "a*"
with ShouldRaise(URITemplateFormatException):
self._make_one(path)
def test_match_empty(self):
""" test matching empty path """
path = ""
target = self._make_one(path)
result = target.match(path)
compare(result.matchdict, dict())
compare(result.matchlength, 0)
def test_wildcard(self):
""" test matching pattern including wildcard"""
path = "hoge/{var1}/*"
target = self._make_one(path)
result = target.match("hoge/egg/bacon")
compare(result.matchdict, dict(var1="egg"))
compare(result.matchlength, 9)
def test_match_no_match(self):
""" test no mathing"""
path = "hoge/{vars}"
target = self._make_one(path)
result = target.match("spam/egg")
compare(result, None)
def test_match_match_one(self):
""" test matching a character """
path = "{var1}"
target = self._make_one(path)
result = target.match("a")
compare(result.matchdict, dict(var1="a"))
compare(result.matchlength, 1)
def test_match_match_complex_word(self):
""" test matching a string"""
path = "{var1}"
target = self._make_one(path)
result = target.match("abc")
compare(result.matchdict, dict(var1="abc"))
def test_match_match_many(self):
""" test matching pattern including two vars """
path = "{var1}/users/{var2}"
target = self._make_one(path)
result = target.match("a/users/egg")
compare(result.matchdict, dict(var1="a", var2="egg"))
def test_match_conveter(self):
""" test matching pattern including specified converter """
path = "{var1:int}/users/{var2}"
target = self._make_one(path)
result = target.match("1/users/egg")
compare(result.matchdict, dict(var1=1, var2="egg"))
def test_match_conveter_error(self):
""" test matching pattern including specified converter """
path = "{var1:int}/users/{var2}"
target = self._make_one(path)
result = target.match("a/users/egg")
compare(result, None)
def test_match_custom_conveter(self):
""" test using custom converter """
from datetime import datetime
converters = {
"testing": lambda s: datetime.strptime(s, '%Y%m%d')
}
path = "{var1}/users/{var2:testing}"
target = self._make_one(path, converters=converters)
result = target.match("1/users/20140420")
compare(result.matchdict,
dict(var1="1", var2=datetime(2014, 4, 20)))
def test_substitue(self):
""" test subtituting vars to pattern """
path = "{var1}/users/{var2}"
target = self._make_one(path)
result = target.substitute(dict(var1="x", var2="y"))
compare(result, "x/users/y")