Skip to content

Commit

Permalink
Fix even more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deltamarnix committed Dec 19, 2023
1 parent c0ccab1 commit 7849788
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 64 deletions.
30 changes: 15 additions & 15 deletions core/src/main.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function help(x)::Cint
function help(x::AbstractString)::Cint
println(x)
println("Usage: ribasim path/to/model/ribasim.toml")
return 1
end

function main(ARGS)::Cint
function main(ARGS::Vector{AbstractString})::Cint
n = length(ARGS)
if n != 1
return help("Exactly 1 argument expected, got $n")
Expand All @@ -25,21 +25,21 @@ function main(ARGS)::Cint
# show progress bar in terminal
config = Config(arg)
open(results_path(config, "ribasim.log"), "w") do io
logger = Ribasim.setup_logger(; verbosity = config.verbosity, stream = io)
model = with_logger(logger) do
Ribasim.run(config)
logger =
Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io)
with_logger(logger) do
model = Ribasim.run(config)
if successful_retcode(model)
@info "The model finished successfully"
return 0
end

t = Ribasim.datetime_since(model.integrator.t, model.config.starttime)
retcode = model.integrator.sol.retcode
@error "The model exited at model time $t with return code $retcode.\nSee https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes"
return 1
end
end
return if successful_retcode(model)
println("The model finished successfully")
0
else
t = Ribasim.datetime_since(model.integrator.t, model.config.starttime)
retcode = model.integrator.sol.retcode
println("The model exited at model time $t with return code $retcode")
println("See https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes")
1
end
catch
Base.invokelatest(Base.display_error, current_exceptions())
return 1
Expand Down
38 changes: 0 additions & 38 deletions core/test/cli_test.jl

This file was deleted.

12 changes: 6 additions & 6 deletions core/test/control_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
toml_path =
normpath(@__DIR__, "../../generated_testmodels/pump_discrete_control/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
p = model.integrator.p
(; discrete_control, graph) = p

Expand Down Expand Up @@ -49,7 +49,7 @@ end
@testitem "Flow condition control" begin
toml_path = normpath(@__DIR__, "../../generated_testmodels/flow_condition/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
p = model.integrator.p
(; discrete_control, flow_boundary) = p

Expand All @@ -73,7 +73,7 @@ end
"../../generated_testmodels/level_boundary_condition/ribasim.toml",
)
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
p = model.integrator.p
(; discrete_control, level_boundary) = p

Expand Down Expand Up @@ -138,7 +138,7 @@ end
"../../generated_testmodels/tabulated_rating_curve_control/ribasim.toml",
)
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
p = model.integrator.p
(; discrete_control) = p
# it takes some months to fill the Basin above 0.5 m
Expand All @@ -157,7 +157,7 @@ end
"../../generated_testmodels/level_setpoint_with_minmax/ribasim.toml",
)
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
p = model.integrator.p
(; discrete_control) = p
(; record, greater_than) = discrete_control
Expand Down Expand Up @@ -187,7 +187,7 @@ end
"../../generated_testmodels/discrete_control_of_pid_control/ribasim.toml",
)
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
p = model.integrator.p
(; discrete_control, pid_control) = p

Expand Down
10 changes: 5 additions & 5 deletions core/test/equations_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
toml_path =
normpath(@__DIR__, "../../generated_testmodels/linear_resistance/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test successful_retcode(model)
p = model.integrator.p

Expand All @@ -47,7 +47,7 @@ end

toml_path = normpath(@__DIR__, "../../generated_testmodels/rating_curve/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test successful_retcode(model)
p = model.integrator.p

Expand Down Expand Up @@ -82,7 +82,7 @@ end
toml_path =
normpath(@__DIR__, "../../generated_testmodels/manning_resistance/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test successful_retcode(model)
p = model.integrator.p
(; manning_resistance) = p
Expand Down Expand Up @@ -119,7 +119,7 @@ end
toml_path =
normpath(@__DIR__, "../../generated_testmodels/pid_control_equation/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test successful_retcode(model)
p = model.integrator.p
(; basin, pid_control) = p
Expand Down Expand Up @@ -165,7 +165,7 @@ end

toml_path = normpath(@__DIR__, "../../generated_testmodels/misc_nodes/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test successful_retcode(model)
p = model.integrator.p
(; flow_boundary, fractional_flow, pump) = p
Expand Down
28 changes: 28 additions & 0 deletions core/test/main_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@testitem "version" begin
using IOCapture: capture
using Logging: global_logger, ConsoleLogger

(; value, output) = capture() do
Ribasim.main(["--version"])
end
@test value == 0
@test output == string(pkgversion(Ribasim))
end

@testitem "toml_path" begin
using IOCapture: capture
using Logging: global_logger, ConsoleLogger

model_path = normpath(@__DIR__, "../../generated_testmodels/basic/")
toml_path = normpath(model_path, "ribasim.toml")
@test ispath(toml_path)
(; value, output, error, backtrace) = capture() do
Ribasim.main([toml_path])
end
@test value == 0
if value != 0
@show output
@show error
@show backtrace
end
end

0 comments on commit 7849788

Please sign in to comment.