-
-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving a bunch of unit tests to pytest. (#4264)
* Moving a bunch of unit tests to pytest Signed-off-by: Pradyot Ranjan <[email protected]> * style: pre-commit fixes * style fix Signed-off-by: Pradyot Ranjan <[email protected]> * Update tests/unit/test_models/test_full_battery_models/test_lithium_ion/test_mpm_half_cell.py Co-authored-by: Agriya Khetarpal <[email protected]> * Update tests/unit/test_models/test_full_battery_models/test_lithium_ion/test_spm.py Co-authored-by: Agriya Khetarpal <[email protected]> * Update tests/unit/test_models/test_full_battery_models/test_lithium_ion/test_spme.py Co-authored-by: Agriya Khetarpal <[email protected]> * Update tests/unit/test_parameters/test_parameter_sets_class.py Co-authored-by: Agriya Khetarpal <[email protected]> * Update tests/unit/test_parameters/test_parameter_sets_class.py Co-authored-by: Agriya Khetarpal <[email protected]> * Update tests/unit/test_plotting/test_plot.py Co-authored-by: Agriya Khetarpal <[email protected]> * adding skips Signed-off-by: Pradyot Ranjan <[email protected]> * Update tests/unit/test_plotting/test_plot.py Co-authored-by: Agriya Khetarpal <[email protected]> * adding reason for skips Signed-off-by: Pradyot Ranjan <[email protected]> --------- Signed-off-by: Pradyot Ranjan <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Agriya Khetarpal <[email protected]>
- Loading branch information
1 parent
6c1a559
commit 4796ba9
Showing
52 changed files
with
423 additions
and
1,022 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,54 @@ | ||
# | ||
# Tests for the Scalar class | ||
# | ||
from tests import TestCase | ||
import pytest | ||
import pybamm | ||
|
||
import unittest | ||
import numpy as np | ||
|
||
|
||
class TestDDT(TestCase): | ||
class TestDDT: | ||
def test_time_derivative(self): | ||
a = pybamm.Scalar(5).diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.Scalar) | ||
self.assertEqual(a.value, 0) | ||
assert isinstance(a, pybamm.Scalar) | ||
assert a.value == 0 | ||
|
||
a = pybamm.t.diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.Scalar) | ||
self.assertEqual(a.value, 1) | ||
assert isinstance(a, pybamm.Scalar) | ||
assert a.value == 1 | ||
|
||
a = (pybamm.t**2).diff(pybamm.t) | ||
self.assertEqual(a, (2 * pybamm.t**1 * 1)) | ||
self.assertEqual(a.evaluate(t=1), 2) | ||
assert a == (2 * pybamm.t**1 * 1) | ||
assert a.evaluate(t=1) == 2 | ||
|
||
a = (2 + pybamm.t**2).diff(pybamm.t) | ||
self.assertEqual(a.evaluate(t=1), 2) | ||
assert a.evaluate(t=1) == 2 | ||
|
||
def test_time_derivative_of_variable(self): | ||
a = (pybamm.Variable("a")).diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.VariableDot) | ||
self.assertEqual(a.name, "a'") | ||
assert isinstance(a, pybamm.VariableDot) | ||
assert a.name == "a'" | ||
|
||
p = pybamm.Parameter("p") | ||
a = 1 + p * pybamm.Variable("a") | ||
diff_a = a.diff(pybamm.t) | ||
self.assertIsInstance(diff_a, pybamm.Multiplication) | ||
self.assertEqual(diff_a.children[0].name, "p") | ||
self.assertEqual(diff_a.children[1].name, "a'") | ||
assert isinstance(diff_a, pybamm.Multiplication) | ||
assert diff_a.children[0].name == "p" | ||
assert diff_a.children[1].name == "a'" | ||
|
||
with self.assertRaises(pybamm.ModelError): | ||
with pytest.raises(pybamm.ModelError): | ||
a = (pybamm.Variable("a")).diff(pybamm.t).diff(pybamm.t) | ||
|
||
def test_time_derivative_of_state_vector(self): | ||
sv = pybamm.StateVector(slice(0, 10)) | ||
y_dot = np.linspace(0, 2, 19) | ||
|
||
a = sv.diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.StateVectorDot) | ||
self.assertEqual(a.name[-1], "'") | ||
assert isinstance(a, pybamm.StateVectorDot) | ||
assert a.name[-1] == "'" | ||
np.testing.assert_array_equal( | ||
a.evaluate(y_dot=y_dot), np.linspace(0, 1, 10)[:, np.newaxis] | ||
) | ||
|
||
with self.assertRaises(pybamm.ModelError): | ||
with pytest.raises(pybamm.ModelError): | ||
a = (sv).diff(pybamm.t).diff(pybamm.t) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Add -v for more debug output") | ||
import sys | ||
|
||
if "-v" in sys.argv: | ||
debug = True | ||
pybamm.settings.debug_mode = True | ||
unittest.main() |
72 changes: 29 additions & 43 deletions
72
tests/unit/test_expression_tree/test_independent_variable.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,73 @@ | ||
# | ||
# Tests for the Parameter class | ||
# | ||
from tests import TestCase | ||
import unittest | ||
|
||
import pytest | ||
|
||
import pybamm | ||
import sympy | ||
|
||
|
||
class TestIndependentVariable(TestCase): | ||
class TestIndependentVariable: | ||
def test_variable_init(self): | ||
a = pybamm.IndependentVariable("a") | ||
self.assertEqual(a.name, "a") | ||
self.assertEqual(a.domain, []) | ||
assert a.name == "a" | ||
assert a.domain == [] | ||
a = pybamm.IndependentVariable("a", domain=["test"]) | ||
self.assertEqual(a.domain[0], "test") | ||
assert a.domain[0] == "test" | ||
a = pybamm.IndependentVariable("a", domain="test") | ||
self.assertEqual(a.domain[0], "test") | ||
with self.assertRaises(TypeError): | ||
assert a.domain[0] == "test" | ||
with pytest.raises(TypeError): | ||
pybamm.IndependentVariable("a", domain=1) | ||
|
||
def test_time(self): | ||
t = pybamm.Time() | ||
self.assertEqual(t.name, "time") | ||
self.assertEqual(t.evaluate(4), 4) | ||
with self.assertRaises(ValueError): | ||
assert t.name == "time" | ||
assert t.evaluate(4) == 4 | ||
with pytest.raises(ValueError): | ||
t.evaluate(None) | ||
|
||
t = pybamm.t | ||
self.assertEqual(t.name, "time") | ||
self.assertEqual(t.evaluate(4), 4) | ||
with self.assertRaises(ValueError): | ||
assert t.name == "time" | ||
assert t.evaluate(4) == 4 | ||
with pytest.raises(ValueError): | ||
t.evaluate(None) | ||
|
||
self.assertEqual(t.evaluate_for_shape(), 0) | ||
assert t.evaluate_for_shape() == 0 | ||
|
||
def test_spatial_variable(self): | ||
x = pybamm.SpatialVariable("x", "negative electrode") | ||
self.assertEqual(x.name, "x") | ||
self.assertFalse(x.evaluates_on_edges("primary")) | ||
assert x.name == "x" | ||
assert not x.evaluates_on_edges("primary") | ||
y = pybamm.SpatialVariable("y", "separator") | ||
self.assertEqual(y.name, "y") | ||
assert y.name == "y" | ||
z = pybamm.SpatialVariable("z", "positive electrode") | ||
self.assertEqual(z.name, "z") | ||
assert z.name == "z" | ||
r = pybamm.SpatialVariable("r", "negative particle") | ||
self.assertEqual(r.name, "r") | ||
with self.assertRaises(NotImplementedError): | ||
assert r.name == "r" | ||
with pytest.raises(NotImplementedError): | ||
x.evaluate() | ||
|
||
with self.assertRaisesRegex(ValueError, "domain must be"): | ||
with pytest.raises(ValueError, match="domain must be"): | ||
pybamm.SpatialVariable("x", []) | ||
with self.assertRaises(pybamm.DomainError): | ||
with pytest.raises(pybamm.DomainError): | ||
pybamm.SpatialVariable("r_n", ["positive particle"]) | ||
with self.assertRaises(pybamm.DomainError): | ||
with pytest.raises(pybamm.DomainError): | ||
pybamm.SpatialVariable("r_p", ["negative particle"]) | ||
with self.assertRaises(pybamm.DomainError): | ||
with pytest.raises(pybamm.DomainError): | ||
pybamm.SpatialVariable("x", ["negative particle"]) | ||
|
||
def test_spatial_variable_edge(self): | ||
x = pybamm.SpatialVariableEdge("x", "negative electrode") | ||
self.assertEqual(x.name, "x") | ||
self.assertTrue(x.evaluates_on_edges("primary")) | ||
assert x.name == "x" | ||
assert x.evaluates_on_edges("primary") | ||
|
||
def test_to_equation(self): | ||
# Test print_name | ||
func = pybamm.IndependentVariable("a") | ||
func.print_name = "test" | ||
self.assertEqual(func.to_equation(), sympy.Symbol("test")) | ||
assert func.to_equation() == sympy.Symbol("test") | ||
|
||
self.assertEqual( | ||
pybamm.IndependentVariable("a").to_equation(), sympy.Symbol("a") | ||
) | ||
assert pybamm.IndependentVariable("a").to_equation() == sympy.Symbol("a") | ||
|
||
# Test time | ||
self.assertEqual(pybamm.t.to_equation(), sympy.Symbol("t")) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Add -v for more debug output") | ||
import sys | ||
|
||
if "-v" in sys.argv: | ||
debug = True | ||
pybamm.settings.debug_mode = True | ||
unittest.main() | ||
assert pybamm.t.to_equation() == sympy.Symbol("t") |
Oops, something went wrong.