|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from django.core.exceptions import ImproperlyConfigured |
| 4 | +from django.http import HttpRequest |
| 5 | +from django.test import SimpleTestCase |
| 6 | + |
| 7 | +from pattern_library import register_context_modifier |
| 8 | +from pattern_library.context_modifiers import registry |
| 9 | +from pattern_library.utils import render_pattern |
| 10 | + |
| 11 | + |
| 12 | +def accepts_context_only(context): |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +def accepts_request_only(request): |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +def modifier_1(context, request): |
| 21 | + context["foo"] = "foo" |
| 22 | + |
| 23 | + |
| 24 | +def modifier_2(context, request): |
| 25 | + context["foo"] = "bar" |
| 26 | + |
| 27 | + |
| 28 | +def modifier_3(context, request): |
| 29 | + context["beep"] = "boop" |
| 30 | + |
| 31 | + |
| 32 | +atom_template = "patterns/atoms/test_atom/test_atom.html" |
| 33 | + |
| 34 | + |
| 35 | +class ContextModifierTestCase(SimpleTestCase): |
| 36 | + |
| 37 | + maxDiff = None |
| 38 | + |
| 39 | + def setUp(self): |
| 40 | + registry.clear() |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + registry.clear() |
| 44 | + |
| 45 | + def test_validation(self): |
| 46 | + with self.assertRaisesRegex(ImproperlyConfigured, "must be callables"): |
| 47 | + registry.register(0, template=atom_template) |
| 48 | + with self.assertRaisesRegex( |
| 49 | + ImproperlyConfigured, "must accept a 'request' argument" |
| 50 | + ): |
| 51 | + registry.register(accepts_context_only, template=atom_template) |
| 52 | + with self.assertRaisesRegex( |
| 53 | + ImproperlyConfigured, "must accept a 'context' argument" |
| 54 | + ): |
| 55 | + registry.register(accepts_request_only, template=atom_template) |
| 56 | + |
| 57 | + def test_registred_without_ordering(self): |
| 58 | + registry.register(modifier_1, template=atom_template) |
| 59 | + registry.register(modifier_2, template=atom_template) |
| 60 | + registry.register(modifier_3, template=atom_template) |
| 61 | + |
| 62 | + result = registry.get_for_template(atom_template) |
| 63 | + self.assertEqual( |
| 64 | + result, |
| 65 | + [ |
| 66 | + modifier_1, |
| 67 | + modifier_2, |
| 68 | + modifier_3, |
| 69 | + ], |
| 70 | + ) |
| 71 | + |
| 72 | + def test_registered_with_ordering(self): |
| 73 | + registry.register(modifier_1, template=atom_template, order=10) |
| 74 | + registry.register(modifier_2, template=atom_template, order=5) |
| 75 | + registry.register(modifier_3, template=atom_template, order=0) |
| 76 | + |
| 77 | + result = registry.get_for_template(atom_template) |
| 78 | + self.assertEqual( |
| 79 | + result, |
| 80 | + [ |
| 81 | + modifier_3, |
| 82 | + modifier_2, |
| 83 | + modifier_1, |
| 84 | + ], |
| 85 | + ) |
| 86 | + |
| 87 | + def test_registered_via_decorator(self): |
| 88 | + @register_context_modifier(order=100) |
| 89 | + def func_a(context, request): |
| 90 | + pass |
| 91 | + |
| 92 | + @register_context_modifier(order=50) |
| 93 | + def func_b(context, request): |
| 94 | + pass |
| 95 | + |
| 96 | + @register_context_modifier(template=atom_template) |
| 97 | + def func_c(context, request): |
| 98 | + pass |
| 99 | + |
| 100 | + @register_context_modifier(template="different_template.html", order=1) |
| 101 | + def func_x(context, request): # NOQA |
| 102 | + pass |
| 103 | + |
| 104 | + result = registry.get_for_template(atom_template) |
| 105 | + self.assertEqual( |
| 106 | + result, |
| 107 | + [ |
| 108 | + func_c, |
| 109 | + func_b, |
| 110 | + func_a, |
| 111 | + ], |
| 112 | + ) |
| 113 | + |
| 114 | + @mock.patch("pattern_library.utils.render_to_string") |
| 115 | + def test_applied_by_render_pattern(self, render_to_string): |
| 116 | + request = HttpRequest() |
| 117 | + registry.register(modifier_1) |
| 118 | + registry.register(modifier_2, template=atom_template) |
| 119 | + registry.register(modifier_3, template=atom_template) |
| 120 | + |
| 121 | + render_pattern(request, atom_template) |
| 122 | + render_to_string.assert_called_with( |
| 123 | + atom_template, |
| 124 | + request=request, |
| 125 | + context={ |
| 126 | + "atom_var": "atom_var value from test_atom.yaml", |
| 127 | + "__pattern_library_view": True, |
| 128 | + "foo": "bar", |
| 129 | + "beep": "boop", |
| 130 | + }, |
| 131 | + ) |
0 commit comments