Skip to content

Commit

Permalink
Add reload method (#75)
Browse files Browse the repository at this point in the history
* Add reload method
* Fix skjold
  • Loading branch information
fbjorn authored Oct 16, 2024
1 parent d5dc1e0 commit 250753a
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 12 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

## [0.8.0] - 2024-10-16

### Added

- New `reload` method to refresh model state from the database.

## [0.7.2] - 2024-06-17

### Fixed
Expand Down Expand Up @@ -238,7 +244,8 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Update README.md
- Update .gitignore

[unreleased]: https://github.com/ioxiocom/firedantic/compare/0.7.2...HEAD
[unreleased]: https://github.com/ioxiocom/firedantic/compare/0.8.0...HEAD
[0.8.0]: https://github.com/ioxiocom/firedantic/compare/0.7.2...0.8.0
[0.7.2]: https://github.com/ioxiocom/firedantic/compare/0.7.1...0.7.2
[0.7.1]: https://github.com/ioxiocom/firedantic/compare/0.7.0...0.7.1
[0.7.0]: https://github.com/ioxiocom/firedantic/compare/0.6.0...0.7.0
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ company.save()

# Prints out the firestore ID of the Company model
print(company.id)

# Reloads model data from the database
company.reload()
```

Querying is done via a MongoDB-like `find()`:
Expand Down
16 changes: 16 additions & 0 deletions firedantic/_async/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ async def delete(self) -> None:
"""
await self._get_doc_ref().delete()

async def reload(self) -> None:
"""
Reloads this model from the database.
:raise ModelNotFoundError: If the document ID is missing in the model.
"""
doc_id = self.__dict__.get(self.__document_id__)
if doc_id is None:
raise ModelNotFoundError("Can not reload unsaved model")

updated_model = await self.get_by_doc_id(doc_id)
updated_model_doc_id = updated_model.__dict__[self.__document_id__]
assert doc_id == updated_model_doc_id

self.__dict__.update(updated_model.__dict__)

def get_document_id(self):
"""
Get the document ID for this model instance
Expand Down
16 changes: 16 additions & 0 deletions firedantic/_sync/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ def delete(self) -> None:
"""
self._get_doc_ref().delete()

def reload(self) -> None:
"""
Reloads this model from the database.
:raise ModelNotFoundError: If the document ID is missing in the model.
"""
doc_id = self.__dict__.get(self.__document_id__)
if doc_id is None:
raise ModelNotFoundError("Can not reload unsaved model")

updated_model = self.get_by_doc_id(doc_id)
updated_model_doc_id = updated_model.__dict__[self.__document_id__]
assert doc_id == updated_model_doc_id

self.__dict__.update(updated_model.__dict__)

def get_document_id(self):
"""
Get the document ID for this model instance
Expand Down
19 changes: 19 additions & 0 deletions firedantic/tests/tests_async/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,22 @@ async def test_get_user_purchases(configure_db):
await us(id="2021", purchases=42).save()

assert await get_user_purchases(u.id) == 42


@pytest.mark.asyncio
async def test_reload(configure_db):
u = User(name="Foo")
await u.save()

# change the value in the database
u_ = await User.find_one({"name": "Foo"})
u_.name = "Bar"
await u_.save()

assert u.name == "Foo"
await u.reload()
assert u.name == "Bar"

another_user = User(name="Another")
with pytest.raises(ModelNotFoundError):
await another_user.reload()
18 changes: 18 additions & 0 deletions firedantic/tests/tests_sync/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,21 @@ def test_get_user_purchases(configure_db):
us(id="2021", purchases=42).save()

assert get_user_purchases(u.id) == 42


def test_reload(configure_db):
u = User(name="Foo")
u.save()

# change the value in the database
u_ = User.find_one({"name": "Foo"})
u_.name = "Bar"
u_.save()

assert u.name == "Foo"
u.reload()
assert u.name == "Bar"

another_user = User(name="Another")
with pytest.raises(ModelNotFoundError):
another_user.reload()
19 changes: 9 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 = "firedantic"
version = "0.7.2"
version = "0.8.0"
description = "Pydantic base models for Firestore"
authors = ["IOXIO Ltd"]
license = "BSD-3-Clause"
Expand Down

0 comments on commit 250753a

Please sign in to comment.