Starting to work on #47 a.k.a the context processor mess. This is a first implementation removing all the uggly function calls, but further work must work on less things called "context", as it is bad for readability. Concerns better separated now.
This commit is contained in:
27
tests/test_basics.py
Normal file
27
tests/test_basics.py
Normal file
@ -0,0 +1,27 @@
|
||||
import pprint
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import bonobo
|
||||
import pytest
|
||||
from bonobo.config.processors import ContextCurrifier
|
||||
|
||||
|
||||
def test_count():
|
||||
with pytest.raises(TypeError):
|
||||
bonobo.count()
|
||||
|
||||
context = MagicMock()
|
||||
|
||||
currified = ContextCurrifier(bonobo.count)
|
||||
currified.setup(context)
|
||||
|
||||
for i in range(42):
|
||||
currified()
|
||||
currified.teardown()
|
||||
|
||||
context.send.assert_called_once()
|
||||
bag = context.send.call_args[0][0]
|
||||
assert isinstance(bag, bonobo.Bag)
|
||||
assert 0 == len(bag.kwargs)
|
||||
assert 1 == len(bag.args)
|
||||
assert bag.args[0] == 42
|
||||
@ -1,24 +1,26 @@
|
||||
from operator import attrgetter
|
||||
|
||||
from bonobo.config.processors import ContextProcessor, contextual, resolve_processors
|
||||
from bonobo.config import Configurable
|
||||
from bonobo.config.processors import ContextProcessor, resolve_processors, ContextCurrifier
|
||||
|
||||
|
||||
@contextual
|
||||
class CP1:
|
||||
class CP1(Configurable):
|
||||
@ContextProcessor
|
||||
def c(self):
|
||||
pass
|
||||
yield
|
||||
|
||||
@ContextProcessor
|
||||
def a(self):
|
||||
pass
|
||||
yield 'this is A'
|
||||
|
||||
@ContextProcessor
|
||||
def b(self):
|
||||
pass
|
||||
def b(self, a):
|
||||
yield a.upper()[:-1] + 'b'
|
||||
|
||||
def __call__(self, a, b):
|
||||
return a, b
|
||||
|
||||
|
||||
@contextual
|
||||
class CP2(CP1):
|
||||
@ContextProcessor
|
||||
def f(self):
|
||||
@ -33,7 +35,6 @@ class CP2(CP1):
|
||||
pass
|
||||
|
||||
|
||||
@contextual
|
||||
class CP3(CP2):
|
||||
@ContextProcessor
|
||||
def c(self):
|
||||
@ -52,3 +53,11 @@ def test_inheritance_and_ordering():
|
||||
assert get_all_processors_names(CP1) == ['c', 'a', 'b']
|
||||
assert get_all_processors_names(CP2) == ['c', 'a', 'b', 'f', 'e', 'd']
|
||||
assert get_all_processors_names(CP3) == ['c', 'a', 'b', 'f', 'e', 'd', 'c', 'b']
|
||||
|
||||
|
||||
def test_setup_teardown():
|
||||
o = CP1()
|
||||
stack = ContextCurrifier(o)
|
||||
stack.setup()
|
||||
assert o(*stack.context) == ('this is A', 'THIS IS b')
|
||||
stack.teardown()
|
||||
@ -1,4 +1,4 @@
|
||||
from bonobo.config.processors import contextual
|
||||
from bonobo.config.processors import ContextProcessor
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.graph import GraphExecutionContext
|
||||
from bonobo.strategies import NaiveStrategy
|
||||
@ -13,12 +13,11 @@ def square(i: int) -> int:
|
||||
return i**2
|
||||
|
||||
|
||||
@contextual
|
||||
def push_result(results, i: int):
|
||||
results.append(i)
|
||||
|
||||
|
||||
@push_result.__processors__.append
|
||||
@ContextProcessor.decorate(push_result)
|
||||
def results(f, context):
|
||||
results = []
|
||||
yield results
|
||||
|
||||
Reference in New Issue
Block a user