Skip to content

Commit

Permalink
Remove database from struct Toml (#1882)
Browse files Browse the repository at this point in the history
It is not configurable, so it should not be in the struct.
  • Loading branch information
visr authored Oct 8, 2024
1 parent e09eeb5 commit f895fa1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
15 changes: 13 additions & 2 deletions core/src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions core/test/io_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions core/test/validation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f895fa1

Please sign in to comment.