Skip to content

Commit

Permalink
Put tests in separate files (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
clizbe authored Oct 25, 2023
1 parent 528c67a commit c245e1c
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/TulipaEnergyModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Graphs
using HiGHS
using JuMP

include("input_tables.jl")
include("input-tables.jl")
include("io.jl")
include("model.jl")
include("time-resolution.jl")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
114 changes: 5 additions & 109 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,116 +8,12 @@ using Test
const INPUT_FOLDER = joinpath(@__DIR__, "inputs")
const OUTPUT_FOLDER = joinpath(@__DIR__, "outputs")

@testset "TulipaEnergyModel.jl" begin
dir = joinpath(INPUT_FOLDER, "Norse")
parameters, sets = create_parameters_and_sets_from_file(dir)
graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))
model = create_model(graph, parameters, sets)
solution = solve_model(model)
@test solution.objective_value 183696778.98322 atol = 1e-5
save_solution_to_file(
OUTPUT_FOLDER,
sets.assets_investment,
solution.assets_investment,
parameters.assets_unit_capacity,
)
end

@testset "Write lp file" begin
dir = joinpath(INPUT_FOLDER, "tiny")
parameters, sets = create_parameters_and_sets_from_file(dir)
graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))
model = create_model(graph, parameters, sets; write_lp_file = true)
solution = solve_model(model)
@test solution.objective_value 269238.43825 atol = 1e-5
save_solution_to_file(
OUTPUT_FOLDER,
sets.assets_investment,
solution.assets_investment,
parameters.assets_unit_capacity,
)
end

@testset "Infeasible run" begin
dir = joinpath(INPUT_FOLDER, "tiny")
parameters, sets = create_parameters_and_sets_from_file(dir)
parameters.peak_demand["demand"] = -1 # make it infeasible
graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))
model = create_model(graph, parameters, sets)
solution = solve_model(model)
@test solution === nothing
end

@testset "Tiny graph" begin
@testset "Graph structure is correct" begin
dir = joinpath(INPUT_FOLDER, "tiny")
graph =
create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))

@test Graphs.nv(graph) == 6
@test Graphs.ne(graph) == 5
@test collect(Graphs.edges(graph)) ==
[Graphs.Edge(e) for e in [(1, 6), (2, 6), (3, 6), (4, 6), (5, 6)]]
end
end

@testset "Input validation" begin
@testset "Make sure that input validation fails for bad files" begin
dir = joinpath(INPUT_FOLDER, "tiny")
@test_throws ArgumentError TulipaEnergyModel.read_csv_with_schema(
joinpath(dir, "bad-assets-data.csv"),
TulipaEnergyModel.AssetData,
)
end
end

@testset "Time resolution" begin
@testset "resolution_matrix" begin
rp_periods = [1:4, 5:8, 9:12]
time_steps = [1:4, 5:8, 9:12]
expected = [
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
]
@test resolution_matrix(rp_periods, time_steps) == expected

time_steps = [1:3, 4:6, 7:9, 10:12]
expected = [
1.0 1/3 0.0 0.0
0.0 2/3 2/3 0.0
0.0 0.0 1/3 1.0
]
@test resolution_matrix(rp_periods, time_steps) == expected

time_steps = [1:6, 7:9, 10:10, 11:11, 12:12]
expected = [
2/3 0.0 0.0 0.0 0.0
1/3 2/3 0.0 0.0 0.0
0.0 1/3 1.0 1.0 1.0
]
@test resolution_matrix(rp_periods, time_steps) == expected
end

@testset "compute_rp_periods" begin
# regular
time_steps1 = [1:4, 5:8, 9:12] # every 4 hours
time_steps2 = [1:3, 4:6, 7:9, 10:12] # every 3 hours
time_steps3 = [i:i for i = 1:12] # hourly

@test compute_rp_periods([time_steps1, time_steps2]) == time_steps1
@test compute_rp_periods([time_steps1, time_steps2, time_steps3]) == time_steps1
@test compute_rp_periods([time_steps2, time_steps3]) == time_steps2

# Irregular
time_steps4 = [1:6, 7:9, 10:11, 12:12]
time_steps5 = [1:2, 3:4, 5:12]
@test compute_rp_periods([time_steps1, time_steps4]) == [1:6, 7:9, 10:12]
@test compute_rp_periods([time_steps1, time_steps5]) == [1:4, 5:12]
@test compute_rp_periods([time_steps4, time_steps5]) == [1:6, 7:12]
end
end
# Add run all test files in test folder
include("test-io.jl")
include("test-time-resolution.jl")
include("test-case-studies.jl")

