Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deltamarnix committed Dec 18, 2023
1 parent 75f5430 commit abe6e6a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion core/src/Ribasim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ using Graphs:
rem_edge!

using Legolas: Legolas, @schema, @version, validate, SchemaVersion, declared
using Logging: with_logger, LogLevel, AbstractLogger
using Logging: with_logger, LogLevel, AbstractLogger, SimpleLogger
using MetaGraphsNext:
MetaGraphsNext,
MetaGraph,
Expand Down
8 changes: 7 additions & 1 deletion core/src/logging.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
The type of message that is sent to is_current_module.
The information is generated in LoggingExtras.EarlyFilteredLogger.
See https://github.com/JuliaLogging/LoggingExtras.jl/blob/d35e7c8cfc197853ee336ace17182e6ed36dca24/src/CompositionalLoggers/earlyfiltered.jl#L39
"""
const LogMessageType =
@NamedTuple{level::LogLevel, _module::Module, group::Symbol, id::Symbol}

Expand Down Expand Up @@ -30,7 +36,7 @@ function close(logger::AbstractLogger)
close(logger.logger)
elseif hasfield(typeof(logger), :loggers)
foreach(close, logger.loggers)
elseif hasfield(typeof(logger), :stream) && !isa(logger, TerminalLogger)
elseif hasfield(typeof(logger), :stream) && logger isa SimpleLogger # FileLogger contains a SimpleLogger that contains the Stream.
Base.close(logger.stream)
end
end
18 changes: 8 additions & 10 deletions core/test/logging_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
config = Ribasim.Config(normpath(dir, "ribasim.toml"))
mkdir(Ribasim.results_path(config, "."))
logger = Ribasim.setup_logger(config)
@test Logging.shouldlog(logger, Logging.Error, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Info, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, nothing, "message") # progress bar
@test !Logging.shouldlog(logger, Logging.Debug, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Error, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, :group, :message) # progress bar
@test !Logging.shouldlog(logger, Logging.Debug, Ribasim, :group, :message)

Ribasim.close(logger)
end
Expand All @@ -29,10 +29,10 @@ end
config = Ribasim.Config(normpath(dir, "ribasim.toml"))
mkdir(Ribasim.results_path(config, "."))
logger = Ribasim.setup_logger(config)
@test Logging.shouldlog(logger, Logging.Error, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Info, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, nothing, "message") # progress bar
@test Logging.shouldlog(logger, Logging.Debug, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Error, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, :group, :message) # progress bar
@test Logging.shouldlog(logger, Logging.Debug, Ribasim, :group, :message)

Ribasim.close(logger)
end
Expand Down Expand Up @@ -63,8 +63,6 @@ end

Ribasim.close(logger)

println(@__MODULE__)

open(normpath(dir, "results", "ribasim.log"), "r") do io
result = read(io, String)
@test occursin("Info: foo", result)
Expand Down
18 changes: 9 additions & 9 deletions core/test/run_models_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

toml_path = normpath(@__DIR__, "../../generated_testmodels/trivial/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test model isa Ribasim.Model
@test successful_retcode(model)
(; p) = model.integrator
Expand Down Expand Up @@ -91,7 +91,7 @@ end

toml_path = normpath(@__DIR__, "../../generated_testmodels/bucket/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test model isa Ribasim.Model
@test successful_retcode(model)
end
Expand All @@ -107,7 +107,7 @@ end

logger = TestLogger()
model = with_logger(logger) do
Ribasim.run(toml_path)
Ribasim.run(Ribasim.Config(toml_path))
end

@test model isa Ribasim.Model
Expand Down Expand Up @@ -148,7 +148,7 @@ end

toml_path = normpath(@__DIR__, "../../generated_testmodels/basic_arrow/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test model isa Ribasim.Model
@test successful_retcode(model)
end
Expand All @@ -159,7 +159,7 @@ end
toml_path =
normpath(@__DIR__, "../../generated_testmodels/basic_transient/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test model isa Ribasim.Model
@test successful_retcode(model)
@test length(model.integrator.p.basin.precipitation) == 4
Expand Down Expand Up @@ -203,7 +203,7 @@ end
toml_path =
normpath(@__DIR__, "../../generated_testmodels/tabulated_rating_curve/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test model isa Ribasim.Model
@test successful_retcode(model)
@test model.integrator.sol.u[end] Float32[7.783636, 726.16394] skip = Sys.isapple()
Expand Down Expand Up @@ -267,7 +267,7 @@ end
toml_path = normpath(@__DIR__, "../../generated_testmodels/outlet/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
(; level_boundary, outlet) = p
Expand Down Expand Up @@ -298,7 +298,7 @@ end

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

day = 86400.0
Expand Down Expand Up @@ -370,7 +370,7 @@ end

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

u = model.integrator.sol.u[end]
Expand Down
2 changes: 1 addition & 1 deletion core/test/time_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
toml_path =
normpath(@__DIR__, "../../generated_testmodels/flow_boundary_time/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
model = Ribasim.run(Ribasim.Config(toml_path))
@test successful_retcode(model)

flow = DataFrame(Ribasim.flow_table(model))
Expand Down

0 comments on commit abe6e6a

Please sign in to comment.