def test_simple(self): graph = Graph() source = graph.add_source() items = [] terminated = [ False ] def add_items(inp, outp): for value in inp: items.append(len(value)) terminated[0] = True graph.add_node(add_items) graph.add_edge(source, add_items) source("hello") source("pizza") source("cheese") source("2") source("blahblahblah") graph.terminate() self.assertEqual(items, [ 5, 5, 6, 1, 12, ], "Node yielded expected items") self.assertTrue(terminated[0], "Node terminated")
def test_sink_to_callable(self): from canal.nodes import sink_to_callable graph = Graph() source = graph.add_source() values = [] def func(value): values.append(value) sink = sink_to_callable(func) graph.add_node(sink) graph.add_edge(source, sink) for value in (1, 2, 3, 4, 5): source(value) self.assertEquals(values, [1, 2, 3, 4, 5])
def __init__(self, node): graph = Graph() source = graph.add_source() graph.add_node(node) output = [] def sink(inp, outp): log.debug("Sink initializing") for value in inp: log.debug("Sink got %r", value) output.append(value) log.debug("Sink terminating") graph.add_node(sink) graph.add_edge(source, node) graph.add_edge(node, sink) self.output = output self.source = source self.graph = graph
def test_sink_to_file(self): from canal.nodes import sink_to_file from StringIO import StringIO linef = StringIO() spacef = StringIO() graph = Graph() source = graph.add_source() linesink = sink_to_file(linef) spacesink = sink_to_file(spacef, separator=" ") graph.add_node(linesink) graph.add_node(spacesink) graph.add_edge(source, linesink) graph.add_edge(source, spacesink) test_values = ("a", "b", "c", "d", "e") for value in test_values: source(value) self.assertEquals(linef.getvalue(), "\n".join(test_values + ('',))) self.assertEquals(spacef.getvalue(), " ".join(test_values + ('',)))