Beispiel #1
0
    def test_no_process(self):
        class N(Node):
            pass

        pipe = Pipeline(N('a') | N('b'))
        with self.assertRaises(NotImplementedError):
            pipe.consume(range(3))
Beispiel #2
0
    def test_undefined_process(self):
        class B(GroupByNode):
            def key(self, item):
                pass

        pipe = Pipeline(B('a'))

        with self.assertRaises(NotImplementedError):
            pipe.consume(range(9))
Beispiel #3
0
    def test_bad_route(self):
        def bad_router(item):
            return 'bad'

        class N(Node):
            def process(self, item):
                self.push(item)

        pipeline = Pipeline(N('a') | [N('b'), N('c'), bad_router])

        with self.assertRaises(ValueError):
            pipeline.consume(range(3))
Beispiel #4
0
 def test_logging(self):
     pipeline = Pipeline(TestNode('a') | TestNode('b'))
     pipeline['a'].log('output')
     pipeline['b'].log('input')
     with print_catcher() as catcher:
         pipeline.consume(item_generator())
     text = """
         node_log,what,node_name,item
         node_log,output,a,1|generator|a
         node_log,input,b,1|generator|a
         node_log,output,a,2|generator|a
         node_log,input,b,2|generator|a
     """
     for line in text.split('\n'):
         self.assertTrue(line.strip() in catcher.txt)
Beispiel #5
0
    def test_flattened_list(self):
        pipeline = Pipeline(TestNode('a') | [[Node('b'), Node('c')]])

        with print_catcher() as catcher:
            print(pipeline)

        self.assertTrue('a | [b, c]' in catcher.txt)
Beispiel #6
0
 def test_replace_no_router(self):
     a = TestNode('a')
     b = TestNode('b')
     pipe = Pipeline(a | b)
     pipe['b'] = TestNode('b')
     with print_catcher() as catcher:
         print(pipe)
     self.assertTrue('a | b' in catcher.txt)
Beispiel #7
0
    def test_ror(self):
        a = Node('a')
        b = Node('b')
        c = Node('c')
        d = Node('d')

        p = Pipeline(a | ([b, c] | d))
        with print_catcher() as catcher:
            print(p)
        self.assertTrue('a | [b, c]' in catcher.txt)
        self.assertTrue('c | d' in catcher.txt)
        self.assertTrue('b | d' in catcher.txt)
Beispiel #8
0
    def setUp(self):
        a = TestNode('a')
        b = TestNode('b')
        c = TestNode('c')
        d = TestNode('d')
        even = TestNode('even')
        odd = TestNode('odd')
        g = TestNode('g')

        def even_odd(item):
            return ['even', 'odd'][item.value % 2]

        a | b | [c, d] | [even, odd, even_odd] | g

        self.pipeline = Pipeline(a, global_state=GlobalState(final_items=[]))
Beispiel #9
0
    def test_manual_feed(self):
        class N(Node):
            def begin(self):
                self.global_state.out_list = []

            def process(self, item):
                self.global_state.out_list.append(item)

        pipeline = Pipeline(TestNode('a') | N('b'))
        pushed_list = []
        for item in item_generator():
            pushed_list.append(item)
            pipeline.push(item)
        pipeline.end()
        self.assertEqual(len(pipeline.global_state.out_list), 2)
Beispiel #10
0
    def test_reset(self):
        class N(Node):
            def begin(self):
                self.was_reset = False

            def process(self, item):
                self.push(item)

            def reset(self):
                self.was_reset = True

        pipe = Pipeline(N('a') | N('b'))
        pipe.consume(range(3))
        self.assertFalse(pipe['a'].was_reset)
        self.assertFalse(pipe['b'].was_reset)

        pipe.reset()

        self.assertTrue(pipe['a'].was_reset)
        self.assertTrue(pipe['b'].was_reset)
Beispiel #11
0
 def test_kwargs_passed(self):
     g = GlobalState(custom_name='custom')
     p = Pipeline(TestNode('a'), global_state=g)
     self.assertTrue(p.global_state.custom_name == 'custom')
     self.assertTrue(p.global_state['custom_name'] == 'custom')
Beispiel #12
0
 def test_batching(self):
     pipe = Pipeline(Batch('a'))
     pipe.consume(range(9))
     self.assertEqual(pipe.global_state.batches,
                      [[0, 1, 2], [3, 4, 5], [6, 7, 8]])
Beispiel #13
0
 def test_bad_replacement_name(self):
     pipeline = Pipeline(TestNode('a') | TestNode('b'))
     with self.assertRaises(ValueError):
         pipeline['b'] = TestNode('c')
Beispiel #14
0
    def test_bad_node_lookup(self):
        pipeline = Pipeline(TestNode('a') | TestNode('b'))

        with self.assertRaises(KeyError):
            pipeline['c']
Beispiel #15
0
 def test_push_in_begin(self):
     pipeline = Pipeline(BadNode('a') | TestNode('b'))
     with self.assertRaises(AttributeError):
         with print_catcher('stderr'):
             pipeline.begin()