From f895fa1be3a26fe43de92e199dcc59d53e80d0bf Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Tue, 8 Oct 2024 21:31:35 +0200 Subject: [PATCH] Remove database from struct Toml (#1882) It is not configurable, so it should not be in the struct. --- core/src/config.jl | 15 +++++++++++++-- core/src/model.jl | 2 +- core/test/allocation_test.jl | 6 +++--- core/test/io_test.jl | 13 +++++-------- core/test/utils_test.jl | 4 ++-- core/test/validation_test.jl | 8 ++++---- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/core/src/config.jl b/core/src/config.jl index 62fad53fe..bfbea84ef 100644 --- a/core/src/config.jl +++ b/core/src/config.jl @@ -23,7 +23,14 @@ using OrdinaryDiffEqRosenbrock: Rosenbrock23, Rodas4P, Rodas5P export Config, Solver, Results, Logging, Toml export algorithm, - camel_case, snake_case, input_path, results_path, convert_saveat, convert_dt, nodetypes + camel_case, + snake_case, + input_path, + database_path, + results_path, + convert_saveat, + convert_dt, + nodetypes const schemas = getfield.( @@ -133,7 +140,6 @@ end ribasim_version::String input_dir::String results_dir::String - database::String = "database.gpkg" allocation::Allocation = Allocation() solver::Solver = Solver() logging::Logging = Logging() @@ -168,6 +174,11 @@ function input_path(config::Config, path::String) return normpath(dirname(config), config.input_dir, path) end +"Construct the database path relative to both the TOML directory and the optional `input_dir`" +function database_path(config::Config) + return normpath(dirname(config), config.input_dir, "database.gpkg") +end + "Construct a path relative to both the TOML directory and the optional `results_dir`" function results_path(config::Config, path::String) return normpath(dirname(config), config.results_dir, path) diff --git a/core/src/model.jl b/core/src/model.jl index 921b3b7df..148f0bc5d 100644 --- a/core/src/model.jl +++ b/core/src/model.jl @@ -37,7 +37,7 @@ function Model(config_path::AbstractString)::Model end function Model(config::Config)::Model - db_path = input_path(config, config.database) + db_path = database_path(config) if !isfile(db_path) @error "Database file not found" db_path error("Database file not found") diff --git a/core/test/allocation_test.jl b/core/test/allocation_test.jl index f9524851f..f7549f1d1 100644 --- a/core/test/allocation_test.jl +++ b/core/test/allocation_test.jl @@ -7,7 +7,7 @@ toml_path = normpath(@__DIR__, "../../generated_testmodels/subnetwork/ribasim.toml") @test ispath(toml_path) cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) p = Ribasim.Parameters(db, cfg) @@ -77,7 +77,7 @@ end ) @test ispath(toml_path) cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) p = Ribasim.Parameters(db, cfg) close(db) @@ -207,7 +207,7 @@ end ) @test ispath(toml_path) cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) p = Ribasim.Parameters(db, cfg) close(db) diff --git a/core/test/io_test.jl b/core/test/io_test.jl index 45d552a94..da482360f 100644 --- a/core/test/io_test.jl +++ b/core/test/io_test.jl @@ -5,35 +5,32 @@ toml = Ribasim.Toml(; starttime = now(), endtime = now(), - database = "path/to/file", input_dir = ".", results_dir = "results", crs = "EPSG:28992", ribasim_version = string(Ribasim.pkgversion(Ribasim)), ) config = Ribasim.Config(toml, "model") - @test Ribasim.input_path(config, "path/to/file") == - normpath("model", "path", "to", "file") + @test Ribasim.database_path(config) == normpath("model/database.gpkg") + @test Ribasim.input_path(config, "path/to/file") == normpath("model/path/to/file") # also relative to inputdir toml = Ribasim.Toml(; starttime = now(), endtime = now(), - database = "path/to/file", input_dir = "input", results_dir = "results", crs = "EPSG:28992", ribasim_version = string(Ribasim.pkgversion(Ribasim)), ) config = Ribasim.Config(toml, "model") - @test Ribasim.input_path(config, "path/to/file") == - normpath("model", "input", "path", "to", "file") + @test Ribasim.database_path(config) == normpath("model/input/database.gpkg") + @test Ribasim.input_path(config, "path/to/file") == normpath("model/input/path/to/file") # absolute path toml = Ribasim.Toml(; starttime = now(), endtime = now(), - database = "/path/to/file", input_dir = ".", results_dir = "results", crs = "EPSG:28992", @@ -92,7 +89,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/basic_transient/ribasim.toml") config = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(config, config.database) + db_path = Ribasim.database_path(config) db = SQLite.DB(db_path) # load a sorted table diff --git a/core/test/utils_test.jl b/core/test/utils_test.jl index 8d5b54198..ef0b85ae8 100644 --- a/core/test/utils_test.jl +++ b/core/test/utils_test.jl @@ -166,7 +166,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/basic/ribasim.toml") cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) p = Ribasim.Parameters(db, cfg) @@ -188,7 +188,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/pid_control/ribasim.toml") cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) p = Ribasim.Parameters(db, cfg) diff --git a/core/test/validation_test.jl b/core/test/validation_test.jl index 96c409a7b..547a11ff4 100644 --- a/core/test/validation_test.jl +++ b/core/test/validation_test.jl @@ -41,7 +41,7 @@ end @test ispath(toml_path) config = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(config, config.database) + db_path = Ribasim.database_path(config) db = SQLite.DB(db_path) graph = Ribasim.create_graph(db, config) @@ -194,7 +194,7 @@ end @test ispath(toml_path) cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) p = Ribasim.Parameters(db, cfg) close(db) @@ -274,7 +274,7 @@ end @test ispath(toml_path) cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) logger = TestLogger() with_logger(logger) do @@ -482,7 +482,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/basic/ribasim.toml") cfg = Ribasim.Config(toml_path) - db_path = Ribasim.input_path(cfg, cfg.database) + db_path = Ribasim.database_path(cfg) db = SQLite.DB(db_path) logger = TestLogger()