-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SQL storage for an "enable error recovery" scalar.
- Loading branch information
1 parent
058854c
commit 4a2b8e0
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
robot-server/robot_server/runs/error_recovery_setting_store.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# noqa: D100 | ||
|
||
|
||
import sqlalchemy | ||
|
||
from robot_server.persistence.tables import enable_error_recovery_table | ||
|
||
|
||
class ErrorRecoverySettingStore: | ||
"""Persistently stores settings related to error recovery.""" | ||
|
||
def __init__(self, sql_engine: sqlalchemy.engine.Engine) -> None: | ||
self._sql_engine = sql_engine | ||
|
||
def get_is_enabled(self) -> bool | None: | ||
"""Get the value of the "error recovery enabled" setting. | ||
`None` is the default, i.e. it's never been explicitly set one way or the other. | ||
""" | ||
with self._sql_engine.begin() as transaction: | ||
return transaction.execute( | ||
sqlalchemy.select(enable_error_recovery_table.c.enable_error_recovery) | ||
).scalar_one_or_none() | ||
|
||
def set_is_enabled(self, is_enabled: bool) -> None: | ||
"""Set the value of the "error recovery enabled" setting.""" | ||
with self._sql_engine.begin() as transaction: | ||
transaction.execute(sqlalchemy.delete(enable_error_recovery_table)) | ||
transaction.execute( | ||
sqlalchemy.insert(enable_error_recovery_table).values( | ||
id=0, # id=0 to match the single-row constraint trick in the table declaration. | ||
enable_error_recovery=is_enabled, | ||
) | ||
) |
26 changes: 26 additions & 0 deletions
26
robot-server/tests/runs/test_error_recovery_setting_store.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Tests for error_recovery_setting_store.""" | ||
|
||
|
||
from robot_server.runs.error_recovery_setting_store import ErrorRecoverySettingStore | ||
|
||
import pytest | ||
import sqlalchemy | ||
|
||
|
||
@pytest.fixture | ||
def subject( | ||
sql_engine: sqlalchemy.engine.Engine, | ||
) -> ErrorRecoverySettingStore: | ||
"""Return a test subject.""" | ||
return ErrorRecoverySettingStore(sql_engine=sql_engine) | ||
|
||
|
||
def test_error_recovery_setting_store(subject: ErrorRecoverySettingStore) -> None: | ||
"""Test `ErrorRecoverySettingStore`.""" | ||
assert subject.get_is_enabled() is None | ||
|
||
subject.set_is_enabled(is_enabled=False) | ||
assert subject.get_is_enabled() is False | ||
|
||
subject.set_is_enabled(is_enabled=True) | ||
assert subject.get_is_enabled() is True |