-
Notifications
You must be signed in to change notification settings - Fork 28
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
Showing
4 changed files
with
219 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
""" | ||
Param | ||
===== | ||
Params are values that are passed to the activities. They are either provided | ||
by the execution context (see Param) or are statically provided at the runtime | ||
of the activity (see StaticParam). Custom params should extend the Param class. | ||
Note: | ||
Make sure the custom param class lists out all the dependencies to the | ||
execution context in `requirements`. | ||
""" | ||
|
||
|
||
class BaseParam: | ||
"""Base Param Class. | ||
Provides the structure and required methods of any param class. | ||
""" | ||
|
||
@property | ||
def requirements(self): | ||
"""Return the requirements for this param. | ||
""" | ||
|
||
return | ||
yield | ||
|
||
def get_data(self, context): | ||
"""Get the data. | ||
Args: | ||
context (dict): the context (optional) in which the data might be | ||
found. For Static Param this won't be necessary. | ||
""" | ||
|
||
raise NotImplementedError() | ||
|
||
|
||
class Param(BaseParam): | ||
|
||
def __init__(self, context_key): | ||
"""Create a default param. | ||
Args: | ||
context_key (str): the context key. | ||
""" | ||
|
||
self.context_key = context_key | ||
|
||
@property | ||
def requirements(self): | ||
"""Return the requirements for this param. | ||
""" | ||
|
||
yield self.context_key | ||
|
||
def get_data(self, context): | ||
"""Get value from the context. | ||
Args: | ||
context (dict): the context in which the data might be found based | ||
on the key provided. | ||
Return: | ||
obj: an object from the context that corresponds to the context | ||
key. | ||
""" | ||
|
||
return context.get(self.context_key, None) | ||
|
||
|
||
class StaticParam(BaseParam): | ||
|
||
def __init__(self, value): | ||
"""Create a static param. | ||
Args: | ||
value (str): the value of the param. | ||
""" | ||
|
||
self.value = value | ||
|
||
def get_data(self, context): | ||
"""Get value from the context. | ||
Args: | ||
context (dict): execution context (not used.) | ||
""" | ||
|
||
|
||
return self.value | ||
|
||
|
||
def get_all_requirements(params): | ||
"""Get all the requirements from a list of params. | ||
Args: | ||
params (list): The list of params. | ||
""" | ||
|
||
requirements = [] | ||
for param in params: | ||
requirements += list(param.requirements) | ||
return requirements | ||
|
||
|
||
def parametrize(requirement): | ||
"""Parametrize a requirement. | ||
Args: | ||
requirement (*): the requirement to parametrize. | ||
""" | ||
|
||
if isinstance(requirement, str): | ||
return Param(requirement) | ||
elif isinstance(requirement, BaseParam): | ||
return requirement | ||
raise UnknownParamException() | ||
|
||
|
||
class UnknownParamException(Exception): | ||
pass |
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,77 @@ | ||
import pytest | ||
|
||
from garcon import param | ||
|
||
|
||
def test_base_param_class(): | ||
"""Test the base class: cannot get data and return no requirements. | ||
""" | ||
|
||
current_param = param.BaseParam() | ||
with pytest.raises(NotImplementedError): | ||
current_param.get_data({}) | ||
|
||
assert not list(current_param.requirements) | ||
|
||
|
||
def test_static_param(): | ||
"""Test the behavior of the static param class | ||
""" | ||
|
||
message = 'Hello World' | ||
current_param = param.StaticParam(message) | ||
assert current_param.get_data({}) is message | ||
assert not list(current_param.requirements) | ||
|
||
|
||
def test_default_param(): | ||
"""Test the behavior of the default param class. | ||
""" | ||
|
||
key = 'context.key' | ||
message = 'Hello World' | ||
current_param = param.Param(key) | ||
requirements = list(current_param.requirements) | ||
assert current_param.get_data({key: message}) is message | ||
assert requirements[0] is key | ||
|
||
|
||
def test_all_requirements(): | ||
"""Test getting all the requirements. | ||
""" | ||
|
||
keys = ['context.key1', 'context.key2', 'context.key3'] | ||
manual_keys = ['context.manual_key1', 'context.manual_key2'] | ||
params = [param.Param(key) for key in keys] | ||
params += manual_keys | ||
params += [param.StaticParam('Value')] | ||
params = [param.parametrize(current_param) for current_param in params] | ||
|
||
resp = param.get_all_requirements(params) | ||
for key in keys: | ||
assert key in resp | ||
|
||
for manual_key in manual_keys: | ||
assert manual_key in resp | ||
|
||
assert 'Value' not in resp | ||
|
||
|
||
def test_parametrize(): | ||
"""Test parametrize. | ||
Parametrize only allows objects that inherits BaseParam or string. | ||
""" | ||
|
||
keys = ['context.key1', 'context.key2', 'context.key3'] | ||
manual_keys = ['context.manual_key1', 'context.manual_key2'] | ||
params = [param.Param(key) for key in keys] | ||
params += manual_keys | ||
params += [param.StaticParam('Value')] | ||
params = [param.parametrize(current_param) for current_param in params] | ||
|
||
for current_param in params: | ||
assert isinstance(current_param, param.BaseParam) | ||
|
||
with pytest.raises(param.UnknownParamException): | ||
param.parametrize(list('Unknown')) |
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