Skip to content

Commit cb69db6

Browse files
committed
(most) tests for InstanceBuilder
1 parent 656a22e commit cb69db6

2 files changed

Lines changed: 122 additions & 8 deletions

File tree

‎paths_cli/parsing/core.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def schema_name(self):
172172
if not self.name.endswith(self.parser_name):
173173
schema_name = f"{self.name}-{self.object_type}"
174174
else:
175-
schema_name = name
175+
schema_name = self.parser_name
176176
return schema_name
177177

178178
def to_json_schema(self, schema_context=None):
@@ -225,6 +225,7 @@ def parse_attrs(self, dct):
225225
new_dct[attr] = func(input_dct[attr])
226226

227227
optionals = set(self.optional_attributes) & set(dct)
228+
228229
for attr in optionals:
229230
new_dct[attr] = self.optional_attributes[attr](dct[attr])
230231

@@ -271,7 +272,8 @@ def _parse_dict(self, dct):
271272
def register_object(self, obj, name):
272273
if name is not None:
273274
if name in self.named_objs:
274-
raise RuntimeError("Same name twice") # TODO improve
275+
raise InputError(f"An object of type {self.name} and name "
276+
f"{name} already exists.")
275277
obj = obj.named(name)
276278
self.named_objs[name] = obj
277279
obj = obj.named(name)

‎paths_cli/tests/parsing/test_core.py‎

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
from unittest.mock import Mock, patch
33

4+
import os
5+
46
import numpy.testing as npt
57

68
from paths_cli.parsing.core import *
@@ -71,42 +73,152 @@ def test_to_json_schema(self):
7173

7274

7375
class TestBuilder:
76+
@staticmethod
77+
def _callable(string):
78+
return "".join(reversed(string))
79+
7480
def test_with_remapper(self):
7581
# a builder that includes a remapping should use the remapper before
7682
# 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"
7889

7990
def test_imported_builder(self):
8091
# a builder that takes a string to define its builder callable
8192
# 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
8396

8497
def test_callable_builder(self):
8598
# a builder that takes a callable as its builder should use that for
8699
# its call method
87-
pytest.skip()
100+
builder = Builder(self._callable)
101+
assert builder(string="foo") == "oof"
88102

89103
def test_with_after_build(self):
90104
# a builder with an after_build parameter should use that after the
91105
# 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"
93111

94112
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+
95134
def test_to_json_schema(self):
96135
# to_json_schema should create a valid JSON schema entry for this
97136
# 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']
99155

100156
def test_parse_attrs(self):
101157
# parse_attrs should create a dictionary with correct objects in the
102158
# 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
103168
pytest.skip()
104169

105170
def test_parse_attrs_missing_required(self):
106171
# 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)
108175

109176
def test_call(self):
110177
# 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
111185
pytest.skip()
112186

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

Comments
 (0)