Skip to content

Test modification errors #790

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

Merged
merged 2 commits into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 15 additions & 11 deletions src/modifications.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet,
C<:AbstractFunctionModification} <: NotAllowedError
C<:AbstractFunctionModification} <: NotAllowedError
constraint_index::ConstraintIndex{F, S}
change::C
message::String
Expand All @@ -10,15 +10,18 @@ An error indicating that the constraint modification `change` cannot be applied
to the constraint of index `ci`.
"""
struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet,
C<:AbstractFunctionModification} <: NotAllowedError
C<:AbstractFunctionModification} <: NotAllowedError
constraint_index::ConstraintIndex{F, S}
change::C
message::String
end
function ModifyConstraintNotAllowed(ci::ConstraintIndex{F, S},
change::AbstractFunctionModification) where {F<:AbstractFunction, S<:AbstractSet}
ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, "")
function ModifyConstraintNotAllowed(
ci::ConstraintIndex{F, S},
change::AbstractFunctionModification,
message="") where {F<:AbstractFunction, S<:AbstractSet}
ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, message)
end
throw_modify_not_allowed(ci::ConstraintIndex, args...) = throw(ModifyConstraintNotAllowed(ci, args...))

operation_name(err::ModifyConstraintNotAllowed{F, S}) where {F, S} = "Modifying the constraints $(err.constraint_index) with $(err.change)"

Expand All @@ -38,6 +41,7 @@ end
function ModifyObjectiveNotAllowed(change::AbstractFunctionModification)
ModifyObjectiveNotAllowed(change, "")
end
throw_modify_not_allowed(::ObjectiveFunction, args...) = throw(ModifyObjectiveNotAllowed(args...))

operation_name(err::ModifyObjectiveNotAllowed) = "Modifying the objective function with $(err.change)"

Expand Down Expand Up @@ -75,12 +79,12 @@ modify(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstant
"""
function modify end

function modify(model::ModelLike, ci::ConstraintIndex{F, S},
change::AbstractFunctionModification) where {F, S}
throw(ModifyConstraintNotAllowed(ci, change))
function modify(model::ModelLike, ci::ConstraintIndex,
change::AbstractFunctionModification)
throw_modify_not_allowed(ci, change)
end

function modify(model::ModelLike, ::ObjectiveFunction,
change::AbstractFunctionModification)
throw(ModifyObjectiveNotAllowed(change))
function modify(model::ModelLike, attr::ObjectiveFunction,
change::AbstractFunctionModification)
throw_modify_not_allowed(attr, change)
end
32 changes: 32 additions & 0 deletions test/errors.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
using Test
using MathOptInterface
const MOI = MathOptInterface

include("dummy.jl")

@testset "Fallbacks for `set` methods" begin
model = DummyModel()

Expand Down Expand Up @@ -182,6 +188,32 @@
MOI.set(model, MOI.ConstraintSet(), ci, MOI.GreaterThan(1.0))
end
end

@testset "ModifyNotAllowed" begin
change = MOI.ScalarConstantChange(1.0)
@testset "Constraint" begin
err = MOI.ModifyConstraintNotAllowed(ci, change)
@test_throws err MOI.modify(model, ci, change)
@test sprint(showerror, err) == "MathOptInterface.ModifyConstraintNotAllowed{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64},MathOptInterface.ScalarConstantChange{Float64}}:" *
" Modifying the constraints MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64}}(1)" *
" with MathOptInterface.ScalarConstantChange{Float64}(1.0) cannot" *
" be performed. You may want to use a `CachingOptimizer` in" *
" `AUTOMATIC` mode or you may need to call `reset_optimizer`" *
" before doing this operation if the `CachingOptimizer` is in" *
" `MANUAL` mode."
end
@testset "Objective" begin
attr = MOI.ObjectiveFunction{MOI.SingleVariable}()
err = MOI.ModifyObjectiveNotAllowed(change)
@test_throws err MOI.modify(model, attr, change)
@test sprint(showerror, err) == "MathOptInterface.ModifyObjectiveNotAllowed{MathOptInterface.ScalarConstantChange{Float64}}:" *
" Modifying the objective function with MathOptInterface.ScalarConstantChange{Float64}(1.0)" *
" cannot be performed. You may want to use a `CachingOptimizer`" *
" in `AUTOMATIC` mode or you may need to call `reset_optimizer`" *
" before doing this operation if the `CachingOptimizer` is in" *
" `MANUAL` mode."
end
end
end

@testset "Error messages" begin
Expand Down