Skip to content

Commit

Permalink
Merge pull request #291 from schwannden/main
Browse files Browse the repository at this point in the history
Adding Update Action
  • Loading branch information
roman-right authored Sep 20, 2022
2 parents 42a8a82 + 16033a3 commit 2634bb7
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-actions-publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
with:
python-version: 3.9
- name: install poetry
uses: abatilo/actions-poetry@v2.0.0
uses: abatilo/actions-poetry@v2
with:
poetry-version: 1.1.4
poetry-version: 1.2
- name: install dependencies
run: poetry install
- name: publish docs
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/github-actions-publish-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
with:
python-version: 3.10.5
- name: install poetry
uses: abatilo/actions-poetry@v2.0.0
uses: abatilo/actions-poetry@v2
with:
poetry-version: 1.1.4
poetry-version: 1.2
- name: install dependencies
run: poetry install
- name: publish project
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/github-actions-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
fail-fast: false
matrix:
python-version: [ 3.7, 3.8, 3.9, 3.10.5 ]
poetry-version: [ 1.1.4 ]
poetry-version: [ 1.2 ]
mongodb-version: [ 4.4, 5.0 ]
pydantic-version: [1.9.0]
os: [ ubuntu-18.04 ]
Expand All @@ -23,7 +23,7 @@ jobs:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: test-rs
- name: Run image
uses: abatilo/actions-poetry@v2.0.0
uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ matrix.poetry-version }}
- name: poetry install
Expand Down
4 changes: 3 additions & 1 deletion beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Before,
After,
Delete,
Update,
)
from beanie.odm.bulk import BulkWriter
from beanie.odm.fields import (
Expand All @@ -25,7 +26,7 @@
from beanie.odm.views import View
from beanie.odm.union_doc import UnionDoc

__version__ = "1.11.9"
__version__ = "1.11.10"
__all__ = [
# ODM
"Document",
Expand All @@ -46,6 +47,7 @@
"Delete",
"Before",
"After",
"Update",
# Bulk Write
"BulkWriter",
# Migrations
Expand Down
2 changes: 2 additions & 0 deletions beanie/odm/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class EventTypes(str, Enum):
SAVE_CHANGES = "SAVE_CHANGES"
VALIDATE_ON_SAVE = "VALIDATE_ON_SAVE"
DELETE = "DELETE"
UPDATE = "UPDATE"


Insert = EventTypes.INSERT
Replace = EventTypes.REPLACE
SaveChanges = EventTypes.SAVE_CHANGES
ValidateOnSave = EventTypes.VALIDATE_ON_SAVE
Delete = EventTypes.DELETE
Update = EventTypes.UPDATE


class ActionDirections(str, Enum): # TODO think about this name
Expand Down
4 changes: 4 additions & 0 deletions beanie/odm/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ async def replace_many(
await cls.find(In(cls.id, ids_list), session=session).delete()
await cls.insert_many(documents, session=session)

@wrap_with_actions(EventTypes.UPDATE)
@save_state_after
async def update(
self,
Expand All @@ -466,6 +467,7 @@ async def update(
session: Optional[ClientSession] = None,
bulk_writer: Optional[BulkWriter] = None,
skip_sync: bool = False,
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
**pymongo_kwargs,
) -> None:
"""
Expand All @@ -478,6 +480,7 @@ async def update(
:param **pymongo_kwargs: pymongo native parameters for update operation
:return: None
"""
print("UPDATE CALL")
use_revision_id = self.get_settings().use_revision

find_query: Dict[str, Any] = {"_id": self.id}
Expand Down Expand Up @@ -550,6 +553,7 @@ class Sample(Document):
:param skip_sync: bool - skip doc syncing. Available for the direct instances only
:return: self
"""
print("SET CALL")
return self.update(
SetOperator(expression),
session=session,
Expand Down
11 changes: 11 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

Beanie project

## [1.11.10] - 2022-09-20

### Improvement

- Adding Update Action

### Implementation

- Author - [schwannden](https://github.com/schwannden)
- PR <https://github.com/roman-right/beanie/pull/291/>

## [1.11.9] - 2022-08-19

### Fix
Expand Down
21 changes: 11 additions & 10 deletions docs/tutorial/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
You can register methods as pre- or post- actions for document events.

Currently supported events:

- `Insert`
- `Replace`
- `SaveChanges`
- `ValidateOnSave`
- Insert
- Replace
- SaveChanges
- ValidateOnSave
- Update

Currently supported directions:

- `Before`
- `After`

Current operations creating events:

- `insert()` and `save()` for Insert.
- `replace()` and `save()` for Replace.
- `save_changes()` for SaveChanges.
- `insert()`, `replace()`, `save_changes()`, and `save()` for ValidateOnSave.
- `insert()` for Insert
- `replace()` Replace
- `save()` triggers Insert if it is creating a new document, triggers Replace it replaces existing document.
- `save_changes()` for SaveChanges
- `insert()`, `replace()`, `save_changes()`, and `save()` for ValidateOnSave
- `set()`, `update()` for Update

To register an action, you can use `@before_event` and `@after_event` decorators respectively:

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "beanie"
version = "1.11.9"
version = "1.11.10"
description = "Asynchronous Python ODM for MongoDB"
authors = ["Roman <[email protected]>"]
license = "Apache-2.0"
Expand Down
11 changes: 10 additions & 1 deletion tests/odm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pydantic import BaseModel, Field
from pymongo import IndexModel

from beanie import Document, Indexed, Insert, Replace, ValidateOnSave
from beanie import Document, Indexed, Insert, Replace, ValidateOnSave, Update
from beanie.odm.actions import before_event, after_event, Delete
from beanie.odm.fields import Link
from beanie.odm.settings.timeseries import TimeSeriesConfig
Expand Down Expand Up @@ -243,6 +243,15 @@ def inner_num_to_one(self):
def inner_num_to_two(self):
self.Inner.inner_num_2 = 2

@before_event(Update)
def inner_num_to_one_2(self):
print("HERE")
self.num_1 += 1

@after_event(Update)
def inner_num_to_two_2(self):
self.num_2 -= 1


class InheritedDocumentWithActions(DocumentWithActions):
...
Expand Down
23 changes: 22 additions & 1 deletion tests/odm/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,30 @@ async def test_skip_actions_replace(self, doc_class):
"doc_class", [DocumentWithActions, InheritedDocumentWithActions]
)
async def test_actions_delete(self, doc_class):
test_name = f"test_actions_insert_{doc_class}"
test_name = f"test_actions_delete_{doc_class}"
sample = doc_class(name=test_name)

await sample.delete()
assert sample.Inner.inner_num_1 == 1
assert sample.Inner.inner_num_2 == 2

@pytest.mark.parametrize(
"doc_class", [DocumentWithActions, InheritedDocumentWithActions]
)
async def test_actions_update(self, doc_class):
test_name = f"test_actions_update_{doc_class}"
sample = doc_class(name=test_name)
await sample.save()

await sample.update({"$set": {"name": "new_name"}})
assert sample.name == "new_name"
assert sample.num_1 == 2
assert sample.num_2 == 9

await sample.set({"name": "awesome_name"}, skip_sync=True)

assert sample.num_1 == 3
assert sample.num_2 == 8

await sample._sync()
assert sample.name == "awesome_name"
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.11.9"
assert __version__ == "1.11.10"

0 comments on commit 2634bb7

Please sign in to comment.