-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement instantiated services option.
This introduces an option to enable instantiated services while maintaining backwards compatibility with the singleton pattern. It also allows passing caller arguments into the `Endpoint` declaration. Resolve #119. While I think the singleton pattern should be deprecated, put under a feature flag, and discouraged, the first step is probably to give instantiated services some field experience as an optional feature.
- Loading branch information
1 parent
7d1d059
commit cbc3a04
Showing
8 changed files
with
187 additions
and
72 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
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,38 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
import apiron | ||
|
||
|
||
def instantiated_service(returntype="instance"): | ||
os.environ["APIRON_INSTANTIATED_SERVICES"] = "1" | ||
|
||
class SomeService(apiron.Service): | ||
pass | ||
|
||
if returntype == "instance": | ||
return SomeService(domain="http://foo.com") | ||
elif returntype == "class": | ||
return SomeService | ||
|
||
raise ValueError('Expected "returntype" value to be "instance" or "class".') | ||
|
||
|
||
def singleton_service(): | ||
os.environ["APIRON_INSTANTIATED_SERVICES"] = "0" | ||
|
||
class SomeService(apiron.Service): | ||
domain = "http://foo.com" | ||
|
||
return SomeService | ||
|
||
|
||
@pytest.fixture(scope="function", params=["singleton", "instance"]) | ||
def service(request): | ||
if request.param == "singleton": | ||
yield singleton_service() | ||
elif request.param == "instance": | ||
yield instantiated_service() | ||
else: | ||
raise ValueError(f'unknown service type "{request.param}"') |
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,35 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from apiron.service.base import ServiceMeta | ||
|
||
from .. import conftest | ||
|
||
|
||
class TestInstantiatedServices: | ||
@pytest.mark.parametrize("value,result", [("0", False), ("false", False), ("1", True), ("true", True)]) | ||
def test_instantiated_services_variable_true(self, value, result): | ||
os.environ["APIRON_INSTANTIATED_SERVICES"] = value | ||
|
||
assert ServiceMeta._instantiated_services() is result | ||
|
||
@pytest.mark.parametrize("value", ["", "YES"]) | ||
def test_instantiated_services_variable_other(self, value): | ||
os.environ["APIRON_INSTANTIATED_SERVICES"] = value | ||
|
||
with pytest.raises(ValueError, match="Invalid"): | ||
ServiceMeta._instantiated_services() | ||
|
||
def test_singleton_constructor_arguments(self): | ||
"""Singleton services do not accept arguments.""" | ||
service = conftest.singleton_service() | ||
|
||
with pytest.raises(TypeError, match="object is not callable"): | ||
service(foo="bar") | ||
|
||
def test_instantiated_services_constructor_arguments(self): | ||
"""Instantiated services accept arguments.""" | ||
service = conftest.instantiated_service(returntype="class") | ||
|
||
service(foo="bar") |
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