|
1 | 1 | import pytest |
2 | 2 | from unittest.mock import Mock, patch |
3 | 3 |
|
| 4 | +import os |
| 5 | + |
4 | 6 | import numpy.testing as npt |
5 | 7 |
|
6 | 8 | from paths_cli.parsing.core import * |
@@ -71,42 +73,152 @@ def test_to_json_schema(self): |
71 | 73 |
|
72 | 74 |
|
73 | 75 | class TestBuilder: |
| 76 | + @staticmethod |
| 77 | + def _callable(string): |
| 78 | + return "".join(reversed(string)) |
| 79 | + |
74 | 80 | def test_with_remapper(self): |
75 | 81 | # a builder that includes a remapping should use the remapper before |
76 | 82 | # the builder callable |
77 | | - pytest.skip() |
| 83 | + def local_remapper(dct): |
| 84 | + dct['string'] = dct['string'][:-1] |
| 85 | + return dct |
| 86 | + |
| 87 | + builder = Builder(self._callable, remapper=local_remapper) |
| 88 | + assert builder(string="foo") == "of" |
78 | 89 |
|
79 | 90 | def test_imported_builder(self): |
80 | 91 | # a builder that takes a string to define its builder callable |
81 | 92 | # should import that callable, and use it for its call method |
82 | | - pytest.skip() |
| 93 | + cwd = os.getcwd() |
| 94 | + builder = Builder('os.getcwd') |
| 95 | + assert builder() == cwd |
83 | 96 |
|
84 | 97 | def test_callable_builder(self): |
85 | 98 | # a builder that takes a callable as its builder should use that for |
86 | 99 | # its call method |
87 | | - pytest.skip() |
| 100 | + builder = Builder(self._callable) |
| 101 | + assert builder(string="foo") == "oof" |
88 | 102 |
|
89 | 103 | def test_with_after_build(self): |
90 | 104 | # a builder with an after_build parameter should use that after the |
91 | 105 | # builder callable |
92 | | - pytest.skip() |
| 106 | + def local_after(obj, dct): |
| 107 | + return obj[:-1] + dct['string'] |
| 108 | + |
| 109 | + builder = Builder(self._callable, after_build=local_after) |
| 110 | + assert builder(string="foo") == "oofoo" |
93 | 111 |
|
94 | 112 | class TestInstanceBuilder: |
| 113 | + @staticmethod |
| 114 | + def _builder(req_param, opt_default=10, opt_override=100): |
| 115 | + return f"{req_param}, {opt_default}, {opt_override}" |
| 116 | + |
| 117 | + def setup(self): |
| 118 | + identity = lambda x: x |
| 119 | + self.parameters = [ |
| 120 | + Parameter('req_param', identity, json_type="string"), |
| 121 | + Parameter('opt_default', identity, json_type="int", default=10), |
| 122 | + Parameter('opt_override', identity, json_type='int', |
| 123 | + default=100) |
| 124 | + ] |
| 125 | + self.instance_builder = InstanceBuilder( |
| 126 | + self._builder, |
| 127 | + self.parameters, |
| 128 | + name='demo', |
| 129 | + aliases=['foo', 'bar'], |
| 130 | + ) |
| 131 | + self.instance_builder.parser_name = 'demo' |
| 132 | + self.input_dict = {'req_param': "qux", 'opt_override': 25} |
| 133 | + |
95 | 134 | def test_to_json_schema(self): |
96 | 135 | # to_json_schema should create a valid JSON schema entry for this |
97 | 136 | # instance builder |
98 | | - pytest.skip() |
| 137 | + # TODO: this may change as I better understand JSON schema. Details |
| 138 | + # of the JSON schema API aren't locked until I can build docs from |
| 139 | + # our schema. |
| 140 | + expected_schema = { |
| 141 | + 'properties': { |
| 142 | + 'name': {'type': 'string'}, |
| 143 | + 'type': {'type': 'string', |
| 144 | + 'enum': ['demo']}, |
| 145 | + 'req_param': {'type': 'string', 'description': None}, |
| 146 | + 'opt_default': {'type': 'int', 'description': None}, |
| 147 | + 'opt_override': {'type': 'int', 'description': None}, |
| 148 | + }, |
| 149 | + 'required': ['req_param'], |
| 150 | + } |
| 151 | + name, schema = self.instance_builder.to_json_schema() |
| 152 | + assert name == 'demo' |
| 153 | + assert expected_schema['required'] == schema['required'] |
| 154 | + assert expected_schema['properties'] == schema['properties'] |
99 | 155 |
|
100 | 156 | def test_parse_attrs(self): |
101 | 157 | # parse_attrs should create a dictionary with correct objects in the |
102 | 158 | # attributes from the input dictionary |
| 159 | + expected = {'req_param': "qux", 'opt_override': 25} |
| 160 | + # note that the parameter where we use the default value isn't |
| 161 | + # listed: the default value should match the default used in the |
| 162 | + # code, though! |
| 163 | + assert self.instance_builder.parse_attrs(self.input_dict) == expected |
| 164 | + |
| 165 | + def test_parse_attrs_parser_integration(self): |
| 166 | + # parse_attrs gives the same object as already existing in a parser |
| 167 | + # if one of the parameters uses that parser to load a named object |
103 | 168 | pytest.skip() |
104 | 169 |
|
105 | 170 | def test_parse_attrs_missing_required(self): |
106 | 171 | # an InputError should be raised if a required parameter is missing |
107 | | - pytest.skip() |
| 172 | + input_dict = {'opt_override': 25} |
| 173 | + with pytest.raises(InputError, match="missing required"): |
| 174 | + self.instance_builder.parse_attrs(input_dict) |
108 | 175 |
|
109 | 176 | def test_call(self): |
110 | 177 | # calling the instance builder should create the object |
| 178 | + expected = "qux, 10, 25" |
| 179 | + assert self.instance_builder(self.input_dict) == expected |
| 180 | + |
| 181 | + |
| 182 | +class TestParser: |
| 183 | + def test_parse_str(self): |
| 184 | + # parse_str should load a known object with the input name |
111 | 185 | pytest.skip() |
112 | 186 |
|
| 187 | + def test_parse_str_error(self): |
| 188 | + # if parse_str is given a name that is not known, an InputError |
| 189 | + # should be raised |
| 190 | + pytest.skip() |
| 191 | + |
| 192 | + def test_parse_dict(self): |
| 193 | + # parse_dct should create the object from the input dict |
| 194 | + pytest.skip() |
| 195 | + |
| 196 | + def test_register_object_named(self): |
| 197 | + # when registered, a named object should register with the all_objs |
| 198 | + # list and with the named_objs dict |
| 199 | + pytest.skip() |
| 200 | + |
| 201 | + def test_register_object_unnamed(self): |
| 202 | + # when registered, an unnamed object should register with the |
| 203 | + # all_objs list and leave the named_objs dict unchanged |
| 204 | + pytest.skip() |
| 205 | + |
| 206 | + def test_register_object_duplicate(self): |
| 207 | + # if an attempt is made to register an object with a name that is |
| 208 | + # already in use, an InputError should be raised, and the object |
| 209 | + # should not register with either all_objs or named_objs |
| 210 | + pytest.skip() |
| 211 | + |
| 212 | + def test_register_builder(self): |
| 213 | + pytest.skip() |
| 214 | + |
| 215 | + def test_register_builder_duplicate(self): |
| 216 | + pytest.skip() |
| 217 | + |
| 218 | + @pytest.mark.parametrize('input_type', ['str', 'dict']) |
| 219 | + def test_parse(self, input_type): |
| 220 | + pytest.skip() |
| 221 | + |
| 222 | + @pytest.mark.parametrize('as_list', [True, False]) |
| 223 | + def test_call(self, as_list): |
| 224 | + pytest.skip() |
0 commit comments