forked from nameko/nameko
-
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
Miklos Vincze
committed
Jan 15, 2017
1 parent
aa6b6b8
commit 6737527
Showing
7 changed files
with
81 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.. _built_in_dependencies: | ||
|
||
Built-in Dependencies | ||
===================== | ||
|
||
Nameko includes some commonly used :ref:`dependency_injection`. This section introduces them and gives brief examples of their usage. | ||
|
||
.. _config_dependency: | ||
|
||
Config | ||
------ | ||
|
||
Config is a simple dependency provider for accessing configuration values runtime, see :ref:`running_a_service`. | ||
|
||
.. literalinclude:: examples/config_dependency.py |
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,13 @@ | ||
from nameko.dependencies import Config | ||
from nameko.rpc import rpc | ||
|
||
|
||
class Service: | ||
|
||
name = "test_config" | ||
|
||
config = Config() | ||
|
||
@rpc | ||
def get_max_workers(self): | ||
return self.config.get("max_workers") |
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,12 @@ | ||
""" Nameko built-in dependencies. | ||
""" | ||
|
||
from nameko.extensions import DependencyProvider | ||
|
||
|
||
class Config(DependencyProvider): | ||
""" Dependency provider for accessing configuration values. | ||
""" | ||
|
||
def get_dependency(self, worker_ctx): | ||
return self.container.config.copy() |
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,20 @@ | ||
from mock import Mock, call, patch | ||
import pytest | ||
|
||
from nameko.dependencies import Config | ||
|
||
|
||
class TestConfig: | ||
|
||
@pytest.yield_fixture(autouse=True) | ||
def patch_container(self): | ||
with patch.object(Config, 'container'): | ||
yield | ||
|
||
def test_get_dependency(self): | ||
config = Config() | ||
|
||
dependency = config.get_dependency(Mock(name='worker_ctx')) | ||
|
||
assert config.container.config.copy.return_value == dependency | ||
assert [call()] == config.container.config.copy.call_args_list |