|
6 | 6 | import numpy.testing as npt |
7 | 7 |
|
8 | 8 | from paths_cli.compiling.core import * |
| 9 | +from paths_cli.compiling import compiler_for |
| 10 | +from paths_cli.tests.compiling.utils import mock_compiler |
9 | 11 |
|
10 | 12 | class MockNamedObject: |
11 | 13 | # used in the tests for CategoryCompiler._compile_dict and |
@@ -187,10 +189,30 @@ def test_compile_attrs(self): |
187 | 189 | assert compile_attrs(self.input_dict) == expected |
188 | 190 |
|
189 | 191 | def test_compile_attrs_compiler_integration(self): |
190 | | - # compile_attrs gives the same object as already existing in a |
191 | | - # compiler if one of the parameters uses that compiler to load a |
192 | | - # named object |
193 | | - pytest.skip() |
| 192 | + # compile_attrs gives the existing named object (is-identity) if one |
| 193 | + # of the parameters uses that compiler to load a named object |
| 194 | + user_input = {'foo': 'named_foo'} |
| 195 | + # full_input = {'foo': {'name': 'named_foo', |
| 196 | + # 'type': 'baz', |
| 197 | + # 'bar': 'should see this'}} |
| 198 | + bar_plugin = InstanceCompilerPlugin( |
| 199 | + builder=lambda foo: 'in bar: should not see this', |
| 200 | + parameters=[Parameter('foo', compiler_for('foo'))], |
| 201 | + ) |
| 202 | + foo_plugin = InstanceCompilerPlugin( |
| 203 | + builder=lambda: 'in foo: should not see this', |
| 204 | + parameters=[], |
| 205 | + ) |
| 206 | + named_objs = {'named_foo': 'should see this'} |
| 207 | + type_dispatch = {'baz': foo_plugin} |
| 208 | + PATCH_LOC = 'paths_cli.compiling.root_compiler._COMPILERS' |
| 209 | + compiler = mock_compiler('foo', type_dispatch=type_dispatch, |
| 210 | + named_objs=named_objs) |
| 211 | + with patch.dict(PATCH_LOC, {'foo': compiler}): |
| 212 | + compiled = bar_plugin.compile_attrs(user_input) |
| 213 | + |
| 214 | + # maps attr name 'foo' to the previous existing object |
| 215 | + assert compiled == {'foo': 'should see this'} |
194 | 216 |
|
195 | 217 | def test_compile_attrs_missing_required(self): |
196 | 218 | # an InputError should be raised if a required parameter is missing |
@@ -304,16 +326,44 @@ def test_register_builder_identical(self): |
304 | 326 | orig = self.compiler.type_dispatch['foo'] |
305 | 327 | self.compiler.register_builder(orig, 'foo') |
306 | 328 |
|
| 329 | + @staticmethod |
| 330 | + def _validate_obj(obj, input_type): |
| 331 | + if input_type == 'str': |
| 332 | + assert obj == 'bar' |
| 333 | + elif input_type == 'dict': |
| 334 | + assert obj.data == 'qux' |
| 335 | + else: |
| 336 | + raise RuntimeError("Error in test setup") |
| 337 | + |
307 | 338 | @pytest.mark.parametrize('input_type', ['str', 'dict']) |
308 | 339 | def test_compile(self, input_type): |
309 | 340 | # the compile method should work whether the input is a dict |
310 | 341 | # representing an object to be compiled or string name for an |
311 | 342 | # already-compiled object |
312 | | - pytest.skip() |
| 343 | + self._mock_register_obj() |
| 344 | + input_data = { |
| 345 | + 'str': 'foo', |
| 346 | + 'dict': {'type': 'foo', 'data': 'qux'} |
| 347 | + }[input_type] |
| 348 | + obj = self.compiler.compile(input_data) |
| 349 | + self._validate_obj(obj, input_type) |
313 | 350 |
|
314 | 351 | @pytest.mark.parametrize('input_type', ['str', 'dict']) |
315 | 352 | @pytest.mark.parametrize('as_list', [True, False]) |
316 | 353 | def test_call(self, input_type, as_list): |
317 | 354 | # the call method should work whether the input is a single object |
318 | 355 | # or a list of objects (as well as whether string or dict) |
319 | | - pytest.skip() |
| 356 | + self._mock_register_obj() |
| 357 | + input_data = { |
| 358 | + 'str': 'foo', |
| 359 | + 'dict': {'type': 'foo', 'data': 'qux'} |
| 360 | + }[input_type] |
| 361 | + if as_list: |
| 362 | + input_data = [input_data] |
| 363 | + |
| 364 | + obj = self.compiler(input_data) |
| 365 | + if as_list: |
| 366 | + assert isinstance(obj, list) |
| 367 | + assert len(obj) == 1 |
| 368 | + obj = obj[0] |
| 369 | + self._validate_obj(obj, input_type) |
0 commit comments