Skip to content

Commit a2bb7bc

Browse files
authored
Merge pull request #790 from JuliaOpt/bl/modif_error
Test modification errors
2 parents 69a6e29 + d95e7f5 commit a2bb7bc

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/modifications.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet,
3-
C<:AbstractFunctionModification} <: NotAllowedError
3+
C<:AbstractFunctionModification} <: NotAllowedError
44
constraint_index::ConstraintIndex{F, S}
55
change::C
66
message::String
@@ -10,15 +10,18 @@ An error indicating that the constraint modification `change` cannot be applied
1010
to the constraint of index `ci`.
1111
"""
1212
struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet,
13-
C<:AbstractFunctionModification} <: NotAllowedError
13+
C<:AbstractFunctionModification} <: NotAllowedError
1414
constraint_index::ConstraintIndex{F, S}
1515
change::C
1616
message::String
1717
end
18-
function ModifyConstraintNotAllowed(ci::ConstraintIndex{F, S},
19-
change::AbstractFunctionModification) where {F<:AbstractFunction, S<:AbstractSet}
20-
ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, "")
18+
function ModifyConstraintNotAllowed(
19+
ci::ConstraintIndex{F, S},
20+
change::AbstractFunctionModification,
21+
message="") where {F<:AbstractFunction, S<:AbstractSet}
22+
return ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, message)
2123
end
24+
throw_modify_not_allowed(ci::ConstraintIndex, args...) = throw(ModifyConstraintNotAllowed(ci, args...))
2225

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

@@ -36,8 +39,9 @@ struct ModifyObjectiveNotAllowed{C<:AbstractFunctionModification} <: NotAllowedE
3639
message::String
3740
end
3841
function ModifyObjectiveNotAllowed(change::AbstractFunctionModification)
39-
ModifyObjectiveNotAllowed(change, "")
42+
return ModifyObjectiveNotAllowed(change, "")
4043
end
44+
throw_modify_not_allowed(::ObjectiveFunction, args...) = throw(ModifyObjectiveNotAllowed(args...))
4145

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

@@ -75,12 +79,12 @@ modify(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstant
7579
"""
7680
function modify end
7781

78-
function modify(model::ModelLike, ci::ConstraintIndex{F, S},
79-
change::AbstractFunctionModification) where {F, S}
80-
throw(ModifyConstraintNotAllowed(ci, change))
82+
function modify(model::ModelLike, ci::ConstraintIndex,
83+
change::AbstractFunctionModification)
84+
throw_modify_not_allowed(ci, change)
8185
end
8286

83-
function modify(model::ModelLike, ::ObjectiveFunction,
84-
change::AbstractFunctionModification)
85-
throw(ModifyObjectiveNotAllowed(change))
87+
function modify(model::ModelLike, attr::ObjectiveFunction,
88+
change::AbstractFunctionModification)
89+
throw_modify_not_allowed(attr, change)
8690
end

test/errors.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
using Test
2+
using MathOptInterface
3+
const MOI = MathOptInterface
4+
5+
include("dummy.jl")
6+
17
@testset "Fallbacks for `set` methods" begin
28
model = DummyModel()
39

@@ -182,6 +188,32 @@
182188
MOI.set(model, MOI.ConstraintSet(), ci, MOI.GreaterThan(1.0))
183189
end
184190
end
191+
192+
@testset "ModifyNotAllowed" begin
193+
change = MOI.ScalarConstantChange(1.0)
194+
@testset "Constraint" begin
195+
err = MOI.ModifyConstraintNotAllowed(ci, change)
196+
@test_throws err MOI.modify(model, ci, change)
197+
@test sprint(showerror, err) == "MathOptInterface.ModifyConstraintNotAllowed{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64},MathOptInterface.ScalarConstantChange{Float64}}:" *
198+
" Modifying the constraints MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64}}(1)" *
199+
" with MathOptInterface.ScalarConstantChange{Float64}(1.0) cannot" *
200+
" be performed. You may want to use a `CachingOptimizer` in" *
201+
" `AUTOMATIC` mode or you may need to call `reset_optimizer`" *
202+
" before doing this operation if the `CachingOptimizer` is in" *
203+
" `MANUAL` mode."
204+
end
205+
@testset "Objective" begin
206+
attr = MOI.ObjectiveFunction{MOI.SingleVariable}()
207+
err = MOI.ModifyObjectiveNotAllowed(change)
208+
@test_throws err MOI.modify(model, attr, change)
209+
@test sprint(showerror, err) == "MathOptInterface.ModifyObjectiveNotAllowed{MathOptInterface.ScalarConstantChange{Float64}}:" *
210+
" Modifying the objective function with MathOptInterface.ScalarConstantChange{Float64}(1.0)" *
211+
" cannot be performed. You may want to use a `CachingOptimizer`" *
212+
" in `AUTOMATIC` mode or you may need to call `reset_optimizer`" *
213+
" before doing this operation if the `CachingOptimizer` is in" *
214+
" `MANUAL` mode."
215+
end
216+
end
185217
end
186218

187219
@testset "Error messages" begin

0 commit comments

Comments
 (0)