From abe6e6a8b234972ab776cfec820eb0c1c0fbaa60 Mon Sep 17 00:00:00 2001 From: Marnix Kraus Date: Mon, 18 Dec 2023 18:05:29 +0100 Subject: [PATCH] Fix unit tests --- core/src/Ribasim.jl | 2 +- core/src/logging.jl | 8 +++++++- core/test/logging_test.jl | 18 ++++++++---------- core/test/run_models_test.jl | 18 +++++++++--------- core/test/time_test.jl | 2 +- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/core/src/Ribasim.jl b/core/src/Ribasim.jl index 79947b8a6..eb701d3e4 100644 --- a/core/src/Ribasim.jl +++ b/core/src/Ribasim.jl @@ -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, diff --git a/core/src/logging.jl b/core/src/logging.jl index 77629280b..010b4d436 100644 --- a/core/src/logging.jl +++ b/core/src/logging.jl @@ -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} @@ -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 diff --git a/core/test/logging_test.jl b/core/test/logging_test.jl index 375eebe37..a9e9edf49 100644 --- a/core/test/logging_test.jl +++ b/core/test/logging_test.jl @@ -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 @@ -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 @@ -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) diff --git a/core/test/run_models_test.jl b/core/test/run_models_test.jl index 46f82ddbe..a9d5a5766 100644 --- a/core/test/run_models_test.jl +++ b/core/test/run_models_test.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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() @@ -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 @@ -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 @@ -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] diff --git a/core/test/time_test.jl b/core/test/time_test.jl index 02f73af5f..494139305 100644 --- a/core/test/time_test.jl +++ b/core/test/time_test.jl @@ -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))