From 508f9d064372c5f0ad86ab9a6b12f0c0cb455fb0 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 6 Nov 2024 16:35:00 +1300 Subject: [PATCH] Improve test coverage --- test/test_constraint.jl | 49 +++++++++++++++++++++++++++++++++++++++++ test/test_model.jl | 25 +++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index 04214015ac4..3f800f419df 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -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 diff --git a/test/test_model.jl b/test/test_model.jl index ee4c193e383..d3926b20e8e 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -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