Skip to content

Commit

Permalink
Dnt create state on init for docs with custom id types (#486)
Browse files Browse the repository at this point in the history
* fixed

* version: 1.18.0b1
  • Loading branch information
roman-right committed Feb 9, 2023
1 parent 560692f commit 31bf561
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion beanie/__init__.py
Expand Up @@ -27,7 +27,7 @@
from beanie.odm.views import View
from beanie.odm.union_doc import UnionDoc

__version__ = "1.18.0b0"
__version__ = "1.18.0b1"
__all__ = [
# ODM
"Document",
Expand Down
2 changes: 0 additions & 2 deletions beanie/odm/documents.py
Expand Up @@ -143,8 +143,6 @@ def _swap_revision(self):
def __init__(self, *args, **kwargs):
super(Document, self).__init__(*args, **kwargs)
self.get_motor_collection()
self._save_state()
self._swap_revision()

@classmethod
async def get(
Expand Down
13 changes: 10 additions & 3 deletions beanie/odm/utils/parsing.py
Expand Up @@ -33,6 +33,13 @@ def merge_models(left: BaseModel, right: BaseModel) -> None:
left.__setattr__(k, right_value)


def save_state_swap_revision(item: BaseModel):
if hasattr(item, "_save_state"):
item._save_state()
if hasattr(item, "_swap_revision"):
item._swap_revision()


def parse_obj(
model: Union[Type[BaseModel], Type["Document"]],
data: Any,
Expand Down Expand Up @@ -76,8 +83,6 @@ def parse_obj(
lazy_parse=lazy_parse,
) # type: ignore

# if hasattr(model, "_parse_obj_saving_state"):
# return model._parse_obj_saving_state(data) # type: ignore
if (
lazy_parse
and hasattr(model, "get_model_type")
Expand All @@ -86,4 +91,6 @@ def parse_obj(
o = model.lazy_parse(data, {"_id"})
o._saved_state = {"_id": o.id}
return o
return model.parse_obj(data)
result = model.parse_obj(data)
save_state_swap_revision(result)
return result
13 changes: 13 additions & 0 deletions docs/changelog.md
Expand Up @@ -2,6 +2,19 @@

Beanie project

## [1.18.0b1] - 2023-02-09

### Fix

- Don't create state on init for docs with custom id types

### Implementation

- Author - [Roman Right](https://github.com/roman-right)
- PR <https://github.com/roman-right/beanie/pull/486>

[1.18.0b1]: https://pypi.org/project/beanie/1.18.0b1

## [1.18.0b0] - 2023-01-30

### Feature
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "beanie"
version = "1.18.0b0"
version = "1.18.0b1"
description = "Asynchronous Python ODM for MongoDB"
authors = ["Roman <roman-right@protonmail.com>"]
license = "Apache-2.0"
Expand Down
2 changes: 2 additions & 0 deletions tests/odm/conftest.py
Expand Up @@ -69,6 +69,7 @@
SelfLinked,
LoopedLinksA,
LoopedLinksB,
DocumentWithTurnedOnStateManagementWithCustomId,
)
from tests.odm.views import TestView, TestViewWithLink

Expand Down Expand Up @@ -223,6 +224,7 @@ async def init(loop, db):
SelfLinked,
LoopedLinksA,
LoopedLinksB,
DocumentWithTurnedOnStateManagementWithCustomId,
]
await init_beanie(
database=db,
Expand Down
9 changes: 9 additions & 0 deletions tests/odm/models.py
Expand Up @@ -324,6 +324,15 @@ class Settings:
use_state_management = True


class DocumentWithTurnedOnStateManagementWithCustomId(Document):
id: int
num_1: int
num_2: int

class Settings:
use_state_management = True


class DocumentWithTurnedOnReplaceObjects(Document):
num_1: int
num_2: int
Expand Down
22 changes: 18 additions & 4 deletions tests/odm/test_state_management.py
Expand Up @@ -3,6 +3,7 @@

from beanie import PydanticObjectId, WriteRules
from beanie.exceptions import StateManagementIsTurnedOff, StateNotSaved
from beanie.odm.utils.parsing import parse_obj
from tests.odm.models import (
DocumentWithTurnedOffStateManagement,
DocumentWithTurnedOnReplaceObjects,
Expand All @@ -13,6 +14,7 @@
LockWithRevision,
WindowWithRevision,
StateAndDecimalFieldModel,
DocumentWithTurnedOnStateManagementWithCustomId,
)


Expand All @@ -37,17 +39,17 @@ def state_without_id():

@pytest.fixture
def doc_default(state):
return DocumentWithTurnedOnStateManagement.parse_obj(state)
return parse_obj(DocumentWithTurnedOnStateManagement, state)


@pytest.fixture
def doc_replace(state):
return DocumentWithTurnedOnReplaceObjects.parse_obj(state)
return parse_obj(DocumentWithTurnedOnReplaceObjects, state)


@pytest.fixture
def doc_previous(state):
return DocumentWithTurnedOnSavePrevious.parse_obj(state)
return parse_obj(DocumentWithTurnedOnSavePrevious, state)


@pytest.fixture
Expand Down Expand Up @@ -103,7 +105,7 @@ async def test_parse_object_with_saving_state(self):
"_id": ObjectId(),
"internal": InternalDoc(),
}
doc = DocumentWithTurnedOnStateManagement.parse_obj(obj)
doc = parse_obj(DocumentWithTurnedOnStateManagement, obj)
assert doc.get_saved_state() == obj
assert doc.get_previous_saved_state() is None

Expand Down Expand Up @@ -136,6 +138,18 @@ async def test_save_state(self):
}
assert doc.get_previous_saved_state() is None

async def test_save_state_with_custom_id_type(self):
doc = DocumentWithTurnedOnStateManagementWithCustomId(
id=0,
num_1=1,
num_2=2,
)
with pytest.raises(StateNotSaved):
await doc.save_changes()
doc.num_1 = 2
with pytest.raises(StateNotSaved):
await doc.save_changes()

async def test_save_state_with_previous(self):
doc = DocumentWithTurnedOnSavePrevious(
num_1=1, num_2=2, internal=InternalDoc(num=1, string="s")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.18.0b0"
assert __version__ == "1.18.0b1"

0 comments on commit 31bf561

Please sign in to comment.