Skip to content

Commit 9799e65

Browse files
committed
refactor tests to not use modules
1 parent 8a06fec commit 9799e65

File tree

7 files changed

+90
-183
lines changed

7 files changed

+90
-183
lines changed

test/get_equality.jl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
# This software is distributed under the 3-clause BSD license.
1818
# ___________________________________________________________________________
1919

20-
module TestGetEquality
2120
import JuMP
22-
import MathOptInterface as moi
23-
using Test: @test, @test_throws
21+
import MathOptInterface as MOI
22+
using Test: @test, @test_throws, @testset
2423
using JuMPIn: get_equality_constraints
2524

26-
include("models.jl") # Models
25+
include("models.jl") # make_degenerate_flow_model
2726

2827
function get_flow_model_with_inequalities()
29-
m = Models.make_degenerate_flow_model()
30-
# TODO: Some assertions with the @assert macro
28+
m = make_degenerate_flow_model()
3129
@JuMP.constraint(m, ineq1, m[:x][1] >= 0)
3230
@JuMP.constraint(m, ineq2, m[:x][1]^2 + m[:x][2]^2 <= 0.5)
3331
@JuMP.NLconstraint(m, ineq3, sqrt(m[:x][3]) >= 0.1)
@@ -44,7 +42,7 @@ end
4442
function test_with_vector_constraint()
4543
m = JuMP.Model()
4644
@JuMP.variable(m, var[1:2])
47-
@JuMP.constraint(m, con, var in moi.Nonnegatives(2))
45+
@JuMP.constraint(m, con, var in MOI.Nonnegatives(2))
4846
@test_throws(TypeError, eq_cons = get_equality_constraints(m))
4947
end
5048

@@ -59,14 +57,8 @@ function test_with_fixed_variables()
5957
@test length(eq_cons) == 1
6058
end
6159

62-
function runtests()
60+
@testset "get-equality" begin
6361
test_flow_model_with_inequalities()
6462
test_with_vector_constraint()
6563
test_with_fixed_variables()
6664
end
67-
68-
end # module TestGetEquality
69-
70-
if abspath(PROGRAM_FILE) == @__FILE__
71-
TestGetEquality.runtests()
72-
end

test/identify_variables.jl

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
# This software is distributed under the 3-clause BSD license.
1818
# ___________________________________________________________________________
1919

20-
module TestIdentifyVariables
21-
import JuMP as jmp
22-
import MathOptInterface as moi
20+
import JuMP
21+
import MathOptInterface as MOI
2322
using Test: @testset, @test, @test_throws
2423
using JuMPIn: identify_unique_variables
2524

2625
# Local import of JuMP models for testing
27-
include("models.jl")
28-
using .Models: make_degenerate_flow_model
29-
26+
include("models.jl") # make_degenerate_flow_model
3027

3128
function test_linear()
3229
m = make_degenerate_flow_model()
@@ -55,9 +52,9 @@ function test_nonlinear()
5552
end
5653

5754
function test_nonlinear_with_potential_duplicates()
58-
m = jmp.Model()
59-
@jmp.variable(m, var[1:2])
60-
@jmp.NLconstraint(m, con, var[1]^3 + var[1]*var[2] == 1.0)
55+
m = JuMP.Model()
56+
@JuMP.variable(m, var[1:2])
57+
@JuMP.NLconstraint(m, con, var[1]^3 + var[1]*var[2] == 1.0)
6158
variables = identify_unique_variables(con)
6259
@test(length(variables) == 2)
6360
@test(Set(variables) == Set([m[:var][1], m[:var][2]]))
@@ -76,8 +73,8 @@ end
7673

