-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(robot-server): Implement storage for "enable/disable error recov…
…ery" setting (#16333)
- Loading branch information
1 parent
f4056d0
commit 021d28e
Showing
9 changed files
with
129 additions
and
17 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
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
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
# noqa: D100 | ||
|
||
|
||
import sqlalchemy | ||
|
||
from robot_server.persistence.tables import boolean_setting_table, BooleanSettingKey | ||
|
||
|
||
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_disabled(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(boolean_setting_table.c.value).where( | ||
boolean_setting_table.c.key | ||
== BooleanSettingKey.DISABLE_ERROR_RECOVERY | ||
) | ||
).scalar_one_or_none() | ||
|
||
def set_is_disabled(self, is_disabled: bool | None) -> None: | ||
"""Set the value of the "error recovery enabled" setting. | ||
`None` means revert to the default. | ||
""" | ||
with self._sql_engine.begin() as transaction: | ||
transaction.execute( | ||
sqlalchemy.delete(boolean_setting_table).where( | ||
boolean_setting_table.c.key | ||
== BooleanSettingKey.DISABLE_ERROR_RECOVERY | ||
) | ||
) | ||
if is_disabled is not None: | ||
transaction.execute( | ||
sqlalchemy.insert(boolean_setting_table).values( | ||
key=BooleanSettingKey.DISABLE_ERROR_RECOVERY, | ||
value=is_disabled, | ||
) | ||
) |
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
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
"""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_disabled() is None | ||
|
||
subject.set_is_disabled(is_disabled=False) | ||
assert subject.get_is_disabled() is False | ||
|
||
subject.set_is_disabled(is_disabled=True) | ||
assert subject.get_is_disabled() is True | ||
|
||
subject.set_is_disabled(is_disabled=None) | ||
assert subject.get_is_disabled() is None |