Skip to content

Commit

Permalink
objective class: increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianHofmann committed Oct 24, 2023
1 parent 6649886 commit ed67bb4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 0 additions & 3 deletions linopy/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ def expression(self, expr):
if isinstance(expr, (list, tuple)):
expr = self.model.linexpr(*expr)

if isinstance(expr, Objective):
expr = expr.expression

if not isinstance(
expr, (expressions.LinearExpression, expressions.QuadraticExpression)
):
Expand Down
24 changes: 24 additions & 0 deletions test/test_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,24 @@ def test_add(linear_objective, quadratic_objective):
assert isinstance(obj.expression, QuadraticExpression)


def test_add_expr(linear_objective, quadratic_objective):
obj = linear_objective + quadratic_objective.expression
assert isinstance(obj, Objective)
assert isinstance(obj.expression, QuadraticExpression)


def test_sub(linear_objective, quadratic_objective):
obj = quadratic_objective - linear_objective
assert isinstance(obj, Objective)
assert isinstance(obj.expression, QuadraticExpression)


def test_sub_epxr(linear_objective, quadratic_objective):
obj = quadratic_objective - linear_objective.expression
assert isinstance(obj, Objective)
assert isinstance(obj.expression, QuadraticExpression)


def test_mul(quadratic_objective):
obj = quadratic_objective * 2
assert isinstance(obj, Objective)
Expand All @@ -135,9 +147,21 @@ def test_truediv(quadratic_objective):
assert isinstance(obj.expression, QuadraticExpression)


def test_truediv_false(quadratic_objective):
with pytest.raises(ValueError):
quadratic_objective / quadratic_objective


def test_repr(linear_objective, quadratic_objective):
assert isinstance(linear_objective.__repr__(), str)
assert isinstance(quadratic_objective.__repr__(), str)

assert "Linear" in linear_objective.__repr__()
assert "Quadratic" in quadratic_objective.__repr__()


def test_objective_constant():
m = Model()
linear_expr = LinearExpression(None, m) + 1
with pytest.raises(ValueError):
m.objective = Objective(linear_expr, m)
3 changes: 3 additions & 0 deletions test/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ def test_default_setting(model, solver, io_api):
assert status == "ok"
assert np.isclose(model.objective.value, 3.3)

with pytest.warns(DeprecationWarning):
assert np.isclose(model.objective_value, 3.3)


@pytest.mark.parametrize("solver,io_api", params)
def test_default_setting_sol_and_dual_accessor(model, solver, io_api):
Expand Down

0 comments on commit ed67bb4

Please sign in to comment.