Skip to content

Commit

Permalink
Write ribasim_version to TOML and warn in core if it is different
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Feb 26, 2024
1 parent 7ee3f1a commit d7fc006
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ end
@option @addnodetypes struct Toml <: TableOption
starttime::DateTime
endtime::DateTime
ribasim_version::String
input_dir::String
results_dir::String
database::String = "database.gpkg"
Expand Down
5 changes: 4 additions & 1 deletion core/src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ function main(ARGS::Vector{String})::Cint
logger =
Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io)
with_logger(logger) do
ribasim_version = pkgversion(Ribasim)
ribasim_version = string(pkgversion(Ribasim))
(; starttime, endtime) = config
if string(ribasim_version) != config.ribasim_version
@warn "The Ribasim version in the TOML config file does not match the used Ribasim CLI version." config.ribasim_version ribasim_version
end
@info "Starting a Ribasim simulation." ribasim_version starttime endtime
model = Ribasim.run(config)
if successful_retcode(model)
Expand Down
5 changes: 1 addition & 4 deletions core/test/data/config_test.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
starttime = 2019-01-01
endtime = 2019-12-31

# optional, default is the path of the TOML
input_dir = "../../generated_testmodels/lhm"
results_dir = "../../generated_testmodels/lhm"

database = "database.gpkg"
ribasim_version = "2024.2.0"

[basin]
time = "basin/time.arrow"
Expand Down
1 change: 1 addition & 0 deletions core/test/data/logging_test_loglevel_debug.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ starttime = 2019-01-01
endtime = 2019-12-31
input_dir = "."
results_dir = "results"
ribasim_version = "2024.2.0"

[logging]
verbosity = "debug"
1 change: 1 addition & 0 deletions core/test/data/logging_test_no_loglevel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ starttime = 2019-01-01
endtime = 2019-12-31
input_dir = "."
results_dir = "results"
ribasim_version = "2024.2.0"
2 changes: 2 additions & 0 deletions core/test/docs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ endtime = 2021-01-01 # required
input_dir = "." # required
results_dir = "results" # required

ribasim_version = "2024.2.0" # required

# Specific tables can also go into Arrow files rather than the database.
# For large tables this can benefit from better compressed file sizes.
# This is optional, tables are retrieved from the database if not specified in the TOML.
Expand Down
3 changes: 3 additions & 0 deletions core/test/io_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
database = "path/to/file",
input_dir = ".",
results_dir = "results",
ribasim_version = string(Ribasim.pkgversion(Ribasim)),
)
config = Ribasim.Config(toml, "model")
@test Ribasim.input_path(config, "path/to/file") ==
Expand All @@ -20,6 +21,7 @@
database = "path/to/file",
input_dir = "input",
results_dir = "results",
ribasim_version = string(Ribasim.pkgversion(Ribasim)),
)
config = Ribasim.Config(toml, "model")
@test Ribasim.input_path(config, "path/to/file") ==
Expand All @@ -32,6 +34,7 @@
database = "/path/to/file",
input_dir = ".",
results_dir = "results",
ribasim_version = string(Ribasim.pkgversion(Ribasim)),
)
config = Ribasim.Config(toml)
@test Ribasim.input_path(config, "/path/to/file") == abspath("/path/to/file")
Expand Down
10 changes: 10 additions & 0 deletions core/test/main_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ end

@testitem "toml_path" begin
using IOCapture: capture
import TOML

model_path = normpath(@__DIR__, "../../generated_testmodels/basic/")
toml_path = normpath(model_path, "ribasim.toml")

# change the ribasim_version in the toml file to check warning
toml_dict = TOML.parsefile(toml_path)
toml_dict["ribasim_version"] = "a_different_version"
open(toml_path, "w") do io
TOML.print(io, toml_dict)
end

@test ispath(toml_path)
(; value, output, error, backtrace) = capture() do
Ribasim.main([toml_path])
Expand All @@ -23,6 +32,7 @@ end
@show error
@show backtrace
end
@test occursin("version in the TOML config file does not match", output)
end

@testitem "too many arguments for main" begin
Expand Down
9 changes: 2 additions & 7 deletions docs/contribute/release.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ Determine the new version number like `2023.1.0`, filling in the current year, a
This follows `YYYY.MINOR.MICRO` from [calver](https://calver.org/).

Update the version numbers in the repository to the new version number.
A single find and replace can update all 5 locations:

- `pixi.toml`
- `core/Project.toml`
- `python/ribasim/ribasim/__init__.py`
- `python/ribasim_api/ribasim_api/__init__.py`
- `ribasim_qgis/metadata.txt`
A single find and replace can update all locations.
The Ribasim version number is not in `pixi.lock`, that file does not need updating.

Now submit a pull request to update the version numbers.

Expand Down
2 changes: 2 additions & 0 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
model_validator,
)

import ribasim
from ribasim.config import (
Allocation,
Basin,
Expand Down Expand Up @@ -229,6 +230,7 @@ def _write_toml(self, fn: FilePath):
content = self.model_dump(exclude_unset=True, exclude_none=True, by_alias=True)
# Filter empty dicts (default Nodes)
content = dict(filter(lambda x: x[1], content.items()))
content["ribasim_version"] = ribasim.__version__
with open(fn, "wb") as f:
tomli_w.dump(content, f)
return fn
Expand Down
11 changes: 9 additions & 2 deletions python/ribasim/tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pandas as pd
import pytest
import ribasim
import tomli
from numpy.testing import assert_array_equal
from pandas import DataFrame
from pandas.testing import assert_frame_equal
Expand Down Expand Up @@ -36,8 +37,14 @@ def __assert_equal(a: DataFrame, b: DataFrame, is_network=False) -> None:

def test_basic(basic, tmp_path):
model_orig = basic
model_orig.write(tmp_path / "basic/ribasim.toml")
model_loaded = ribasim.Model(filepath=tmp_path / "basic/ribasim.toml")
toml_path = tmp_path / "basic/ribasim.toml"
model_orig.write(toml_path)
model_loaded = ribasim.Model(filepath=toml_path)

with open(toml_path, "rb") as f:
toml_dict = tomli.load(f)

assert toml_dict["ribasim_version"] == ribasim.__version__

index_a = model_orig.network.node.df.index.to_numpy(int)
index_b = model_loaded.network.node.df.index.to_numpy(int)
Expand Down

0 comments on commit d7fc006

Please sign in to comment.