def test_invoke_processor_wrapper(caplog): """.invoke() recognizes syntax sugar.""" testapp = holocron.Application() marker = None def processor(app, items): yield from items def processor_wrapper(app, items, processor, *, secret): nonlocal marker marker = secret yield from app.invoke([processor]) testapp.add_processor("processor", processor) testapp.add_processor_wrapper("wrapper", processor_wrapper) for _ in testapp.invoke([{ "name": "processor", "wrapper": { "secret": 42 } }]): pass assert marker == 42 assert len(caplog.records) == 0
def test_add_processor_override(caplog): """.add_processor() overrides registered one.""" testapp = holocron.Application() marker = None def processor_a(app, items): nonlocal marker marker = 42 yield from items def processor_b(app, items): nonlocal marker marker = 13 yield from items testapp.add_processor("processor", processor_a) testapp.add_processor("processor", processor_b) for _ in testapp.invoke([{"name": "processor"}]): pass assert marker == 13 assert len(caplog.records) == 1 assert caplog.records[0].message == "processor override: 'processor'"
def testapp(request): def spam(app, items, *, text=42): for item in items: item["spam"] = text yield item def rice(app, items): yield from items yield holocron.Item({"content": "rice"}) def eggs(app, items): while True: try: item_a = next(items) item_b = next(items) except StopIteration: break else: yield holocron.Item({"key": item_a["key"] + item_b["key"]}) instance = holocron.Application() instance.add_processor("spam", spam) instance.add_processor("rice", rice) instance.add_processor("eggs", eggs) return instance
def test_invoke_empty_pipe(): """.invoke() returns nothing.""" testapp = holocron.Application() testapp.add_pipe("test", []) with pytest.raises(StopIteration): next(testapp.invoke("test"))
def test_invoke_pipe_not_found(): """.invoke() raises proper exception.""" testapp = holocron.Application() with pytest.raises(ValueError) as excinfo: next(testapp.invoke("test")) assert str(excinfo.value) == "no such pipe: 'test'"
def test_invoke_processor_not_found(): """.invoke() raises proper exception.""" testapp = holocron.Application() testapp.add_pipe("test", [{"name": "processor"}]) with pytest.raises(ValueError) as excinfo: next(testapp.invoke("test")) assert str(excinfo.value) == "no such processor: 'processor'"
def test_metadata(): """.metadata property works like mapping.""" testapp = holocron.Application() with pytest.raises(KeyError, match="'yoda'"): testapp.metadata["yoda"] assert testapp.metadata.get("yoda", "master") == "master" testapp.metadata["yoda"] = "master" assert testapp.metadata["yoda"] == "master"
def test_metadata_init(): """.metadata property is initialized.""" testapp = holocron.Application({"yoda": "master", "vader": "sith"}) assert testapp.metadata["yoda"] == "master" assert testapp.metadata["vader"] == "sith" testapp.metadata["vader"] = "darth" assert testapp.metadata["vader"] == "darth" assert testapp.metadata["yoda"] == "master"
def test_invoke_propagates_processor_args(processor_args): """.invoke() propagates processor's arguments.""" def processor(app, items, **args): assert args == processor_args yield from items testapp = holocron.Application() testapp.add_processor("processor", processor) testapp.add_pipe("test", [{"name": "processor", "args": processor_args}]) with pytest.raises(StopIteration): next(testapp.invoke("test"))
def test_invoke_anonymous_pipe(): """.invoke() works with anonymous pipes.""" def processor_a(app, items): items = list(items) assert items == [holocron.Item({"a": "b"})] items[0]["x"] = 42 yield from items def processor_b(app, items): items = list(items) assert items == [holocron.Item({"a": "b", "x": 42})] items.append(holocron.Item({"z": 13})) yield from items def processor_c(app, items): items = list(items) assert items == [ holocron.Item({ "a": "b", "x": 42 }), holocron.Item({"z": 13}), ] yield from items testapp = holocron.Application() testapp.add_processor("processor_a", processor_a) testapp.add_processor("processor_b", processor_b) testapp.add_processor("processor_c", processor_c) stream = testapp.invoke( [ { "name": "processor_a" }, { "name": "processor_b" }, { "name": "processor_c" }, ], [holocron.Item({"a": "b"})], ) assert next(stream) == holocron.Item({"a": "b", "x": 42}) assert next(stream) == holocron.Item({"z": 13}) with pytest.raises(StopIteration): next(stream)
def test_invoke_processor_wrapper_not_found(caplog): """.invoke() raises proper exception.""" testapp = holocron.Application() def processor(app, items): yield from items testapp.add_processor("processor", processor) with pytest.raises(ValueError) as excinfo: next(testapp.invoke([{"name": "processor", "wrapper": {}}])) assert str(excinfo.value) == "no such processor: 'wrapper'" assert len(caplog.records) == 0
def test_invoke_processor_errors(): """.invoke() raises proper exception.""" def processor(app, documents): raise ValueError("something bad happened") yield testapp = holocron.Application() testapp.add_processor("processor", processor) testapp.add_pipe("test", [{"name": "processor"}]) stream = testapp.invoke("test") with pytest.raises(ValueError, match=r"^something bad happened$"): next(stream) with pytest.raises(StopIteration): next(stream)
def test_add_processor_wrapper_illegal_name(caplog, wrapper_name): """.add_processor_wrapper() raises on illegal name.""" testapp = holocron.Application() def processor(app, items): yield from items def processor_wrapper(app, items, processor): yield from app.invoke([processor]) testapp.add_processor("processor", processor) with pytest.raises(ValueError) as excinfo: testapp.add_processor_wrapper(wrapper_name, processor_wrapper) assert str(excinfo.value) == f"illegal wrapper name: {wrapper_name}" assert len(caplog.records) == 0
def test_add_processor(caplog): """.add_processor() registers a processor.""" testapp = holocron.Application() marker = None def processor(app, items): nonlocal marker marker = 42 yield from items testapp.add_processor("processor", processor) for _ in testapp.invoke([{"name": "processor"}]): pass assert marker == 42 assert len(caplog.records) == 0
def test_invoke_passthrough_items_empty_pipe(): """.invoke() passes through items on empty pipe.""" testapp = holocron.Application() testapp.add_pipe("test", []) stream = testapp.invoke( "test", [ holocron.Item(name="yoda", rank="master"), holocron.Item(name="skywalker"), ], ) assert next(stream) == holocron.Item(name="yoda", rank="master") assert next(stream) == holocron.Item(name="skywalker") with pytest.raises(StopIteration): next(testapp.invoke("test"))
def test_invoke_resolves_jsonref(processor_args, resolved): """.invoke() resolves JSON references in processor's arguments.""" testapp = holocron.Application({ "extra": [{ "luke": "skywalker" }], "is_yoda_master": True }) def processor(app, items, **args): assert args == resolved yield from items testapp.add_processor("processor", processor) testapp.add_pipe("test", [{"name": "processor", "args": processor_args}]) with pytest.raises(StopIteration): next(testapp.invoke("test"))
def testapp(): def spam(app, items, **args): for item in items: item["spam"] = args.get("text", 42) yield item def eggs(app, items, **args): for item in items: item["content"] += " #friedeggs" yield item def rice(app, items, **args): yield from items yield holocron.Item({"content": "rice"}) instance = holocron.Application() instance.add_processor("spam", spam) instance.add_processor("eggs", eggs) instance.add_processor("rice", rice) return instance
def test_invoke_passthrough_items(): """.invoke() passes through items on non empty pipe.""" def processor(app, items): yield from items testapp = holocron.Application() testapp.add_processor("processor", processor) testapp.add_pipe("test", [{"name": "processor"}]) stream = testapp.invoke( "test", [ holocron.Item(name="yoda", rank="master"), holocron.Item(name="skywalker"), ], ) assert next(stream) == holocron.Item(name="yoda", rank="master") assert next(stream) == holocron.Item(name="skywalker") with pytest.raises(StopIteration): next(testapp.invoke("test"))
def testapp(): return holocron.Application({"url": "https://yoda.ua"})
def testapp(): return holocron.Application()
def testapp(request): return holocron.Application()