# Other general tests that don't need their own file
@testset "Ensuring benchmark loads" begin
include(joinpath(@__DIR__, "..", "benchmark", "benchmarks.jl"))
@test SUITE !== nothing
Expand Down
39 changes: 39 additions & 0 deletions test/test-case-studies.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@testset "Norse Case Study" begin
dir = joinpath(INPUT_FOLDER, "Norse")
parameters, sets = create_parameters_and_sets_from_file(dir)
graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))
model = create_model(graph, parameters, sets)
solution = solve_model(model)
@test solution.objective_value 183696778.98322 atol = 1e-5
save_solution_to_file(
OUTPUT_FOLDER,
sets.assets_investment,
solution.assets_investment,
parameters.assets_unit_capacity,
)
end

@testset "Tiny Case Study" begin
dir = joinpath(INPUT_FOLDER, "Tiny")
parameters, sets = create_parameters_and_sets_from_file(dir)
graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))
model = create_model(graph, parameters, sets; write_lp_file = true)
solution = solve_model(model)
@test solution.objective_value 269238.43825 atol = 1e-5
save_solution_to_file(
OUTPUT_FOLDER,
sets.assets_investment,
solution.assets_investment,
parameters.assets_unit_capacity,
)
end

@testset "Infeasible Case Study" begin
dir = joinpath(INPUT_FOLDER, "Tiny")
parameters, sets = create_parameters_and_sets_from_file(dir)
parameters.peak_demand["demand"] = -1 # make it infeasible
graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))
model = create_model(graph, parameters, sets)
solution = solve_model(model)
@test solution === nothing
end
22 changes: 22 additions & 0 deletions test/test-io.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@testset "Input validation" begin
@testset "Make sure that input validation fails for bad files" begin
dir = joinpath(INPUT_FOLDER, "Tiny")
@test_throws ArgumentError TulipaEnergyModel.read_csv_with_schema(
joinpath(dir, "bad-assets-data.csv"),
TulipaEnergyModel.AssetData,
)
end
end

@testset "Graph structure" begin
@testset "Graph structure is correct" begin
dir = joinpath(INPUT_FOLDER, "Tiny")
graph =
create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv"))

@test Graphs.nv(graph) == 6
@test Graphs.ne(graph) == 5
@test collect(Graphs.edges(graph)) ==
[Graphs.Edge(e) for e in [(1, 6), (2, 6), (3, 6), (4, 6), (5, 6)]]
end
end
46 changes: 46 additions & 0 deletions test/test-time-resolution.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@testset "Time resolution" begin
@testset "resolution_matrix" begin
rp_periods = [1:4, 5:8, 9:12]
time_steps = [1:4, 5:8, 9:12]
expected = [
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
]
@test resolution_matrix(rp_periods, time_steps) == expected

time_steps = [1:3, 4:6, 7:9, 10:12]
expected = [
1.0 1/3 0.0 0.0
0.0 2/3 2/3 0.0
0.0 0.0 1/3 1.0
]
@test resolution_matrix(rp_periods, time_steps) == expected

time_steps = [1:6, 7:9, 10:10, 11:11, 12:12]
expected = [
2/3 0.0 0.0 0.0 0.0
1/3 2/3 0.0 0.0 0.0
0.0 1/3 1.0 1.0 1.0
]
@test resolution_matrix(rp_periods, time_steps) == expected
end

@testset "compute_rp_periods" begin
# regular
time_steps1 = [1:4, 5:8, 9:12] # every 4 hours
time_steps2 = [1:3, 4:6, 7:9, 10:12] # every 3 hours
time_steps3 = [i:i for i 1:12] # hourly

@test compute_rp_periods([time_steps1, time_steps2]) == time_steps1
@test compute_rp_periods([time_steps1, time_steps2, time_steps3]) == time_steps1
@test compute_rp_periods([time_steps2, time_steps3]) == time_steps2

# Irregular
time_steps4 = [1:6, 7:9, 10:11, 12:12]
time_steps5 = [1:2, 3:4, 5:12]
@test compute_rp_periods([time_steps1, time_steps4]) == [1:6, 7:9, 10:12]
@test compute_rp_periods([time_steps1, time_steps5]) == [1:4, 5:12]
@test compute_rp_periods([time_steps4, time_steps5]) == [1:6, 7:12]
end
end

0 comments on commit c245e1c

Please sign in to comment.