Skip to content

Commit 59b890c

Browse files
committed
Some docstrings and tests for core
1 parent eb78804 commit 59b890c

2 files changed

Lines changed: 131 additions & 4 deletions

File tree

‎paths_cli/parsing/core.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _get_from_loader(loader, attr_name, attr):
6969
return attr
7070

7171
def __call__(self, *args, **kwargs):
72-
# check correct call signature here
72+
# check correct call signature here?
7373
return self.loader(*args, **kwargs)
7474

7575
def to_json_schema(self, schema_context=None):
@@ -86,7 +86,7 @@ class Builder:
8686
When the parsed parameters dictionary matches the kwargs for your class,
8787
you can create a valid delayed builder function with
8888
89-
.. code:
89+
.. code::
9090
9191
builder = Builder('import_path.for_the.ClassToBuild')
9292
@@ -129,6 +129,25 @@ def __call__(self, **dct):
129129

130130

131131
class InstanceBuilder(OPSPlugin):
132+
"""
133+
134+
Parameters
135+
----------
136+
builder : Callable
137+
Function that actually creates an instance of this object. Note that
138+
the :class:`.Builder` class is often a useful tool here (but any
139+
callable is allowed).
140+
parameters : List[:class:`.Parameter]
141+
Descriptions of the paramters for that the builder takes.
142+
name : str
143+
Name used in the text input for this object.
144+
aliases : List[str]
145+
Other names that can be used.
146+
requires_ops : Tuple[int, int]
147+
version of OpenPathSampling required for this functionality
148+
requires_cli : Tuple[int, int]
149+
version of the OPS CLI requires for this functionality
150+
"""
132151
SCHEMA = "http://openpathsampling.org/schemas/sim-setup/draft01.json"
133152
parser_name = None
134153
error_on_duplicate = False # TODO: temporary
@@ -174,6 +193,10 @@ def to_json_schema(self, schema_context=None):
174193
def parse_attrs(self, dct):
175194
"""Parse the user input dictionary to mapping of name to object.
176195
196+
This changes the values in the key-value pairs we get from the file
197+
into objects that are suitable as input to a function. Further
198+
remapping of keys is performed by the builder.
199+
177200
Parameters
178201
----------
179202
dct: Dict
@@ -182,8 +205,8 @@ def parse_attrs(self, dct):
182205
Returns
183206
-------
184207
Dict :
185-
Mapping with the keys relevant to the input dictionary, but
186-
values are now appropriate inputs for the builder.
208+
Mapping with the keys from the input dictionary, but values are
209+
now appropriate inputs for the builder.
187210
"""
188211
# TODO: support aliases in dct[attr]
189212
input_dct = self.defaults.copy()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,112 @@
11
import pytest
2+
from unittest.mock import Mock, patch
23

34
import numpy.testing as npt
45

56
from paths_cli.parsing.core import *
67

8+
class TestParameter:
9+
def setup(self):
10+
self.loader = Mock(
11+
return_value='foo',
12+
json_type='string',
13+
description="string 'foo'",
14+
)
715

16+
def test_parameter_info_in_loader(self):
17+
# if parameter doesn't give json_type/description, but the
18+
# loader does, then we return what the loader says
19+
parameter = Parameter(name='foo_param',
20+
loader=self.loader)
21+
assert parameter.name == 'foo_param'
22+
assert parameter.loader() == "foo"
23+
assert parameter.json_type == "string"
24+
assert parameter.description == "string 'foo'"
25+
26+
def test_parameter_info_local(self):
27+
# if parameter and loader both give json_type/description, then the
28+
# value given by the parameter takes precendence
29+
parameter = Parameter(name='foo_param',
30+
loader=self.loader,
31+
json_type='int', # it's a lie!
32+
description='override')
33+
assert parameter.name == 'foo_param'
34+
assert parameter.loader() == "foo"
35+
assert parameter.json_type == "int"
36+
assert parameter.description == "override"
37+
38+
def test_parameter_info_none(self):
39+
# if neither parameter nor loader define json_type/description, then
40+
# we should return None for those
41+
parameter = Parameter(name="foo_param",
42+
loader=lambda: "foo")
43+
assert parameter.name == 'foo_param'
44+
assert parameter.loader() == "foo"
45+
assert parameter.json_type is None
46+
assert parameter.description is None
47+
48+
@pytest.mark.parametrize('is_required', [True, False])
49+
def test_required(self, is_required):
50+
if is_required:
51+
parameter = Parameter(name="foo_param", loader=self.loader)
52+
else:
53+
parameter = Parameter(name="foo_param", loader=self.loader,
54+
default="bar")
55+
56+
assert parameter.required == is_required
57+
58+
def test_call(self):
59+
# calling the parameter object should call its loader
60+
# TODO: maybe check that we pass arguments along correctly?
61+
parameter = Parameter(name="foo_param", loader=self.loader)
62+
assert parameter() == "foo"
63+
64+
def test_to_json_schema(self):
65+
# to_json_schema should return the expected values
66+
parameter = Parameter(name="foo_param",
67+
loader=self.loader)
68+
expected = {'type': 'string',
69+
'description': "string 'foo'"}
70+
assert parameter.to_json_schema() == ("foo_param", expected)
71+
72+
73+
class TestBuilder:
74+
def test_with_remapper(self):
75+
# a builder that includes a remapping should use the remapper before
76+
# the builder callable
77+
pytest.skip()
78+
79+
def test_imported_builder(self):
80+
# a builder that takes a string to define its builder callable
81+
# should import that callable, and use it for its call method
82+
pytest.skip()
83+
84+
def test_callable_builder(self):
85+
# a builder that takes a callable as its builder should use that for
86+
# its call method
87+
pytest.skip()
88+
89+
def test_with_after_build(self):
90+
# a builder with an after_build parameter should use that after the
91+
# builder callable
92+
pytest.skip()
93+
94+
class TestInstanceBuilder:
95+
def test_to_json_schema(self):
96+
# to_json_schema should create a valid JSON schema entry for this
97+
# instance builder
98+
pytest.skip()
99+
100+
def test_parse_attrs(self):
101+
# parse_attrs should create a dictionary with correct objects in the
102+
# attributes from the input dictionary
103+
pytest.skip()
104+
105+
def test_parse_attrs_missing_required(self):
106+
# an InputError should be raised if a required parameter is missing
107+
pytest.skip()
108+
109+
def test_call(self):
110+
# calling the instance builder should create the object
111+
pytest.skip()
8112

0 commit comments

Comments
 (0)