7774
function test_several_constraints_with_ineq()
7875
m = make_degenerate_flow_model()
79-
@jmp.constraint(m, ineq1, m[:flow_comp][2] >= 1.0)
80-
@jmp.constraint(m, ineq2, m[:flow_comp][1]^2 + m[:flow_comp][3]^2 <= 1.0)
76+
@JuMP.constraint(m, ineq1, m[:flow_comp][2] >= 1.0)
77+
@JuMP.constraint(m, ineq2, m[:flow_comp][1]^2 + m[:flow_comp][3]^2 <= 1.0)
8178
constraints = [
8279
m[:comp_flow_eqn][1],
8380
m[:sum_comp_eqn],
@@ -102,8 +99,8 @@ end
10299

103100
function test_model()
104101
m = make_degenerate_flow_model()
105-
@jmp.variable(m, dummy)
106-
@jmp.NLconstraint(m, dummy^3.0 <= 5)
102+
@JuMP.variable(m, dummy)
103+
@JuMP.NLconstraint(m, dummy^3.0 <= 5)
107104
# Note that include_inequalities=false by default.
108105
variables = identify_unique_variables(m)
109106
pred_var_set = Set([
@@ -122,8 +119,8 @@ end
122119

123120
function test_model_with_ineq()
124121
m = make_degenerate_flow_model()
125-
@jmp.variable(m, dummy)
126-
@jmp.NLconstraint(m, dummy^3.0 <= 5)
122+
@JuMP.variable(m, dummy)
123+
@JuMP.NLconstraint(m, dummy^3.0 <= 5)
127124
variables = identify_unique_variables(m, include_inequality=true)
128125
pred_var_set = Set([
129126
m[:dummy],
@@ -141,20 +138,20 @@ function test_model_with_ineq()
141138
end
142139

143140
function test_function_with_variable_squared()
144-
m = jmp.Model()
145-
@jmp.variable(m, dummy1)
146-
@jmp.variable(m, dummy2)
147-
@jmp.constraint(m, dummy_con, dummy1^2 + dummy1*dummy2 == 2.0)
148-
fcn = moi.get(m, moi.ConstraintFunction(), m[:dummy_con])
141+
m = JuMP.Model()
142+
@JuMP.variable(m, dummy1)
143+
@JuMP.variable(m, dummy2)
144+
@JuMP.constraint(m, dummy_con, dummy1^2 + dummy1*dummy2 == 2.0)
145+
fcn = MOI.get(m, MOI.ConstraintFunction(), m[:dummy_con])
149146
variables = identify_unique_variables(fcn)
150147
@test(length(variables) == 2)
151148
@test(Set(variables) == Set([m[:dummy1].index, m[:dummy2].index]))
152149
end
153150

154151
function test_model_bad_constr()
155152
m = make_degenerate_flow_model()
156-
@jmp.variable(m, dummy[1:2])
157-
@jmp.constraint(m, vectorcon, dummy in moi.Nonnegatives(2))
153+
@JuMP.variable(m, dummy[1:2])
154+
@JuMP.constraint(m, vectorcon, dummy in MOI.Nonnegatives(2))
158155
@test_throws(
159156
TypeError,
160157
identify_unique_variables(m, include_inequality=true),
@@ -163,15 +160,15 @@ end
163160

164161
function test_model_bad_constr_no_ineq()
165162
m = make_degenerate_flow_model()
166-
@jmp.variable(m, dummy[1:2])
167-
@jmp.constraint(m, vectorcon, dummy in moi.Nonnegatives(2))
163+
@JuMP.variable(m, dummy[1:2])
164+
@JuMP.constraint(m, vectorcon, dummy in MOI.Nonnegatives(2))
168165
# Note that we don't throw an error because we don't attempt to
169166
# identify variables in "vectorcon" as we don't recognize it as an
170167
# equality constraint.
171168
#
172169
# NOTE: We now do throw an error. I have added a method of
173170
# set_implies_equality that throws an error if it is provided with a
174-
# subtype of moi.VectorSet. This is not ~strictly~ correct. However,
171+
# subtype of MOI.VectorSet. This is not ~strictly~ correct. However,
175172
# I have punted for now.
176173
@test_throws(
177174
TypeError,
@@ -180,37 +177,37 @@ function test_model_bad_constr_no_ineq()
180177
end
181178

182179
function test_fixing_constraint()
183-
m = jmp.Model()
184-
@jmp.variable(m, x[1:2])
185-
jmp.fix(x[1], 1)
186-
fixing_con = jmp.FixRef(x[1])
180+
m = JuMP.Model()
181+
@JuMP.variable(m, x[1:2])
182+
JuMP.fix(x[1], 1)
183+
fixing_con = JuMP.FixRef(x[1])
187184
variables = identify_unique_variables(fixing_con)
188185
@test length(variables) == 1
189186
@test variables[1] === x[1]
190187
end
191188

192189
function test_inequality_with_bounds()
193-
m = jmp.Model()
194-
@jmp.variable(m, x[1:2])
195-
@jmp.variable(m, 0 <= y[1:2])
196-
@jmp.constraint(m, x[1] + 2*x[2]^2 == 1)
190+
m = JuMP.Model()
191+
@JuMP.variable(m, x[1:2])
192+
@JuMP.variable(m, 0 <= y[1:2])
193+
@JuMP.constraint(m, x[1] + 2*x[2]^2 == 1)
197194
variables = identify_unique_variables(m, include_inequality = true)
198195
pred_var_set = Set([x[1], x[2], y[1], y[2]])
199196
@test Set(variables) == pred_var_set
200197
end
201198

202199
function test_two_constraints_same_type()
203-
m = jmp.Model()
204-
@jmp.variable(m, x[1:3])
205-
@jmp.constraint(m, eq1, x[1] + x[2] == 2)
206-
@jmp.constraint(m, eq2, x[2] + 2*x[3] == 3)
200+
m = JuMP.Model()
201+
@JuMP.variable(m, x[1:3])
202+
@JuMP.constraint(m, eq1, x[1] + x[2] == 2)
203+
@JuMP.constraint(m, eq2, x[2] + 2*x[3] == 3)
207204
cons = [eq1, eq2]
208205
vars = identify_unique_variables(cons)
209206
pred_var_set = Set([x[1], x[2], x[3]])
210207
@test Set(vars) == pred_var_set
211208
end
212209

213-
function runtests()
210+
@testset "get-equality" begin
214211
test_linear()
215212
test_quadratic()
216213
test_nonlinear()
@@ -225,11 +222,4 @@ function runtests()
225222
test_fixing_constraint()
226223
test_inequality_with_bounds()
227224
test_two_constraints_same_type()
228-
return
229-
end
230-
231-
end
232-
233-
if abspath(PROGRAM_FILE) == @__FILE__
234-
TestIdentifyVariables.runtests()
235225
end

test/incidence_graph.jl

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
# This software is distributed under the 3-clause BSD license.
1818
# ___________________________________________________________________________
1919

20-
module TestIncidenceGraph
21-
22-
import JuMP as jmp
23-
import MathOptInterface as moi
24-
using Test: @test, @test_throws
20+
import JuMP
21+
import MathOptInterface as MOI
22+
using Test: @test, @test_throws, @testset
2523
using JuMPIn: get_bipartite_incidence_graph
2624

27-
include("models.jl") # Models
28-
using .Models: make_degenerate_flow_model
25+
include("models.jl") # make_degenerate_flow_model
2926

3027
function test_get_incidence_graph()
3128
m = make_degenerate_flow_model()
@@ -84,8 +81,8 @@ end
8481

8582
function test_get_incidence_graph_badconstraint()
8683
m = make_degenerate_flow_model()
87-
@jmp.variable(m, var[1:2])
88-
@jmp.constraint(m, vectorcon, var in moi.Nonnegatives(2))
84+
@JuMP.variable(m, var[1:2])
85+
@JuMP.constraint(m, vectorcon, var in MOI.Nonnegatives(2))
8986
@test_throws(
9087
TypeError,
9188
get_bipartite_incidence_graph(m, include_inequality=true),
@@ -94,26 +91,26 @@ function test_get_incidence_graph_badconstraint()
9491
end
9592

9693
function test_include_bound_as_inequality()
97-
m = jmp.Model()
98-
@jmp.variable(m, 0 <= x[1:2])
99-
@jmp.constraint(m, eq1, x[1] + 2*x[2] == 1)
94+
m = JuMP.Model()
95+
@JuMP.variable(m, 0 <= x[1:2])
96+
@JuMP.constraint(m, eq1, x[1] + 2*x[2] == 1)
10097
graph, con_node_map, var_node_map = get_bipartite_incidence_graph(
10198
m, include_inequality = true
10299
)
103100
A, B, E = graph
104101
@test length(A) == 3
105102
@test length(B) == 2
106103
@test length(E) == 4
107-
pred_con_set = Set([eq1, jmp.LowerBoundRef(x[1]), jmp.LowerBoundRef(x[2])])
104+
pred_con_set = Set([eq1, JuMP.LowerBoundRef(x[1]), JuMP.LowerBoundRef(x[2])])
108105
@test pred_con_set == keys(con_node_map)
109106
return
110107
end
111108

112109
function test_construct_from_constraints()
113-
m = jmp.Model()
114-
@jmp.variable(m, x[1:3])
115-
@jmp.constraint(m, eq1, x[1] + 2*x[2] == 1)
116-
@jmp.constraint(m, eq2, x[2]*x[3] == 0.5)
110+
m = JuMP.Model()
111+
@JuMP.variable(m, x[1:3])
112+
@JuMP.constraint(m, eq1, x[1] + 2*x[2] == 1)
113+
@JuMP.constraint(m, eq2, x[2]*x[3] == 0.5)
117114
cons = [eq1, eq2]
118115
graph, con_node_map, var_node_map = get_bipartite_incidence_graph(cons)
119116
A, B, E = graph
@@ -131,10 +128,10 @@ function test_construct_from_constraints()
131128
end
132129

133130
function test_construct_from_constraints_and_variables()
134-
m = jmp.Model()
135-
@jmp.variable(m, x[1:3])
136-
@jmp.constraint(m, eq1, x[1] + 2*x[2] == 1)
137-
@jmp.constraint(m, eq2, x[2]*x[3] == 0.5)
131+
m = JuMP.Model()
132+
@JuMP.variable(m, x[1:3])
133+
@JuMP.constraint(m, eq1, x[1] + 2*x[2] == 1)
134+
@JuMP.constraint(m, eq2, x[2]*x[3] == 0.5)
138135
cons = [eq2, eq1]
139136
vars = [x[3], x[2], x[1]]
140137
graph, con_node_map, var_node_map = get_bipartite_incidence_graph(cons, vars)
@@ -155,17 +152,10 @@ function test_construct_from_constraints_and_variables()
155152
return
156153
end
157154

158-
function runtests()
155+
@testset "incidence-graph" begin
159156
test_get_incidence_graph()
160157
test_get_incidence_graph_badconstraint()
161158
test_include_bound_as_inequality()
162159
test_construct_from_constraints()
163160
test_construct_from_constraints_and_variables()
164-
return
165-
end
166-
167-
end # module TestIncidenceGraph
168-
169-
if abspath(PROGRAM_FILE) == @__FILE__
170-
TestIncidenceGraph.runtests()
171161
end

test/incidence_matrix.jl

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,19 @@
1717
# This software is distributed under the 3-clause BSD license.
1818
# ___________________________________________________________________________
1919

20-
module TestIncidenceMatrix
21-
22-
import JuMP as jmp
23-
import MathOptInterface as moi
20+
import JuMP
2421
import SparseArrays
25-
using Test: @test, @test_throws
22+
using Test: @test, @test_throws, @testset
2623

2724
import JuMPIn as ji
2825

29-
include("models.jl") # Models
30-
using .Models: make_degenerate_flow_model
31-
26+
include("models.jl") # make_degenerate_flow_model
3227

3328
function test_incidence_matrix_from_constraints_and_variables()
34-
m = jmp.Model()
35-
@jmp.variable(m, x[1:3])
36-
@jmp.constraint(m, eq1, 2*x[1] + 3*x[2] == 4)
37-
@jmp.NLconstraint(m, eq2, 2*x[3]^1.5*x[2] == 1)
29+
m = JuMP.Model()
30+
@JuMP.variable(m, x[1:3])
31+
@JuMP.constraint(m, eq1, 2*x[1] + 3*x[2] == 4)
32+
@JuMP.NLconstraint(m, eq2, 2*x[3]^1.5*x[2] == 1)
3833
constraints = [eq1, eq2]
3934
variables = [x[1], x[2], x[3]]
4035
imat = ji.incidence_matrix(constraints, variables)
@@ -48,10 +43,10 @@ function test_incidence_matrix_from_constraints_and_variables()
4843
end
4944

5045
function test_incidence_matrix_from_incidence_graph()
51-
m = jmp.Model()
52-
@jmp.variable(m, x[1:3])
53-
@jmp.constraint(m, eq1, 2*x[1] + 3*x[2] == 4)
54-
@jmp.NLconstraint(m, eq2, 2*x[3]^1.5*x[2] == 1)
46+
m = JuMP.Model()
47+
@JuMP.variable(m, x[1:3])
48+
@JuMP.constraint(m, eq1, 2*x[1] + 3*x[2] == 4)
49+
@JuMP.NLconstraint(m, eq2, 2*x[3]^1.5*x[2] == 1)
5550
constraints = [eq1, eq2]
5651
variables = [x[3], x[2], x[1]]
5752
igraph = ji.IncidenceGraphInterface(constraints, variables)
@@ -65,15 +60,7 @@ function test_incidence_matrix_from_incidence_graph()
6560
@test imat == pred_mat
6661
end
6762

68-
69-
function runtests()
63+
@testset "incidence-matrix" begin
7064
test_incidence_matrix_from_constraints_and_variables()
7165
test_incidence_matrix_from_incidence_graph()
72-
return
73-
end
74-
75-
end # module TestIncidenceGraph
76-
77-
if abspath(PROGRAM_FILE) == @__FILE__
78-
TestIncidenceMatrix.runtests()
7966
end

0 commit comments

Comments
 (0)