Skip to content

Commit

Permalink
fix: 629
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-right committed Aug 21, 2023
1 parent b9abfc4 commit 95184fb
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
WriteRules,
DeleteRules,
)
from beanie.odm.queries.update import UpdateResponse
from beanie.odm.settings.timeseries import TimeSeriesConfig, Granularity
from beanie.odm.utils.init import init_beanie
from beanie.odm.documents import Document
Expand Down Expand Up @@ -64,4 +65,6 @@
"DeleteRules",
# Custom Types
"DecimalAnnotation",
# UpdateResponse
"UpdateResponse",
]
1 change: 1 addition & 0 deletions beanie/odm/utils/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def merge_models(left: BaseModel, right: BaseModel) -> None:
break
if links_found:
continue
left.__setattr__(k, right_value)
elif not isinstance(right_value, Link):
left.__setattr__(k, right_value)

Expand Down
9 changes: 9 additions & 0 deletions docs/tutorial/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ await Product.find_one(Product.name == "Milka").delete()

await Product.find(Product.category.name == "Chocolate").delete()
```

## Response Type

For the object methods `update` and `upsert`, you can use the `response_type` parameter to specify the type of response.

The options are:
- `UpdateResponse.UPDATE_RESULT` - returns the result of the update operation.
- `UpdateResponse.NEW_DOCUMENT` - returns the newly updated document.
- `UpdateResponse.OLD_DOCUMENT` - returns the document before the update.
2 changes: 2 additions & 0 deletions tests/odm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
DocumentWithCustomInit,
DocumentWithTextIndexAndLink,
LinkDocumentForTextSeacrh,
TestDocumentWithList,
)
from tests.odm.views import TestView, TestViewWithLink

Expand Down Expand Up @@ -257,6 +258,7 @@ async def init(db):
DocumentWithCustomInit,
DocumentWithTextIndexAndLink,
LinkDocumentForTextSeacrh,
TestDocumentWithList,
]
await init_beanie(
database=db,
Expand Down
11 changes: 11 additions & 0 deletions tests/odm/documents/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
DocumentTestModel,
ModelWithOptionalField,
DocumentWithKeepNullsFalse,
TestDocumentWithList,
)


Expand Down Expand Up @@ -278,3 +279,13 @@ async def test_save_changes_keep_nulls_false():
# {"test_str": "smth_else"}, session=session
# ).to_list()
# assert len(smth_else_documetns) == 17


async def test_update_list():
test_record = TestDocumentWithList(list_values=["1", "2", "3"])
test_record = await test_record.insert()
update_data = test_record.dict()
update_data["list_values"] = ["5", "6", "7"]

updated_test_record = await test_record.update({"$set": update_data})
assert updated_test_record.list_values == update_data["list_values"]
4 changes: 4 additions & 0 deletions tests/odm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,7 @@ class Settings:
name="text_index",
)
]


class TestDocumentWithList(Document):
list_values: List[str]
17 changes: 17 additions & 0 deletions tests/odm/query/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from beanie.odm.operators.update.general import Set, Max
from beanie.odm.queries.update import UpdateResponse
from tests.odm.models import Sample


Expand Down Expand Up @@ -194,6 +195,22 @@ async def test_update_one_upsert_without_insert(
assert len(new_docs) == 0


async def test_update_one_upsert_without_insert_return_doc(
preset_documents, sample_doc_not_saved
):
result = await Sample.find_one(Sample.integer > 1).upsert(
Set({Sample.integer: 100}),
on_insert=sample_doc_not_saved,
response_type=UpdateResponse.NEW_DOCUMENT,
)
assert isinstance(result, Sample)

new_docs = await Sample.find_many(
Sample.string == sample_doc_not_saved.string
).to_list()
assert len(new_docs) == 0


async def test_update_pymongo_kwargs(preset_documents):
with pytest.raises(TypeError):
await Sample.find_many(Sample.increment > 4).update(
Expand Down
1 change: 1 addition & 0 deletions tests/odm/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async def test_multi_insert_links(self):
house.windows.append(new_window)
await house.save(link_rule=WriteRules.WRITE)
for win in house.windows:
print(type(win), win)
assert isinstance(win, Window)
assert win.id

Expand Down

0 comments on commit 95184fb

Please sign in to comment.