Skip to content

Commit

Permalink
Test Timeseries.transact(…, discard_on_error=True)
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Aug 14, 2023
1 parent 977337d commit 4bac827
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions ixmp/tests/core/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging
import re
from datetime import datetime, timedelta

import pandas as pd
Expand Down Expand Up @@ -313,6 +315,34 @@ def test_remove(self, mp, ts, commit):
# Result is empty
assert ts.timeseries().empty

def test_transact_discard(self, caplog, mp, ts):
caplog.set_level(logging.INFO, "ixmp.utils")

df = expected(DATA[2050], ts)

ts.add_timeseries(DATA[2050])
ts.commit("")

# Catch the deliberately-raised exception so that the test passes
with pytest.raises(AttributeError):
with ts.transact(discard_on_error=True):
# Remove a single data point; this operation will not be committed
ts.remove_timeseries(df[df.year == 2010])

# Trigger AttributeError
ts.foo_bar()

# Reopen the database connection
mp.open_db()

# Exception was caught and logged
assert caplog.messages[-4].startswith("Avoid locking ")
assert re.match("Discard (timeseries|scenario) changes", caplog.messages[-3])
assert "Close database connection" == caplog.messages[-2]

# Data are unchanged
assert_frame_equal(expected(DATA[2050], ts), ts.timeseries())

# Geodata

def test_add_geodata(self, ts):
Expand Down
2 changes: 1 addition & 1 deletion ixmp/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def discard_on_error(ts: "TimeSeries"):
)
try:
ts.discard_changes()
log.info("Discard scenario changes")
log.info(f"Discard {ts.__class__.__name__.lower()} changes")
except Exception:
pass
mp.close_db()
Expand Down

0 comments on commit 4bac827

Please sign in to comment.