Skip to content

Commit

Permalink
Use closing everywhere. Add commit explicitely.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Sep 23, 2024
1 parent d863fdb commit e84414b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/ribasim/ribasim/input_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def _write_arrow(self, filepath: Path, directory: Path, input_dir: Path) -> None

@classmethod
def _from_db(cls, path: Path, table: str) -> pd.DataFrame | None:
with connect(path) as connection:
with closing(connect(path)) as connection:
if exists(connection, table):
query = f"select * from {esc_id(table)}"
df = pd.read_sql_query(
Expand Down Expand Up @@ -371,7 +371,7 @@ def sort(self):

@classmethod
def _from_db(cls, path: Path, table: str):
with connect(path) as connection:
with closing(connect(path)) as connection:
if exists(connection, table):
# pyogrio hardcodes fid name on reading
df = gpd.read_file(path, layer=table, fid_as_index=True)
Expand Down
1 change: 1 addition & 0 deletions python/ribasim/ribasim/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,6 @@ def _add_styles_to_geopackage(gpkg_path: Path, layer: str):
"update_date_time": update_date_time,
},
)
connection.commit()
else:
logging.warning(f"Style not found for layer: {layer}")
9 changes: 9 additions & 0 deletions python/ribasim/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,12 @@ def test_minimal_toml():
(toml_path.parent / "database.gpkg").touch() # database file must exist for `read`
model = ribasim.Model.read(toml_path)
assert model.crs == "EPSG:28992"


def test_closed_model(basic, tmp_path):
# Test whether we can write to a just opened model
# implicitly testing that the database is closed after read
toml_path = tmp_path / "basic/ribasim.toml"
basic.write(toml_path)
model = ribasim.Model.read(toml_path)
model.write(toml_path)

0 comments on commit e84414b

Please sign in to comment.