Skip to content

Commit 1044066

Browse files
committed
work to make it readable with a nod towards further tests
1 parent 457d27a commit 1044066

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

tests/test_mig_wsgi-bin_migwsgi.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def _is_return_value(return_value):
8888
return return_value in defined_return_values
8989

9090

91+
def _unpack_result(application_result):
92+
chunks = list(application_result)
93+
assert len(chunks) > 0, "invocation returned no output"
94+
complete_value = b''.join(chunks)
95+
decoded_value = codecs.decode(complete_value, 'utf8')
96+
return decoded_value
97+
98+
9199
def create_instrumented_format_output(arranged):
92100
def _instrumented_format_output(
93101
configuration,
@@ -147,7 +155,10 @@ def create_instrumented_retrieve_handler(output_objects=None, return_value=None)
147155
assert _is_return_value(return_value), "return value must be present in returnvalues"
148156

149157
def _instrumented_retrieve_handler(*args):
158+
_instrumented_retrieve_handler.calls.append(tuple(args))
150159
return [], return_value
160+
_instrumented_retrieve_handler.calls = []
161+
151162
return _instrumented_retrieve_handler
152163

153164

@@ -165,15 +176,23 @@ def noop(*args):
165176
pass
166177

167178

168-
class MigSharedConfiguration(MigTestCase):
169-
def assertHtmlBasics(self, value):
179+
class MigWsgi_binMigwsgi(MigTestCase):
180+
def assertInstrumentation(self):
181+
def was_called(fake):
182+
assert hasattr(fake, 'calls')
183+
return len(fake.calls) > 0
184+
185+
self.assertTrue(was_called(self.instrumented_format_output))
186+
self.assertTrue(was_called(self.instrumented_retrieve_handler))
187+
188+
def assertIsValidHtmlDocument(self, value):
170189
assert isinstance(value, type(u""))
171190
assert value.startswith("<!DOCTYPE")
172191
end_html_tag_idx = value.rfind('</html>')
173192
maybe_document_end = value[end_html_tag_idx:].rstrip()
174193
self.assertEqual(maybe_document_end, '</html>')
175194

176-
def test_xxx(self):
195+
def before_each(self):
177196
config = _assert_local_config()
178197
config_global_values = _assert_local_config_global_values(config)
179198

@@ -182,30 +201,37 @@ def fake_start_response(status, headers, exc=None):
182201
fake_start_response.calls = []
183202

184203
def fake_set_environ(value):
185-
fake_set_environ.value = value
186-
fake_set_environ.value = None
204+
fake_set_environ.calls.append((value))
205+
fake_set_environ.calls = []
187206

188-
wsgi_environ = create_wsgi_environ(_TEST_CONF_FILE, wsgi_variables=dict(
207+
fake_wsgi_environ = create_wsgi_environ(_TEST_CONF_FILE, wsgi_variables=dict(
189208
http_host='localhost',
190209
path_info='/',
191210
))
192211

193-
instrumented_format_output = create_instrumented_format_output('HELLO WORLD')
194-
instrumented_retrieve_handler = create_instrumented_retrieve_handler(None, returnvalues.OK)
212+
self.instrumented_format_output = create_instrumented_format_output('HELLO WORLD')
213+
self.instrumented_retrieve_handler = create_instrumented_retrieve_handler(None, returnvalues.OK)
195214

196-
yielder = migwsgi._application(wsgi_environ, fake_start_response,
197-
_format_output=instrumented_format_output,
198-
_retrieve_handler=instrumented_retrieve_handler,
215+
self.application_args = (fake_wsgi_environ, fake_start_response,)
216+
self.application_kwargs = dict(
217+
_format_output=self.instrumented_format_output,
218+
_retrieve_handler=self.instrumented_retrieve_handler,
199219
_set_environ=fake_set_environ,
220+
)
221+
222+
def test_creates_valid_html_page_for_return_value_ok(self):
223+
application_result = migwsgi._application(
224+
*self.application_args,
200225
_wrap_wsgi_errors=noop,
201226
_config_file=_TEST_CONF_FILE,
202227
_skip_log=True,
228+
**self.application_kwargs
203229
)
204-
chunks = list(yielder)
205230

206-
self.assertGreater(len(chunks), 0)
207-
value = codecs.decode(chunks[0], 'utf8')
208-
self.assertHtmlBasics(value)
231+
output = _unpack_result(application_result)
232+
233+
self.assertInstrumentation()
234+
self.assertIsValidHtmlDocument(output)
209235

210236

211237
if __name__ == '__main__':

0 commit comments

Comments
 (0)