Skip to content

Commit 14f4fda

Browse files
committed
improve test coverage
1 parent a63ea0f commit 14f4fda

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

test/test_common.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
import polars as pl
1111
import pytest
1212
import xarray as xr
13-
from test_linear_expression import m, u, x # noqa: F401
1413
from xarray import DataArray
1514
from xarray.testing.assertions import assert_equal
1615

17-
from linopy import LinearExpression, Variable
16+
from linopy import LinearExpression, Model, Variable
1817
from linopy.common import (
1918
align,
2019
as_dataarray,
2120
assign_multiindex_safe,
2221
best_int,
2322
get_dims_with_index_levels,
23+
is_a_constant,
2424
iterate_slices,
2525
)
2626
from linopy.testing import assert_linequal, assert_varequal
@@ -711,3 +711,27 @@ def test_align(x: Variable, u: Variable) -> None: # noqa: F811
711711
assert expr_obs.shape == (1, 1) # _term dim
712712
assert isinstance(expr_obs, LinearExpression)
713713
assert_linequal(expr_obs, expr.loc[[1]])
714+
715+
716+
def test_is_a_constant() -> None:
717+
m = Model()
718+
index = pd.Index(range(10), name="t")
719+
a = m.add_variables(name="a", coords=[index])
720+
b = a.sel(t=1)
721+
c = a * 2
722+
d = a * a
723+
724+
non_constant = [a, b, c, d]
725+
for nc in non_constant:
726+
assert not is_a_constant(nc)
727+
728+
constant_values = [
729+
5,
730+
3.14,
731+
np.int32(7),
732+
np.float64(2.71),
733+
pd.Series([1, 2, 3]),
734+
np.array([4, 5, 6], xr.DataArray([k for k in range(10)], coords=[index])),
735+
]
736+
for cv in constant_values:
737+
assert is_a_constant(cv)

test/test_variable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def test_variable_upper_setter_with_array_invalid_dim(x: linopy.Variable) -> Non
149149
x.upper = upper
150150

151151

152+
def test_variable_lower_setter_with_non_constant(z: linopy.Variable) -> None:
153+
with pytest.raises(TypeError):
154+
z.upper = z
155+
156+
152157
def test_variable_lower_setter_with_array(x: linopy.Variable) -> None:
153158
idx = pd.RangeIndex(10, name="first")
154159
lower = pd.Series(range(15, 25), index=idx)

0 commit comments

Comments
 (0)