Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Nov 6, 2024
1 parent b016cdd commit 508f9d0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2171,4 +2171,53 @@ function test_constraint_by_name()
return
end

function test_shadow_price_errors()
model = Model()
@variable(model, x[1:2])
c = @constraint(model, x in SOS1())
@test_throws(
ErrorException(
"The shadow price is not defined or not implemented for this type " *
"of constraint.",
),
shadow_price(c),
)
err = ErrorException(
"The shadow price is not available because no dual result is " *
"available.",
)
model = Model()
@variable(model, x)
c = @constraint(model, x <= 1)
@test_throws err shadow_price(c)
c = @constraint(model, x >= 1)
@test_throws err shadow_price(c)
c = @constraint(model, x == 1)
@test_throws err shadow_price(c)

model = Model()
@variable(model, x >= 0)
set_optimizer(
model,
() -> MOI.Utilities.MockOptimizer(
MOI.Utilities.Model{Float64}(),
),
)
optimize!(model)
mock = unsafe_backend(model)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.DualStatus(), MOI.FEASIBLE_POINT)
F, S = MOI.VariableIndex, MOI.GreaterThan{Float64}
xi = only(MOI.get(mock, MOI.ListOfVariableIndices()))
MOI.set(mock, MOI.ConstraintDual(), MOI.ConstraintIndex{F,S}(xi.value), 1.0)
@test_throws(
ErrorException(
"The shadow price is not available because the objective sense $FEASIBILITY_SENSE is not minimization or maximization.",
),
shadow_price(LowerBoundRef(x)),
)
return
end

end # module
25 changes: 25 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1364,4 +1364,29 @@ function test_optimizer_index()
return
end

function test_copy_nonlinear()
model = Model()
@variable(model, x)
@NLconstraint(model, x <= 1)
@test_throws(
ErrorException(
"copy is not supported yet for models with nonlinear constraints" *
" and/or nonlinear objective function"
),
copy_model(model),
)
return
end

function test_deepcopy()
model = Model()
@test_throws(
ErrorException(
"`JuMP.Model` does not support `deepcopy` as the reference to the underlying solver cannot be deep copied, use `copy` instead.",
),
deepcopy(model),
)
return
end

end # module TestModels

0 comments on commit 508f9d0

Please sign in to comment.