-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_resource.py
182 lines (138 loc) · 4.39 KB
/
test_resource.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
import pytest
from pyramid_resource import Resource
def test_init():
class MyResource(Resource): ...
r = MyResource("request", "name", "parent")
assert r.request == "request"
assert r.__name__ == "name"
assert r.__parent__ == "parent"
def test_init_root():
class MyResource(Resource): ...
r = MyResource("request")
assert r.request == "request"
assert r.__name__ == ""
assert r.__parent__ is None
def test_init_direct():
with pytest.raises(TypeError) as e:
Resource("request")
assert str(e.value) == (
"Cannot instanciate `Resource` directly; please make a subclass."
)
def test_attach():
class MyResource(Resource): ...
r = MyResource()
assert not r.attached
r.attach("request", "name", "parent")
assert r.attached
assert r.request == "request"
assert r.__name__ == "name"
assert r.__parent__ == "parent"
def test_default_lookup():
class SubResource(Resource):
pass
class MyResource(Resource):
__children__ = {
"sub": SubResource,
}
root = MyResource("request")
sub = root["sub"]
assert isinstance(sub, SubResource)
assert sub.request == "request"
assert sub.__name__ == "sub"
assert sub.__parent__ is root
with pytest.raises(KeyError):
root["sub2"]
def test_custom_lookup_subclass():
class SubResource(Resource):
pass
class MyResource(Resource):
def get_child(self, key):
assert key == "sub"
return SubResource
root = MyResource("request")
sub = root["sub"]
assert isinstance(sub, SubResource)
assert sub.request == "request"
assert sub.__name__ == "sub"
assert sub.__parent__ is root
def test_custom_lookup_tuple():
class SubResource(Resource):
pass
class MyResource(Resource):
def get_child(self, key):
assert key == "sub"
return SubResource, {"foo": "bar"}
root = MyResource("request")
sub = root["sub"]
assert isinstance(sub, SubResource)
assert sub.request == "request"
assert sub.__name__ == "sub"
assert sub.__parent__ is root
assert sub.foo == "bar"
def test_custom_lookup_instance():
class SubResource(Resource):
pass
class MyResource(Resource):
def get_child(self, key):
assert key == "sub"
return SubResource(foo="bar")
root = MyResource("request")
sub = root["sub"]
assert isinstance(sub, SubResource)
assert sub.request == "request"
assert sub.__name__ == "sub"
assert sub.__parent__ is root
assert sub.foo == "bar"
def test_custom_lookup_none():
class SubResource(Resource):
pass
class MyResource(Resource):
def get_child(self, key):
assert key == "sub"
return None
root = MyResource("request")
with pytest.raises(KeyError):
root["sub"]
def test_custom_lookup_unknown():
class SubResource(Resource):
pass
class MyResource(Resource):
def get_child(self, key):
assert key == "sub"
return "foo"
root = MyResource("request")
with pytest.raises(ValueError) as e:
root["sub"]
assert str(e.value) == "Unexpected return value from `get_child`: 'foo'"
def test_getattr():
class SubResource(Resource):
pass
class MyResource(Resource):
subfoo = "subbar"
_foo = "_bar"
@property
def prop(self):
return "myprop"
parent = MyResource("request")
child = SubResource("request", "sub", parent, foo="bar")
grandchild = SubResource("request", "sub", child)
assert parent.subfoo == "subbar"
assert parent.prop == "myprop"
with pytest.raises(AttributeError) as exc:
assert parent.foo
assert str(exc.value) == "'MyResource' object has no attribute 'foo'"
assert parent._foo == "_bar"
assert child.foo == "bar"
assert child.subfoo == "subbar"
assert child.prop == "myprop"
# Don't proxy private attributes
with pytest.raises(AttributeError) as exc:
assert child._foo
assert str(exc.value) == (
"'SubResource' object has no attribute '_foo'. Note: Resource "
"objects do not proxy attributes prefixed with an underscore (i.e. "
"private attributes)."
)
assert grandchild.foo == "bar"
assert grandchild.subfoo == "subbar"
assert grandchild.prop == "myprop"