-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest__helpers.py
More file actions
226 lines (185 loc) · 8.1 KB
/
Copy pathtest__helpers.py
File metadata and controls
226 lines (185 loc) · 8.1 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import unittest2
class Test_PropertyMixin(unittest2.TestCase):
def _getTargetClass(self):
from gcloud.storage._helpers import _PropertyMixin
return _PropertyMixin
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _derivedClass(self, connection=None, path=None, **custom_fields):
class Derived(self._getTargetClass()):
CUSTOM_PROPERTY_ACCESSORS = custom_fields
@property
def connection(self):
return connection
@property
def path(self):
return path
return Derived
def test_connetction_is_abstract(self):
mixin = self._makeOne()
self.assertRaises(NotImplementedError, lambda: mixin.connection)
def test_path_is_abstract(self):
mixin = self._makeOne()
self.assertRaises(NotImplementedError, lambda: mixin.path)
def test_properties_eager(self):
derived = self._derivedClass()(properties={'extant': False})
self.assertEqual(derived.properties, {'extant': False})
def test_batch(self):
connection = _Connection({'foo': 'Qux', 'bar': 'Baz'})
derived = self._derivedClass(connection, '/path')()
with derived.batch:
derived._patch_properties({'foo': 'Foo'})
derived._patch_properties({'bar': 'Baz'})
derived._patch_properties({'foo': 'Qux'})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['data'], {'foo': 'Qux', 'bar': 'Baz'})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_properties_lazy(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(connection, '/path')()
self.assertEqual(derived.properties, {'foo': 'Foo'})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'GET')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
def test__reload_properties(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(connection, '/path')()
derived._reload_properties()
self.assertEqual(derived._properties, {'foo': 'Foo'})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'GET')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
def test__get_property_eager_hit(self):
derived = self._derivedClass()(properties={'foo': 'Foo'})
self.assertEqual(derived._get_property('foo'), 'Foo')
def test__get_property_eager_miss_w_default(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(connection, '/path')()
default = object()
self.assertTrue(derived._get_property('nonesuch', default) is default)
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'GET')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
def test__get_property_lazy_hit(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(connection, '/path')()
self.assertTrue(derived._get_property('nonesuch') is None)
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'GET')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
def test__get_property_w_custom_field(self):
derived = self._derivedClass(foo='get_foo')()
try:
derived._get_property('foo')
except KeyError as e:
self.assertTrue('get_foo' in str(e))
else: # pragma: NO COVER
self.assert_('KeyError not raised')
def test__patch_properties(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(connection, '/path')()
self.assertTrue(derived._patch_properties({'foo': 'Foo'}) is derived)
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['data'], {'foo': 'Foo'})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_get_acl_not_yet_loaded(self):
class ACL(object):
loaded = False
def reload(self):
self.loaded = True
mixin = self._makeOne()
acl = mixin.acl = ACL()
self.assertTrue(mixin.get_acl() is acl)
self.assertTrue(acl.loaded)
def test_get_acl_already_loaded(self):
class ACL(object):
loaded = True
mixin = self._makeOne()
acl = mixin.acl = ACL()
self.assertTrue(mixin.get_acl() is acl) # no 'reload'
class TestPropertyBatch(unittest2.TestCase):
def _getTargetClass(self):
from gcloud.storage._helpers import _PropertyBatch
return _PropertyBatch
def _makeOne(self, wrapped):
return self._getTargetClass()(wrapped)
def _makeWrapped(self, connection=None, path=None, **custom_fields):
from gcloud.storage._helpers import _PropertyMixin
class Wrapped(_PropertyMixin):
CUSTOM_PROPERTY_ACCESSORS = custom_fields
@property
def connection(self):
return connection
@property
def path(self):
return path
return Wrapped()
def test_ctor_does_not_intercept__patch_properties(self):
wrapped = self._makeWrapped()
before = wrapped._patch_properties
batch = self._makeOne(wrapped)
after = wrapped._patch_properties
self.assertEqual(before, after)
self.assertTrue(batch._wrapped is wrapped)
def test_cm_intercepts_restores__patch_properties(self):
wrapped = self._makeWrapped()
before = wrapped._patch_properties
batch = self._makeOne(wrapped)
with batch:
# No deferred patching -> no call to the real '_patch_properties'
during = wrapped._patch_properties
after = wrapped._patch_properties
self.assertNotEqual(before, during)
self.assertEqual(before, after)
def test___exit___w_error_skips__patch_properties(self):
class Testing(Exception):
pass
wrapped = self._makeWrapped()
batch = self._makeOne(wrapped)
try:
with batch:
# deferred patching
wrapped._patch_properties({'foo': 'Foo'})
# but error -> no call to the real '_patch_properties'
raise Testing('testing')
except Testing:
pass
def test___exit___no_error_aggregates__patch_properties(self):
connection = _Connection({'foo': 'Foo'})
wrapped = self._makeWrapped(connection, '/path')
batch = self._makeOne(wrapped)
kw = connection._requested
with batch:
# deferred patching
wrapped._patch_properties({'foo': 'Foo'})
wrapped._patch_properties({'bar': 'Baz'})
wrapped._patch_properties({'foo': 'Qux'})
self.assertEqual(len(kw), 0)
# exited w/o error -> call to the real '_patch_properties'
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['data'], {'foo': 'Qux', 'bar': 'Baz'})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
class _Connection(object):
def __init__(self, *responses):
self._responses = responses
self._requested = []
def api_request(self, **kw):
self._requested.append(kw)
response, self._responses = self._responses[0], self._responses[1:]
return response