-
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
de3700b
commit 675ed83
Showing
6 changed files
with
130 additions
and
4 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
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,7 @@ | ||
import pytest | ||
from gyjd import setup_defaults | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def reset_injector(): | ||
setup_defaults(clear_dependencies=True) |
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,40 @@ | ||
from dataclasses import dataclass, field | ||
from uuid import uuid4 | ||
|
||
from gyjd import gyjd | ||
|
||
|
||
@dataclass | ||
class MockConfig: | ||
mock_id: str = field(default_factory=lambda: str(uuid4())) | ||
|
||
|
||
def gen_mock() -> MockConfig: | ||
return MockConfig() | ||
|
||
|
||
@gyjd | ||
def get_mock(mock: MockConfig = None) -> str: | ||
for _ in range(10): | ||
# Ensure that instance is reused on each attribute access | ||
mock.mock_id | ||
|
||
return mock.mock_id | ||
|
||
|
||
def test_reuse_25_times_injection(): | ||
gyjd.register_dependency(gen_mock, reuse_times=25) | ||
|
||
assert len(set([get_mock() for _ in range(100)])) == 4 | ||
|
||
|
||
def test_reuse_10_times_injection(): | ||
gyjd.register_dependency(gen_mock, reuse_times=10) | ||
|
||
assert len(set([get_mock() for _ in range(100)])) == 10 | ||
|
||
|
||
def test_reuse_05_times_injection(): | ||
gyjd.register_dependency(gen_mock, reuse_times=5) | ||
|
||
assert len(set([get_mock() for _ in range(100)])) == 20 |
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,31 @@ | ||
from dataclasses import dataclass, field | ||
from uuid import uuid4 | ||
|
||
from gyjd import gyjd | ||
|
||
|
||
@dataclass | ||
class MockConfig: | ||
mock_id: str = field(default_factory=lambda: str(uuid4())) | ||
|
||
|
||
def gen_mock() -> MockConfig: | ||
return MockConfig() | ||
|
||
|
||
@gyjd | ||
def get_mock(mock: MockConfig = None) -> str: | ||
for _ in range(10): | ||
# Ensure that instance is reused on each attribute access | ||
mock.mock_id | ||
|
||
return mock.mock_id | ||
|
||
|
||
def test_singleton_injection(): | ||
gyjd.register_dependency(gen_mock) | ||
|
||
first_mock_id = get_mock() | ||
|
||
for _ in range(10): | ||
assert get_mock() == first_mock_id |
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,29 @@ | ||
from dataclasses import dataclass, field | ||
from uuid import uuid4 | ||
|
||
from gyjd import gyjd | ||
|
||
|
||
@dataclass | ||
class MockConfig: | ||
mock_id: str = field(default_factory=lambda: str(uuid4())) | ||
|
||
|
||
def gen_mock() -> MockConfig: | ||
return MockConfig() | ||
|
||
|
||
@gyjd | ||
def get_mock(mock: MockConfig = None) -> str: | ||
for _ in range(10): | ||
# Ensure that instance is reused on each attribute access | ||
mock.mock_id | ||
|
||
return mock.mock_id | ||
|
||
|
||
def test_transient_injection(): | ||
gyjd.register_dependency(gen_mock, reuse_times=0) | ||
|
||
# Ensure that each instance is unique | ||
assert len(set([get_mock() for _ in range(100)])) == 100 |