|
1 | 1 | import os
|
| 2 | +from unittest import mock |
2 | 3 |
|
| 4 | +import yaml |
3 | 5 | from django.conf import settings
|
4 | 6 | from django.test import SimpleTestCase, override_settings
|
| 7 | +from yaml.constructor import ConstructorError |
5 | 8 |
|
6 | 9 | from pattern_library.utils import (
|
| 10 | + get_pattern_config, |
7 | 11 | get_pattern_config_str,
|
8 | 12 | get_renderer,
|
9 | 13 | get_template_dirs,
|
| 14 | + PatternLibraryLoader, |
10 | 15 | )
|
11 | 16 |
|
12 | 17 |
|
@@ -121,3 +126,31 @@ def test_atom_yml(self):
|
121 | 126 |
|
122 | 127 | self.assertNotEqual(result, "")
|
123 | 128 | self.assertIn("atom_var value from test_atom.yml", result)
|
| 129 | + |
| 130 | + @mock.patch("pattern_library.utils.get_pattern_config_str") |
| 131 | + def test_custom_yaml_tag_error_if_unregistered(self, get_pattern_config_str): |
| 132 | + get_pattern_config_str.return_value = "context:\n atom_var: !customtag" |
| 133 | + |
| 134 | + self.assertRaises( |
| 135 | + ConstructorError, |
| 136 | + get_pattern_config, |
| 137 | + "patterns/atoms/test_custom_yaml_tag/test_custom_yaml_tag.html", |
| 138 | + ) |
| 139 | + |
| 140 | + @mock.patch("pattern_library.utils.get_pattern_config_str") |
| 141 | + def test_custom_yaml_tag(self, get_pattern_config_str): |
| 142 | + get_pattern_config_str.return_value = "context:\n atom_var: !customtag" |
| 143 | + yaml.add_constructor( |
| 144 | + "!customtag", |
| 145 | + lambda loader, node: 42, |
| 146 | + Loader=PatternLibraryLoader, |
| 147 | + ) |
| 148 | + # PyYAML's API doesn't have a remove_constructor() function so we do |
| 149 | + # that manually to avoid leaving things on the loader after the test |
| 150 | + # is finished. |
| 151 | + self.addCleanup(PatternLibraryLoader.yaml_constructors.pop, "!customtag") |
| 152 | + |
| 153 | + self.assertEqual( |
| 154 | + get_pattern_config("mocked.html"), |
| 155 | + {"context": {"atom_var": 42}}, |
| 156 | + ) |
0 commit comments