Skip to content

Commit 0126134

Browse files
committed
Extend snapshotting support to allow capturing the content only output.
1 parent a0d40af commit 0126134

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>some text</p>

tests/support/snapshotsupp.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
from tests.support.suppconst import TEST_BASE
3535

36+
HTML_TAG = '<html>'
37+
MARKER_CONTENT_BEGIN = '<!-- Begin UI container -->'
38+
MARKER_CONTENT_END = '<!-- End UI container -->'
3639
TEST_SNAPSHOTS_DIR = os.path.join(TEST_BASE, "snapshots")
3740

3841
try:
@@ -42,6 +45,28 @@
4245
raise
4346

4447

48+
def _html_content_only(value):
49+
"""For a given HTML input extract only the portion that corresponds to the
50+
page content. This is somewhat convoluted due to having to work around an
51+
inability to move the comment markers to enclose only the content.
52+
"""
53+
54+
assert value.find(HTML_TAG) > -1, "value does not appear to be HTML"
55+
content_start_index = value.find(MARKER_CONTENT_BEGIN)
56+
assert content_start_index > -1, "unable to locate beginning of content"
57+
# set the index after the content marker
58+
content_start_index += len(MARKER_CONTENT_BEGIN)
59+
# we now need to remove the container div inside it ..first find it
60+
content_start_inner_div = value.find('<div', content_start_index)
61+
# reset the content start to exclude up the end of the container div
62+
content_start_index = value.find('>', content_start_inner_div) + 1
63+
64+
content_end_index = value.find(MARKER_CONTENT_END)
65+
assert content_end_index > -1, "unable to locate end of content"
66+
67+
return value[content_start_index:content_end_index].strip()
68+
69+
4570
def _delimited_lines(value):
4671
"""Break a value by newlines into lines suitable for diffing."""
4772

@@ -73,14 +98,14 @@ def _force_refresh_snapshots():
7398

7499

75100
class SnapshotAssertMixin:
76-
"""Custom assertions alowing the use of snapshots within tests."""
101+
"""Custom assertions allowing the use of snapshots within tests."""
77102

78-
def assertSnapshot(self, actual_content, extension=None):
79-
"""Load a snapshot corresponding to the named test and check that its
80-
content, which is th expectatoin, matches what was actually given. In
81-
the case a snapshot does not exist it is saved on first invocation."""
103+
def _snapshotsupp_compare_snapshot(self, extension, actual_content):
104+
"""Helper which actually loads the snapshot from a file on disk and
105+
does the comparison.
82106
83-
assert extension is not None
107+
In the case a snapshot does not exist it is saved on first invocation.
108+
"""
84109

85110
file_name = ''.join([self._testMethodName, ".", extension])
86111
file_path = os.path.join(TEST_SNAPSHOTS_DIR, file_name)
@@ -106,3 +131,21 @@ def assertSnapshot(self, actual_content, extension=None):
106131
)
107132
raise AssertionError(
108133
"content did not match snapshot\n\n%s" % (''.join(udiff),))
134+
135+
def assertSnapshot(self, actual_content, extension=None):
136+
"""Load a snapshot corresponding to the named test and check that what
137+
it contains, which is the expectation, matches what was actually given.
138+
"""
139+
140+
assert extension is not None
141+
142+
self._snapshotsupp_compare_snapshot(extension, actual_content)
143+
144+
def assertSnapshotOfHtmlContent(self, actual_content):
145+
"""Load a snapshot corresponding to the named test and check that what
146+
it contains, which is the expectation, matches against the portion of
147+
what was actually given that corresponds to the output HTML content.
148+
"""
149+
150+
actual_content = _html_content_only(actual_content)
151+
self._snapshotsupp_compare_snapshot('html', actual_content)

tests/test_mig_wsgibin.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def test_objects_containing_only_title_matches_snapshot(self):
241241
self.assertSnapshot(output, extension='html')
242242

243243

244-
class MigWsgibin_output_objects(MigTestCase, WsgiAssertMixin):
244+
class MigWsgibin_output_objects(MigTestCase, WsgiAssertMixin, SnapshotAssertMixin):
245245

246246
def _provide_configuration(self):
247247
return 'testconfig'
@@ -281,6 +281,28 @@ def test_unknown_object_type_generates_valid_error_page(self):
281281
output, _ = self.assertWsgiResponse(wsgi_result, self.fake_wsgi, 200)
282282
self.assertIsValidHtmlDocument(output)
283283

284+
def test_objects_with_type_text(self):
285+
output_objects = [
286+
# workaround invalid HTML being generated with no title object
287+
{
288+
'object_type': 'title',
289+
'text': 'TEST'
290+
},
291+
{
292+
'object_type': 'text',
293+
'text': 'some text',
294+
}
295+
]
296+
self.fake_backend.set_response(output_objects, returnvalues.OK)
297+
298+
wsgi_result = migwsgi.application(
299+
*self.application_args,
300+
**self.application_kwargs
301+
)
302+
303+
output, _ = self.assertWsgiResponse(wsgi_result, self.fake_wsgi, 200)
304+
self.assertSnapshotOfHtmlContent(output)
305+
284306

285307
if __name__ == '__main__':
286308
testmain()

0 commit comments

Comments
 (0)