Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #3874

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2171,4 +2171,51 @@ 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
42 changes: 42 additions & 0 deletions test/test_nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1683,4 +1683,46 @@ function test_get_node_type_comparison()
return
end

function test_NL_dot()
model = Model()
@variable(model, x)
y = (; abc = x)
@NLexpression(model, ref, y.abc)
nlp = nonlinear_model(model)
@test MOI.Nonlinear.parse_expression(nlp, x) == nlp[index(ref)]
return
end

function test_NL_adjoint()
model = Model()
@variable(model, x)
@NLexpression(model, ref, x')
nlp = nonlinear_model(model)
@test MOI.Nonlinear.parse_expression(nlp, x) == nlp[index(ref)]
return
end

function test_NL_too_many_arguments_errors()
model = Model()
@test_throws_parsetime(
ErrorException(
"In `@NLexpression()`: To few arguments (0); must pass the model and nonlinear expression as arguments.",
),
@NLexpression(),
)
@test_throws_parsetime(
ErrorException(
"In `@NLexpression(model, ex, 1, 2)`: To many arguments (4).",
),
@NLexpression(model, ex, 1, 2),
)
@test_throws_parsetime(
ErrorException(
"In `@NLconstraint(model, ex, 1, 2)`: too many arguments.",
),
@NLconstraint(model, ex, 1, 2),
)
return
end

end
Loading