From c77114f66ba61eb556ecf2e6a030c850b9c58339 Mon Sep 17 00:00:00 2001 From: Roman Right Date: Sun, 3 Dec 2023 15:23:18 -0600 Subject: [PATCH] Exclude revision_id from the get_changes method (#759) * Exclude revision_id from get_changes * line length fix * test * GH action python versions as strings --- .github/workflows/github-actions-tests.yml | 2 +- beanie/odm/documents.py | 8 +++++- tests/odm/test_state_management.py | 32 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.github/workflows/github-actions-tests.yml b/.github/workflows/github-actions-tests.yml index dda95fec..8f90ba49 100644 --- a/.github/workflows/github-actions-tests.yml +++ b/.github/workflows/github-actions-tests.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: [ 3.7, 3.8, 3.9, 3.10.6, 3.11 ] + python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11" ] mongodb-version: [ 4.4, 5.0 ] pydantic-version: [ 1.10.12, 2.3 ] runs-on: ubuntu-latest diff --git a/beanie/odm/documents.py b/beanie/odm/documents.py index 0151e6f4..c74f167c 100644 --- a/beanie/odm/documents.py +++ b/beanie/odm/documents.py @@ -988,7 +988,13 @@ def _collect_updates( @saved_state_needed def get_changes(self) -> Dict[str, Any]: return self._collect_updates( - self._saved_state, get_dict(self, to_db=True, keep_nulls=self.get_settings().keep_nulls) # type: ignore + self._saved_state, # type: ignore + get_dict( + self, + to_db=True, + keep_nulls=self.get_settings().keep_nulls, + exclude={"revision_id", "_previous_revision_id"}, + ), ) @saved_state_needed diff --git a/tests/odm/test_state_management.py b/tests/odm/test_state_management.py index cc4e473d..0cce2114 100644 --- a/tests/odm/test_state_management.py +++ b/tests/odm/test_state_management.py @@ -430,3 +430,35 @@ async def test_replace_save_previous(self, saved_doc_previous): assert saved_doc_previous.get_saved_state()["num_1"] == 100 assert saved_doc_previous.get_previous_saved_state()["num_1"] == 1 + + async def test_exclude_revision_id_and_previous_revision_id( + self, saved_doc_previous + ): + saved_doc_previous.num_1 = 100 + await saved_doc_previous.replace() + + assert saved_doc_previous.get_saved_state()["num_1"] == 100 + assert saved_doc_previous.get_previous_saved_state()["num_1"] == 1 + + assert ( + saved_doc_previous.get_saved_state().get("revision_id") is None + ) + assert ( + saved_doc_previous.get_saved_state().get( + "previous_revision_id" + ) + is None + ) + + assert ( + saved_doc_previous.get_previous_saved_state().get( + "revision_id" + ) + is None + ) + assert ( + saved_doc_previous.get_previous_saved_state().get( + "previous_revision_id" + ) + is None + )