Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add roundtrip test for model files #1007

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/ribasim/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ def discrete_control_of_pid_control() -> ribasim.Model:
@pytest.fixture()
def level_setpoint_with_minmax() -> ribasim.Model:
return ribasim_testmodels.level_setpoint_with_minmax_model()


@pytest.fixture()
def trivial() -> ribasim.Model:
return ribasim_testmodels.trivial_model()
32 changes: 32 additions & 0 deletions python/ribasim/tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sqlite3

import pandas as pd
import pytest
import ribasim
Expand Down Expand Up @@ -129,3 +131,33 @@ def test_sort(level_setpoint_with_minmax, tmp_path):
table_loaded = model_loaded.discrete_control.condition
assert table_loaded.df.iloc[0]["greater_than"] == 5.0
__assert_equal(table.df, table_loaded.df)


def test_roundtrip(trivial, tmp_path):
model1 = trivial
model1dir = tmp_path / "model1"
model2dir = tmp_path / "model2"
# read a model and then write it to a different path
model1.write(model1dir / "ribasim.toml")
model2 = ribasim.Model(filepath=model1dir / "ribasim.toml")
model2.write(model2dir / "ribasim.toml")

assert (model1dir / "database.gpkg").is_file()
assert (model2dir / "database.gpkg").is_file()

# gpkg_contents contains a last_change column that causes a binary diff
# remove that table so we can check if the rest is the same
for modeldir in [model1dir, model2dir]:
conn = sqlite3.connect(modeldir / "database.gpkg")
cursor = conn.cursor()
cursor.execute("DROP TABLE gpkg_contents")
cursor.execute("VACUUM")
conn.commit()
conn.close()

assert (model1dir / "ribasim.toml").read_text() == (
model2dir / "ribasim.toml"
).read_text()
assert (model1dir / "database.gpkg").read_bytes() == (
model2dir / "database.gpkg"
).read_bytes()
Loading