Skip to content

Commit

Permalink
#2901 coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed May 19, 2023
1 parent c6aac11 commit 25ddb12
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 7 additions & 8 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, values, chemistry=None):
}
)

if isinstance(values, dict):
if isinstance(values, (dict, ParameterValues)):
# remove the "chemistry" key if it exists
values.pop("chemistry", None)
self.update(values, check_already_exists=False)
Expand All @@ -69,13 +69,9 @@ def __init__(self, values, chemistry=None):
self._processed_symbols = {}

# save citations
citations = []
if hasattr(self, "citations"):
citations = self.citations
elif "citations" in self._dict_items:
citations = self._dict_items["citations"]
for citation in citations:
pybamm.citations.register(citation)
if "citations" in self._dict_items:
for citation in self._dict_items["citations"]:
pybamm.citations.register(citation)

@staticmethod
def create_from_bpx(filename, target_soc=1):
Expand Down Expand Up @@ -145,6 +141,9 @@ def items(self):
"""Get the items of the dictionary"""
return self._dict_items.items()

def pop(self, *args, **kwargs):
self._dict_items.pop(*args, **kwargs)

def copy(self):
"""Returns a copy of the parameter values. Makes sure to copy the internal
dictionary."""
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_parameters/test_parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def test_init(self):
self.assertEqual(param["a"], 1)
self.assertIn("a", param.keys())
self.assertIn(1, param.values())
self.assertIn(("a", 1), param.items())

# from dict with strings
param = pybamm.ParameterValues({"a": "1"})
self.assertEqual(param["a"], 1)

# from dict "chemistry" key gets removed
param = pybamm.ParameterValues({"a": 1, "chemistry": "lithium-ion"})
Expand Down Expand Up @@ -84,6 +89,10 @@ def test_update(self):
with self.assertRaisesRegex(KeyError, "Cannot update parameter"):
param.update({"b": 1})

# udpate with a ParameterValues object
new_param = pybamm.ParameterValues(param)
self.assertEqual(new_param["a"], 2)

# test deleting a parameter
del param["a"]
self.assertNotIn("a", param.keys())
Expand Down Expand Up @@ -384,6 +393,10 @@ def my_func(x):
self.assertEqual(func1.domains, func2.domains)
self.assertEqual(func1.domains, func3.domains)

# [function] is deprecated
with self.assertRaisesRegex(ValueError, "[function]"):
pybamm.ParameterValues({"func": "[function]something"})

def test_process_inline_function_parameters(self):
def D(c):
return c**2
Expand Down

0 comments on commit 25ddb12

Please sign in to comment.