Skip to content

Commit

Permalink
Replace SpineDBAPIError by NothingToCommit
Browse files Browse the repository at this point in the history
There should be a way to ignore the specific condition of
nothing-to-commit when calling commit_session()
  • Loading branch information
soininen committed Oct 9, 2024
1 parent 2bbfd92 commit 7ce0427
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- ``commit_session()`` now raises ``NothingToCommit`` when there is nothing to commit.
Previously, it would raise ``SpineDBAPIException``.

### Deprecated

### Removed
Expand Down
4 changes: 2 additions & 2 deletions spinedb_api/db_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .db_mapping_base import DatabaseMappingBase, Status
from .db_mapping_commit_mixin import DatabaseMappingCommitMixin
from .db_mapping_query_mixin import DatabaseMappingQueryMixin
from .exception import SpineDBAPIError, SpineDBVersionError, SpineIntegrityError
from .exception import NothingToCommit, SpineDBAPIError, SpineDBVersionError, SpineIntegrityError
from .filters.tools import apply_filter_stack, load_filters, pop_filter_configs
from .helpers import (
Asterisk,
Expand Down Expand Up @@ -848,7 +848,7 @@ def commit_session(self, comment, apply_compatibility_transforms=True):
dirty_items = self._dirty_items()
if not dirty_items:
connection.execute(commit.delete().where(commit.c.id == commit_id))
raise SpineDBAPIError("Nothing to commit.")
raise NothingToCommit()
for tablename, (to_add, to_update, to_remove) in dirty_items:
for item in to_add + to_update + to_remove:
item.commit(commit_id)
Expand Down
7 changes: 7 additions & 0 deletions spinedb_api/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ def __init__(self, msg, rank=None, key=None):

class ConnectorError(SpineDBAPIError):
"""Failure in import/export connector."""


class NothingToCommit(SpineDBAPIError):
"""A notification that the session contains no changes."""

def __init__(self):
super().__init__("Nothing to commit.")
3 changes: 2 additions & 1 deletion tests/test_DatabaseMapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
to_database,
)
from spinedb_api.db_mapping_base import PublicItem, Status
from spinedb_api.exception import NothingToCommit
from spinedb_api.filters.scenario_filter import scenario_filter_config
from spinedb_api.helpers import Asterisk, DisplayStatus, name_from_elements
from spinedb_api.parameter_value import Duration, type_for_scalar
Expand Down Expand Up @@ -4231,7 +4232,7 @@ def test_commit_session_raise_with_empty_comment(self):
self.assertRaisesRegex(SpineDBAPIError, "Commit message cannot be empty.", self._db_map.commit_session, "")

def test_commit_session_raise_when_nothing_to_commit(self):
self.assertRaisesRegex(SpineDBAPIError, "Nothing to commit.", self._db_map.commit_session, "No changes.")
self.assertRaisesRegex(NothingToCommit, "Nothing to commit.", self._db_map.commit_session, "No changes.")

def test_rollback_addition(self):
import_functions.import_object_classes(self._db_map, ("my_class",))
Expand Down

0 comments on commit 7ce0427

Please sign in to comment.