-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from BradLewis/develop
Adding optional arguments and better error messaging.
- Loading branch information
Showing
4 changed files
with
119 additions
and
9 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,26 @@ | ||
from simple_injection.service_collection import ServiceResolutionError | ||
import pytest | ||
from tests.classes import A, B, C | ||
from simple_injection import ServiceCollection | ||
|
||
|
||
class D: | ||
def __init__(self, b: B): | ||
self._b = b | ||
|
||
|
||
def test_resolution_error(): | ||
collection = ServiceCollection() | ||
collection.add_transient(B) | ||
|
||
with pytest.raises(ServiceResolutionError): | ||
collection.resolve(B) | ||
|
||
|
||
def test_nested_resolution_error(): | ||
collection = ServiceCollection() | ||
collection.add_transient(D) | ||
collection.add_transient(B) | ||
|
||
with pytest.raises(ServiceResolutionError): | ||
collection.resolve(D) |
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,48 @@ | ||
from simple_injection import ServiceCollection | ||
from typing import Optional | ||
from tests.classes import A, B, C | ||
|
||
|
||
class HasOptional: | ||
def __init__(self, a: A, b: Optional[B] = None): | ||
self._a = a | ||
self._b = b | ||
|
||
|
||
class HasOptionalWithDeps: | ||
def __init__(self, a: A, c: Optional[C] = None): | ||
self._a = a | ||
self._c = c | ||
|
||
|
||
def test_optional_declared(): | ||
collection = ServiceCollection() | ||
collection.add_transient(A) | ||
collection.add_transient(B) | ||
collection.add_transient(HasOptional) | ||
|
||
has_optional = collection.resolve(HasOptional) | ||
assert isinstance(has_optional._a, A) | ||
assert isinstance(has_optional._b, B) | ||
|
||
|
||
def test_optional_not_declared(): | ||
collection = ServiceCollection() | ||
collection.add_transient(A) | ||
collection.add_transient(HasOptional) | ||
|
||
has_optional = collection.resolve(HasOptional) | ||
assert isinstance(has_optional._a, A) | ||
assert has_optional._b is None | ||
|
||
|
||
def test_optional_with_dependencies(): | ||
collection = ServiceCollection() | ||
collection.add_transient(A) | ||
collection.add_transient(B) | ||
collection.add_transient(C) | ||
collection.add_transient(HasOptionalWithDeps) | ||
|
||
has_optional_with_deps = collection.resolve(HasOptionalWithDeps) | ||
assert isinstance(has_optional_with_deps._a, A) | ||
assert isinstance(has_optional_with_deps._c, C) |