|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import yaml |
| 4 | +from django.test import SimpleTestCase |
| 5 | +from yaml.constructor import ConstructorError |
| 6 | + |
| 7 | +from pattern_library.utils import get_pattern_context |
| 8 | +from pattern_library.yaml import ( |
| 9 | + register_yaml_tag, |
| 10 | + unregister_yaml_tag, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class PatternLibraryLoaderTestCase(SimpleTestCase): |
| 15 | + def tearDown(self): |
| 16 | + try: |
| 17 | + unregister_yaml_tag("customtag") |
| 18 | + except KeyError: |
| 19 | + pass |
| 20 | + |
| 21 | + def _get_context(self, yaml_str): |
| 22 | + # Use mock.patch to avoid having to create actual files on disk |
| 23 | + with mock.patch("pattern_library.utils.get_pattern_config_str", return_value=yaml_str): |
| 24 | + return get_pattern_context("mocked.html") |
| 25 | + |
| 26 | + def assertContextEqual(self, yaml_str, expected, msg=None): |
| 27 | + """ |
| 28 | + Check that the given yaml string can be loaded and results in the given context. |
| 29 | + """ |
| 30 | + context = self._get_context(yaml_str) |
| 31 | + self.assertEqual(context, expected, msg=msg) |
| 32 | + |
| 33 | + def test_unknown_tag_throws_error(self): |
| 34 | + self.assertRaises( |
| 35 | + ConstructorError, |
| 36 | + self._get_context, |
| 37 | + "context:\n test: !customtag" |
| 38 | + ) |
| 39 | + |
| 40 | + def test_custom_tag_can_be_registered(self): |
| 41 | + register_yaml_tag(lambda: 42, "customtag") |
| 42 | + self.assertContextEqual( |
| 43 | + "context:\n test: !customtag", |
| 44 | + {"test": 42}, |
| 45 | + ) |
| 46 | + |
| 47 | + def test_custom_tag_can_be_unregistered(self): |
| 48 | + register_yaml_tag(lambda: 42, "customtag") |
| 49 | + unregister_yaml_tag("customtag") |
| 50 | + self.assertRaises( |
| 51 | + ConstructorError, |
| 52 | + self._get_context, |
| 53 | + "context:\n test: !customtag" |
| 54 | + ) |
| 55 | + |
| 56 | + def test_custom_tag_registering_doesnt_pollute_parent_loader(self): |
| 57 | + register_yaml_tag(lambda: 42, "customtag") |
| 58 | + self.assertRaises( |
| 59 | + ConstructorError, |
| 60 | + yaml.load, |
| 61 | + "context:\n test: !customtag", |
| 62 | + Loader=yaml.FullLoader, |
| 63 | + ) |
| 64 | + |
| 65 | + def test_registering_plain_decorator(self): |
| 66 | + @register_yaml_tag |
| 67 | + def customtag(): |
| 68 | + return 42 |
| 69 | + |
| 70 | + self.assertContextEqual( |
| 71 | + "context:\n test: !customtag", |
| 72 | + {"test": 42}, |
| 73 | + ) |
| 74 | + |
| 75 | + def test_registering_plain_decorator_called(self): |
| 76 | + @register_yaml_tag() |
| 77 | + def customtag(): |
| 78 | + return 42 |
| 79 | + |
| 80 | + self.assertContextEqual( |
| 81 | + "context:\n test: !customtag", |
| 82 | + {"test": 42}, |
| 83 | + ) |
| 84 | + |
| 85 | + def test_registering_decorator_specify_name(self): |
| 86 | + @register_yaml_tag("customtag") |
| 87 | + def function_with_different_name(): |
| 88 | + return 42 |
| 89 | + |
| 90 | + self.assertContextEqual( |
| 91 | + "context:\n test: !customtag", |
| 92 | + {"test": 42}, |
| 93 | + ) |
| 94 | + |
| 95 | + def test_registering_decorator_specify_name_kwarg(self): |
| 96 | + @register_yaml_tag(name="customtag") |
| 97 | + def function_with_different_name(): |
| 98 | + return 42 |
| 99 | + |
| 100 | + self.assertContextEqual( |
| 101 | + "context:\n test: !customtag", |
| 102 | + {"test": 42}, |
| 103 | + ) |
| 104 | + |
| 105 | + def test_custom_tag_with_args(self): |
| 106 | + register_yaml_tag(lambda *a: sum(a), "customtag") |
| 107 | + |
| 108 | + yaml_str = """ |
| 109 | +context: |
| 110 | + test: !customtag |
| 111 | + - 1 |
| 112 | + - 2 |
| 113 | + - 3 |
| 114 | + """.strip() |
| 115 | + |
| 116 | + self.assertContextEqual(yaml_str, {"test": 6}) |
| 117 | + |
| 118 | + def test_custom_tag_with_kwargs(self): |
| 119 | + register_yaml_tag(lambda **kw: {k.upper(): v for k, v in kw.items()}, "customtag") |
| 120 | + |
| 121 | + yaml_str = """ |
| 122 | +context: |
| 123 | + test: !customtag |
| 124 | + key1: 1 |
| 125 | + key2: 2 |
| 126 | + """.strip() |
| 127 | + |
| 128 | + self.assertContextEqual(yaml_str, {"test": {"KEY1": 1, "KEY2": 2}}) |
0 commit comments