def test_run_default_should_run_default_funcs_in_a_batch(s): """rv = run_default(default, data) rv should be result of batch calls to default functions """ default = [ lambda data, state: (data + 'c'), lambda data, state: (data * 3) ] r = Runner(lambda *a, **k: None) assert r.run_default(default, 'ab') == 'abcabcabc'
def test_run_override_should_run_override_funcs_in_a_batch(s): """rv = run_override(override, data) rv should be result of batch calls to override functions """ override = [ lambda data, state: (data + 'c'), lambda data, state: (data * 3) ] r = Runner(lambda *a, **k: None) assert r.run_override(override, 'ab') == 'abcabcabc'
def test_run_should_call_save_or_fail_if_processor_fails(s, mocker): """run() if ProcProxy.run() fails, should call save_or_fail() """ def proc(data, state): raise err.ValidationError('failure') proc.failsafe = [lambda *a, **k: 'abc'] r = Runner(proc) mk_save_or_fail = mocker.patch.object(r, 'save_or_fail') r.run() assert mk_save_or_fail.called
def test_run_should_not_call_run_default_if_data_not_missing(s, mocker): """run(data) if data is not missing, should not call run_default() """ def proc(data, state): return data d = mocker.Mock(return_value='abc') proc.default = [d] r = Runner(proc) r.run(data=None) assert not d.called
def test_run_should_not_call_save_or_fail_if_processor_succeeds(s, mocker): """run() if processor succeeds, should not call save_or_fail() """ def proc(data, state): return 123 proc.failsafe = [lambda *a, **k: 'abc'] r = Runner(proc) mk_save_or_fail = mocker.patch.object(r, 'save_or_fail') assert r.run() == 123 assert not mk_save_or_fail.called
def test_run_should_still_call_procproxy_run_after_run_default( s, mocker, logger): """run() if run_default called, should still call ProcProxy.run() """ proc = lambda *a, **k: 123 proc.default = [lambda *a, **k: 'abc'] r = Runner(proc) run_default = mocker.patch.object(r, 'run_default') procproxy = mocker.patch.object(r, 'processor') r.run() assert run_default.called assert procproxy.run.called
def test_run_should_call_run_default_if_procproxy_default_set( s, mocker, logger): """run() if data missing and processor.default set, should call run_default() """ def proc(data, state): return data proc.default = mocker.Mock() r = Runner(proc) mk_run_default = mocker.patch.object(r, 'run_default', return_value='abc') r.run() assert mk_run_default.called
def test_if_processor_has_vino_init_should_call_it(s, mocker): """ Runner(p) if p has vino_init(), should call it """ mk = mocker.Mock(spec=['vino_init', 'run']) r = Runner(mk) assert mk.vino_init.called
def test_get_processor_proxy_should_return_ProcProxy_object(s): """pp = get_processor_proxy(proc) if proc valid processor, pp should be a ProcProxy instance """ class P1: def run(self, data, state): return data p1 = P1() class P2: def __call__(self, data, state): return data p2 = P2() pp1 = Runner.get_processor_proxy(p1) pp2 = Runner.get_processor_proxy(p2) assert isinstance(pp1, Runner.ProcProxy) assert isinstance(pp2, Runner.ProcProxy)
def test_if_proc_callable_procproxy_run_should_refer_to_proc(s): """pp = get_processor_proxy(proc) if proc is callable, pp.run should be a reference to proc """ class P: def __call__(self, data, state): return data p = P() pp = Runner.get_processor_proxy(p) assert pp.run is p
def test_processor_with_run_method_should_register(s): """Runner(proc) if proc has run() method, should register it """ class P: def run(self, data, state): return data processor = P() r = Runner(processor) assert hasattr(r, 'processor')
def test_callable_processor_should_register(s): """Runner(proc) if proc is callable, should register it """ class P: def __call__(self, data, state): return data processor = P() r = Runner(processor) assert hasattr(r, 'processor')
def test_if_proc_has_run_procproxy_run_should_refer_to_proc_run(s): """pp = get_processor_proxy(proc) if proc.run is callable, pp.run should be a reference to proc.run """ class P: def run(self, data, state): return data p = P() pp = Runner.get_processor_proxy(p) # because of descriptor protocol we use equality rather than identity assert pp.run == p.run
def test_processor_not_callable_with_no_run_method_should_be_rejected(s): """Runner(p) if p not callable and has no run() method, should not register it """ class P: def execute(self, data, state): return 'abc' p = P() with pytest.raises(err.VinoError) as e: r = Runner(p) assert 'invalid processor' in str(e.value).lower()
def test_procproxy_override_should_default_to_None(s): """pp = get_processor_proxy(p) pp.override should default to None """ class P: def run(self, data, state): return data processor1 = P() processor2 = lambda d, s: d for p in (processor1, processor2): pp = Runner.get_processor_proxy(p) assert pp.override is None
def test_run_should_call_run_override_first_if_procproxy_override_set( s, mocker): """run() if processor.override is set, should call run_override() first """ proc = mocker.Mock(spec=['override', 'run', 'default', 'failsafe']) def override(*a, **kw): raise Exception('override called first') def default(*a, **kw): raise Exception('default called first') def failsafe(*a, **kw): raise Exception('failsafe called first') proc.override = [override] proc.default = [default] proc.failsafe = [failsafe] r = Runner(proc) with pytest.raises(Exception) as e: r.run() assert 'override called first' in str(e.value)
def test_procproxy_object_should_have_4_attributes(s): """pp = get_processor_proxy(p) pp should have 4 attributes """ class P: def run(self, data, state): return data processor1 = P() processor2 = lambda d, s: d for p in (processor1, processor2): pp = Runner.get_processor_proxy(p) assert hasattr(pp, 'default') assert hasattr(pp, 'override') assert hasattr(pp, 'failsafe') assert hasattr(pp, 'raw_processor')
def test_procproxy_raw_processor_should_refer_to_original_proc(s): """pp = get_processor_proxy(proc) pp.raw_processor should be proc""" class P1: def run(self, data, state): return data p1 = P1() class P2: def __call__(self, data, state): return data p2 = P2() p3 = lambda d, s: d for p in (p1, p2, p3): pp = Runner.get_processor_proxy(p) assert pp.raw_processor is p
def test_run_successfully_should_return_processor_rv(s, randstr): '''result = run(value) if no contingency intervenes, result should be processor's rv''' processor = lambda d, s: d * 3 r = Runner(processor) assert r.run(randstr) == processor(randstr, None)