Skip to content

Commit 94d6a0b

Browse files
FBumannpre-commit-ci[bot]FabianHofmann
authored
Improve testing (#482)
* Adjust assert_conequal() to have a no strict mode * Add test * Add release notes * Remove misleading return * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove unintentional runner --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Fabian Hofmann <fab.hof@gmx.de>
1 parent 7c68c9e commit 94d6a0b

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

doc/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Release Notes
33

44
.. Upcoming Version
55
.. ----------------
6+
.. * Improved constraint equality check in `linopy.testing.assert_conequal` to less strict optionally
67
78
Version 0.5.6
89
--------------

linopy/testing.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,25 @@ def assert_quadequal(
2929
return assert_equal(_expr_unwrap(a), _expr_unwrap(b))
3030

3131

32-
def assert_conequal(a: Constraint, b: Constraint) -> None:
33-
"""Assert that two constraints are equal."""
34-
return assert_equal(_con_unwrap(a), _con_unwrap(b))
32+
def assert_conequal(a: Constraint, b: Constraint, strict: bool = True) -> None:
33+
"""
34+
Assert that two constraints are equal.
35+
36+
Parameters
37+
----------
38+
a: Constraint
39+
The first constraint.
40+
b: Constraint
41+
The second constraint.
42+
strict: bool
43+
Whether to compare the constraints strictly. If not, only compare mathematically relevant parts.
44+
"""
45+
if strict:
46+
assert_equal(_con_unwrap(a), _con_unwrap(b))
47+
else:
48+
assert_linequal(a.lhs, b.lhs)
49+
assert_equal(a.sign, b.sign)
50+
assert_equal(a.rhs, b.rhs)
3551

3652

3753
def assert_model_equal(a: Model, b: Model) -> None:

test/test_constraints.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ def test_constraint_assignment() -> None:
4242
assert_conequal(m.constraints.con0, con0)
4343

4444

45+
def test_constraint_equality() -> None:
46+
m: Model = Model()
47+
48+
lower: xr.DataArray = xr.DataArray(
49+
np.zeros((10, 10)), coords=[range(10), range(10)]
50+
)
51+
upper: xr.DataArray = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
52+
x = m.add_variables(lower, upper, name="x")
53+
y = m.add_variables(name="y")
54+
55+
con0 = m.add_constraints(1 * x + 10 * y, EQUAL, 0)
56+
57+
assert_conequal(con0, 1 * x + 10 * y == 0, strict=False)
58+
assert_conequal(1 * x + 10 * y == 0, 1 * x + 10 * y == 0, strict=False)
59+
60+
with pytest.raises(AssertionError):
61+
assert_conequal(con0, 1 * x + 10 * y <= 0, strict=False)
62+
63+
with pytest.raises(AssertionError):
64+
assert_conequal(con0, 1 * x + 10 * y >= 0, strict=False)
65+
66+
with pytest.raises(AssertionError):
67+
assert_conequal(10 * y + 2 * x == 0, 1 * x + 10 * y == 0, strict=False)
68+
69+
4570
def test_constraints_getattr_formatted() -> None:
4671
m: Model = Model()
4772
x = m.add_variables(0, 10, name="x")

0 commit comments

Comments
 (0)