class _AssertTemplateUsedContext(object): def __init__(self, test_case, template_name): self.test_case = test_case self.template_name = template_name self.rendered_templates = [] self.rendered_template_names = [] self.context = ContextList() def on_template_render(self, sender, signal, template, context, **kwargs): self.rendered_templates.append(template) self.rendered_template_names.append(template.name) self.context.append(copy(context)) def test(self): return self.template_name in self.rendered_template_names def message(self): return "%s was not rendered." % self.template_name def __enter__(self): template_rendered.connect(self.on_template_render) return self def __exit__(self, exc_type, exc_value, traceback): template_rendered.disconnect(self.on_template_render) if exc_type is not None: return if not self.test(): message = self.message() if len(self.rendered_templates) == 0: message += " No template was rendered." else: message += " Following templates were rendered: %s" % (", ".join(self.rendered_template_names)) self.test_case.fail(message)
def store_rendered_templates(store, signal, sender, template, context, **kwargs): """ Stores templates and contexts that are rendered. The context is copied so that it is an accurate representation at the time of rendering. """ store.setdefault('templates', []).append(template) store.setdefault('context', ContextList()).append(copy(context))
def store_rendered_templates(store, signal, sender, template, context, **kwargs): """ Store templates and contexts that are rendered. The context is copied so that it is an accurate representation at the time of rendering. """ store.setdefault("templates", []).append(template) if "context" not in store: store["context"] = ContextList() store["context"].append(copy(context))
def store_rendered_templates(store, signal, sender, template, context, **kwargs): """ Stores templates and contexts that are rendered. The context is copied so that it is an accurate representation at the time of rendering. Entirely based on the Django Test Client https://github.com/django/django/blob/master/django/test/client.py#L88 """ store.setdefault('templates', []).append(template) store.setdefault('context', ContextList()).append(copy(context))
class _AssertTemplateUsedContext(object): def __init__(self, test_case, template_name): self.test_case = test_case self.template_name = template_name self.rendered_templates = [] self.rendered_template_names = [] self.context = ContextList() def on_template_render(self, sender, signal, template, context, **kwargs): self.rendered_templates.append(template) self.rendered_template_names.append(template.name) self.context.append(copy(context)) def test(self): return self.template_name in self.rendered_template_names def message(self): return u'%s was not rendered.' % self.template_name def __enter__(self): template_rendered.connect(self.on_template_render) return self def __exit__(self, exc_type, exc_value, traceback): template_rendered.disconnect(self.on_template_render) if exc_type is not None: return if not self.test(): message = self.message() if len(self.rendered_templates) == 0: message += u' No template was rendered.' else: message += u' Following templates were rendered: %s' % ( ', '.join(self.rendered_template_names)) self.test_case.fail(message)
def __init__(self, test_case, template_name): self.test_case = test_case self.template_name = template_name self.rendered_templates = [] self.rendered_template_names = [] self.context = ContextList()
snapshot = Snapshot("test", data_dir=reference_file.dirname) reference_content = "FOO" reference_file.write(reference_content) assert snapshot.reference == reference_content @pytest.mark.parametrize( "input, clean_output", [ (None, None), ([], []), ({"foo": "bar"}, {"foo": "bar"}), ({"block": True, "forloop": False, "lastupdated": 1234}, {}), ([{"foo": "bar"}], [{"foo": "bar"}]), ([{"block": True}, {"forloop": False, "lastupdated": 1234}], [{}, {}]), (ContextList([Context({"foo": "bar"})]), {"foo": "bar"}), (ContextList([Context({"block": True})]), {}), ( ContextList([Context({"block": True}), Context({"foo": "bar"})]), {"foo": "bar"}, ), (RequestContext(None, {"foo": "bar"}), {"foo": "bar"}), ], ) def test_snapshot_clean(input, clean_output): """Tests the cleanup of snapshot contexts.""" snapshot = Snapshot("test") assert snapshot.clean(input) == clean_output def test_snapshot_assert_matches_generate(tmpdir):
def setUp(self): self.templates = [] self.contexts = ContextList() template_rendered.connect(self.store_rendered_template)