Skip to content
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

Add explicit 'using' to tests #995

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 14 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using CSV
using Cbc
using DataFrames
using DuckDB
using GLPK
using Graphs
using HiGHS
using JuMP
using MathOptInterface
using Test
using TOML
using TulipaEnergyModel
using TulipaIO
using CSV: CSV
using Cbc: Cbc
using DataFrames: DataFrames, DataFrame
using DuckDB: DuckDB, DBInterface
using GLPK: GLPK
using Graphs: Graphs
using HiGHS: HiGHS
using JuMP: JuMP
using MathOptInterface: MathOptInterface
using Test: Test, @test, @testset, @test_throws, @test_logs
using TOML: TOML
using TulipaEnergyModel: TulipaEnergyModel
using TulipaIO: TulipaIO

# Folders names
const INPUT_FOLDER = joinpath(@__DIR__, "inputs")
Expand Down Expand Up @@ -49,5 +49,5 @@ end
@testset "Ensuring data can be read and create the internal structures" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(@__DIR__, "../benchmark/EU/"))
create_internal_structures(connection)
TulipaEnergyModel.create_internal_structures(connection)
end
23 changes: 13 additions & 10 deletions test/test-case-studies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
for (optimizer, parameters) in parameters_dict
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(connection; optimizer, parameters)
energy_problem = TulipaEnergyModel.run_scenario(connection; optimizer, parameters)
@test JuMP.is_solved_and_feasible(energy_problem.model)
end
end
Expand All @@ -26,7 +26,7 @@ end
for optimizer in optimizer_list
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(connection; optimizer)
energy_problem = TulipaEnergyModel.run_scenario(connection; optimizer)
@test energy_problem.objective_value ≈ 269238.43825 rtol = 1e-8
end
end
Expand All @@ -35,7 +35,7 @@ end
dir = joinpath(INPUT_FOLDER, "Norse")
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(
energy_problem = TulipaEnergyModel.run_scenario(
connection;
output_folder = OUTPUT_FOLDER,
write_lp_file = true,
Expand All @@ -47,7 +47,7 @@ end
dir = joinpath(INPUT_FOLDER, "Storage")
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(connection)
energy_problem = TulipaEnergyModel.run_scenario(connection)
@test energy_problem.objective_value ≈ 2409.384029 atol = 1e-5
end

Expand All @@ -58,23 +58,24 @@ end
Dict("output_flag" => false, "mip_rel_gap" => 0.0, "mip_feasibility_tolerance" => 1e-5)
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(connection; optimizer = optimizer, parameters = parameters)
energy_problem =
TulipaEnergyModel.run_scenario(connection; optimizer = optimizer, parameters = parameters)
@test energy_problem.objective_value ≈ 293074.923309 atol = 1e-5
end

@testset "Tiny Variable Resolution Case Study" begin
dir = joinpath(INPUT_FOLDER, "Variable Resolution")
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(connection)
energy_problem = TulipaEnergyModel.run_scenario(connection)
@test energy_problem.objective_value ≈ 28.45872 atol = 1e-5
end

@testset "Multi-year Case Study" begin
dir = joinpath(INPUT_FOLDER, "Multi-year Investments")
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = run_scenario(
energy_problem = TulipaEnergyModel.run_scenario(
connection;
model_parameters_file = joinpath(@__DIR__, "inputs", "model-parameters-example.toml"),
)
Expand All @@ -94,9 +95,11 @@ end
AND milestone_year = 2030
",
)
energy_problem = EnergyProblem(connection)
create_model!(energy_problem)
@test_logs (:warn, "Model status different from optimal") solve_model!(energy_problem)
energy_problem = TulipaEnergyModel.EnergyProblem(connection)
TulipaEnergyModel.create_model!(energy_problem)
@test_logs (:warn, "Model status different from optimal") TulipaEnergyModel.solve_model!(
energy_problem,
)
@test energy_problem.termination_status == JuMP.INFEASIBLE
print(energy_problem)
end
6 changes: 3 additions & 3 deletions test/test-economic-parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(2022, "asset1") => 135.6714595,
)

result = calculate_annualized_cost(
result = TulipaEnergyModel.calculate_annualized_cost(
discount_rate,
economic_lifetime,
investment_cost,
Expand Down Expand Up @@ -53,7 +53,7 @@ end
(2022, "asset1") => 964.3285406,
)

result = calculate_salvage_value(
result = TulipaEnergyModel.calculate_salvage_value(
discount_rate,
economic_lifetime,
annualized_cost,
Expand Down Expand Up @@ -93,7 +93,7 @@ end
(2022, "asset1") => 0.0797796,
)

result = calculate_weight_for_investment_discounts(
result = TulipaEnergyModel.calculate_weight_for_investment_discounts(
social_rate,
discount_year,
salvage_value,
Expand Down
22 changes: 11 additions & 11 deletions test/test-io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
@testset "Check missing asset partition if strict" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Norse"))
@test_throws Exception EnergyProblem(connection, strict = true)
@test_throws Exception TulipaEnergyModel.EnergyProblem(connection, strict = true)
end
end

@testset "Output validation" begin
@testset "Make sure that saving an unsolved energy problem fails" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny"))
energy_problem = EnergyProblem(connection)
energy_problem = TulipaEnergyModel.EnergyProblem(connection)
output_dir = mktempdir()
@test_throws Exception save_solution_to_file(output_dir, energy_problem)
create_model!(energy_problem)
@test_throws Exception save_solution_to_file(output_dir, energy_problem)
solve_model!(energy_problem)
@test save_solution_to_file(output_dir, energy_problem) === nothing
@test_throws Exception TulipaEnergyModel.save_solution_to_file(output_dir, energy_problem)
TulipaEnergyModel.create_model!(energy_problem)
@test_throws Exception TulipaEnergyModel.save_solution_to_file(output_dir, energy_problem)
TulipaEnergyModel.solve_model!(energy_problem)
@test TulipaEnergyModel.save_solution_to_file(output_dir, energy_problem) === nothing
end
end

@testset "Printing EnergyProblem validation" begin
@testset "Check the missing cases of printing the EnergyProblem" begin # model infeasible is covered in testset "Infeasible Case Study".
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny"))
energy_problem = EnergyProblem(connection)
energy_problem = TulipaEnergyModel.EnergyProblem(connection)
print(energy_problem)
create_model!(energy_problem)
TulipaEnergyModel.create_model!(energy_problem)
print(energy_problem)
solve_model!(energy_problem)
TulipaEnergyModel.solve_model!(energy_problem)
print(energy_problem)
end
end
Expand All @@ -37,7 +37,7 @@ end
@testset "Graph structure is correct" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny"))
graph, _, _ = create_internal_structures(connection)
graph, _, _ = TulipaEnergyModel.create_internal_structures(connection)

@test Graphs.nv(graph) == 6
@test Graphs.ne(graph) == 5
Expand Down
18 changes: 9 additions & 9 deletions test/test-model-parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@
path = joinpath(@__DIR__, "inputs", "model-parameters-example.toml")

@testset "Basic usage" begin
mp = ModelParameters(; discount_rate = 0.1, discount_year = 2018)
mp = TulipaEnergyModel.ModelParameters(; discount_rate = 0.1, discount_year = 2018)
@test mp.discount_rate == 0.1
@test mp.discount_year == 2018
end

@testset "Errors when missing required parameters" begin
@test_throws UndefKeywordError ModelParameters()
@test_throws UndefKeywordError TulipaEnergyModel.ModelParameters()
end

@testset "Read from file" begin
mp = ModelParameters(path)
mp = TulipaEnergyModel.ModelParameters(path)
data = TOML.parsefile(path)
for (key, value) in data
@test value == getfield(mp, Symbol(key))
end

@testset "explicit keywords take precedence" begin
mp = ModelParameters(path; discount_year = 2019)
mp = TulipaEnergyModel.ModelParameters(path; discount_year = 2019)
@test mp.discount_year == 2019
end

@testset "Errors if path does not exist" begin
@test_throws ArgumentError ModelParameters("nonexistent.toml")
@test_throws ArgumentError TulipaEnergyModel.ModelParameters("nonexistent.toml")
end
end

@testset "Read from DuckDB" begin
connection = DBInterface.connect(DuckDB.DB)
read_csv_folder(connection, joinpath(@__DIR__, "inputs", "Norse"))
mp = ModelParameters(connection)
TulipaIO.read_csv_folder(connection, joinpath(@__DIR__, "inputs", "Norse"))
mp = TulipaEnergyModel.ModelParameters(connection)
@test mp.discount_year == 2030

@testset "path has precedence" begin
mp = ModelParameters(connection, path)
mp = TulipaEnergyModel.ModelParameters(connection, path)
data = TOML.parsefile(path)
for (key, value) in data
@test value == getfield(mp, Symbol(key))
end
end

@testset "explicit keywords take precedence" begin
mp = ModelParameters(connection, path; discount_year = 2019)
mp = TulipaEnergyModel.ModelParameters(connection, path; discount_year = 2019)
@test mp.discount_year == 2019
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/test-model.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@testset "Test that solve_model! throws if model is not created but works otherwise" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny"))
energy_problem = EnergyProblem(connection)
@test_throws Exception solve_model!(energy_problem)
energy_problem = TulipaEnergyModel.EnergyProblem(connection)
@test_throws Exception TulipaEnergyModel.solve_model!(energy_problem)
@test !energy_problem.solved
create_model!(energy_problem)
TulipaEnergyModel.create_model!(energy_problem)
@test !energy_problem.solved
solution = solve_model!(energy_problem)
solution = TulipaEnergyModel.solve_model!(energy_problem)
@test energy_problem.solved
end
35 changes: 18 additions & 17 deletions test/test-options.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "Test some HiGHS options" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny"))
energy_problem = run_scenario(
energy_problem = TulipaEnergyModel.run_scenario(
connection;
output_folder = OUTPUT_FOLDER,
optimizer = HiGHS.Optimizer,
Expand All @@ -19,19 +19,19 @@ end
@testset "Test default_parameters usage" begin
@testset "HiGHS" begin
expected = Dict{String,Any}("output_flag" => false)
@test default_parameters(Val(:HiGHS)) == expected
@test default_parameters(HiGHS.Optimizer) == expected
@test default_parameters(:HiGHS) == expected
@test default_parameters("HiGHS") == expected
@test TulipaEnergyModel.default_parameters(Val(:HiGHS)) == expected
@test TulipaEnergyModel.default_parameters(HiGHS.Optimizer) == expected
@test TulipaEnergyModel.default_parameters(:HiGHS) == expected
@test TulipaEnergyModel.default_parameters("HiGHS") == expected
end

@testset "Undefined values" begin
expected = Dict{String,Any}()
@test default_parameters(Val(:blah)) == expected
@test default_parameters(:blah) == expected
@test default_parameters("blah") == expected
@test TulipaEnergyModel.default_parameters(Val(:blah)) == expected
@test TulipaEnergyModel.default_parameters(:blah) == expected
@test TulipaEnergyModel.default_parameters("blah") == expected
struct DummySolver <: MathOptInterface.AbstractOptimizer end
@test default_parameters(Val(:DummySolver)) == expected
@test TulipaEnergyModel.default_parameters(Val(:DummySolver)) == expected
end

@testset "New definition" begin
Expand Down Expand Up @@ -59,7 +59,7 @@ end
)
close(io)

@test read_parameters_from_file(filepath) == Dict{String,Any}(
@test TulipaEnergyModel.read_parameters_from_file(filepath) == Dict{String,Any}(
"string" => "something",
"integer_number" => 5,
"small_number" => 1.0e-8,
Expand All @@ -68,16 +68,17 @@ end
"big_number" => 6.66e6,
)

@test_throws ArgumentError read_parameters_from_file("badfile")
@test_throws ArgumentError TulipaEnergyModel.read_parameters_from_file("badfile")
end

@testset "Test that bad options throw errors" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny"))
@test_throws MathOptInterface.UnsupportedAttribute energy_problem = run_scenario(
connection;
output_folder = OUTPUT_FOLDER,
optimizer = HiGHS.Optimizer,
parameters = Dict("bad_param" => 1.0),
)
@test_throws MathOptInterface.UnsupportedAttribute energy_problem =
TulipaEnergyModel.run_scenario(
connection;
output_folder = OUTPUT_FOLDER,
optimizer = HiGHS.Optimizer,
parameters = Dict("bad_param" => 1.0),
)
end
27 changes: 14 additions & 13 deletions test/test-pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
dir = joinpath(INPUT_FOLDER, "Tiny")
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
energy_problem = EnergyProblem(connection)
create_model!(energy_problem)
solve_model!(energy_problem, HiGHS.Optimizer)
save_solution_to_file(mktempdir(), energy_problem)
energy_problem = TulipaEnergyModel.EnergyProblem(connection)
TulipaEnergyModel.create_model!(energy_problem)
TulipaEnergyModel.solve_model!(energy_problem, HiGHS.Optimizer)
TulipaEnergyModel.save_solution_to_file(mktempdir(), energy_problem)
end

@testset "Test that everything works for $input from beginning to end without EnergyProblem struct" for input in
Expand All @@ -21,15 +21,16 @@ end
_read_csv_folder(connection, dir)

# Internal data and structures pre-model
graph, representative_periods, timeframe, years = create_internal_structures(connection)
model_parameters = ModelParameters(connection)
sets = create_sets(graph, years)
variables = compute_variables_indices(connection)
constraints = compute_constraints_indices(connection)
profiles = prepare_profiles_structure(connection)
graph, representative_periods, timeframe, years =
TulipaEnergyModel.create_internal_structures(connection)
model_parameters = TulipaEnergyModel.ModelParameters(connection)
sets = TulipaEnergyModel.create_sets(graph, years)
variables = TulipaEnergyModel.compute_variables_indices(connection)
constraints = TulipaEnergyModel.compute_constraints_indices(connection)
profiles = TulipaEnergyModel.prepare_profiles_structure(connection)

# Create model
model = create_model(
model = TulipaEnergyModel.create_model(
connection,
graph,
sets,
Expand All @@ -43,6 +44,6 @@ end
)

# Solve model
solution = solve_model(model, variables, HiGHS.Optimizer)
save_solution_to_file(mktempdir(), graph, solution)
solution = TulipaEnergyModel.solve_model(model, variables, HiGHS.Optimizer)
TulipaEnergyModel.save_solution_to_file(mktempdir(), graph, solution)
end
Loading