-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04a9e05
commit 0d69bf0
Showing
5 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Self | ||
|
||
from asyncstdlib.functools import cached_property as async_cached_property | ||
import pytest | ||
|
||
pytestmark = [pytest.mark.asyncio] | ||
|
||
|
||
class Dummy: | ||
def __init__(self: Self) -> None: | ||
self.counter = 0 | ||
|
||
@async_cached_property | ||
async def func(self: Self) -> int: | ||
self.counter += 1 | ||
|
||
return self.counter | ||
|
||
|
||
async def test_async_cached_property() -> None: | ||
obj = Dummy() | ||
assert "func" not in obj.__dict__, "`func` key should not be cached yet" | ||
|
||
res = await obj.func | ||
assert res == 1 | ||
assert "func" in obj.__dict__, "`func` key should be cached" | ||
|
||
res = await obj.func | ||
assert res == 1, "Cached value must be used" | ||
|
||
delattr(obj, "func") | ||
res = await obj.func | ||
assert res == 2, "Expected to execute func() again, increasing the counter" | ||
assert "func" in obj.__dict__ |