Example #1
0
    def test_remap_name(self):
        m = Mock()

        runner = Runner()
        runner.add(m.job1, label='one')

        class MyPlug(Plug):

            @insert(label='one')
            def run_plug(self):
                m.plug_one()

        plug = MyPlug()
        plug.add_to(runner)

        runner()

        compare([
            call.job1(),
            call.plug_one()
        ], m.mock_calls)

        verify(runner,
               (m.job1, set()),
               (plug.run_plug, {'one'}),
               )
Example #2
0
    def test_replace_explicit_requires_returns(self):
        m = Mock()
        class T1(object): pass
        class T2(object): pass
        class T3(object): pass
        class T4(object): pass

        t2 = T2()
        def job0():
            return t2

        @requires(T1)
        @returns(T3)
        def job1(obj):
            raise Exception() # pragma: nocover

        job2 = requires(T4)(m.job2)
        runner = Runner(job0, job1, job2)

        runner.replace(job1, m.job1, requires=T2, returns=T4)
        runner()

        compare([
            call.job1(t2),
            call.job2(m.job1.return_value),
        ], actual=m.mock_calls)
Example #3
0
    def test_context_declarative(self):
        m = Mock()        
        class T1(object): pass
        class T2(object): pass

        t1 = T1()
        t2 = T2()

        def job1():
            m.job1()
            return t1

        @requires(T1)
        def job2(obj):
            m.job2(obj)
            return t2

        @requires(T2)
        def job3(obj):
            m.job3(obj)

        runner = Runner(job1, job2, job3)
        runner()
        
        compare([
                call.job1(),
                call.job2(t1),
                call.job3(t2),
                ], m.mock_calls)
Example #4
0
    def test_returns_list(self):
        m = Mock()
        class T1(object): pass
        class T2(object): pass

        t1 = T1()
        t2 = T2()

        def job1():
            m.job1()
            return [t1, t2]

        @requires(obj1=T1, obj2=T2)
        def job2(obj1, obj2):
            m.job2(obj1, obj2)

        runner = Runner()
        runner.add(job1, returns=returns(T1, T2))
        runner.add(job2)
        runner()

        compare([
                call.job1(),
                call.job2(t1, t2),
                ], m.mock_calls)
Example #5
0
    def test_simple(self):
        m = Mock()

        runner = Runner()
        runner.add(m.job1, label='one')
        runner.add(m.job2)
        runner.add(m.job3, label='three')
        runner.add(m.job4)

        class MyPlug(Plug):

            def one(self):
                m.plug_one()

            def three(self):
                m.plug_two()

        plug = MyPlug()
        plug.add_to(runner)

        runner()

        compare([
            call.job1(), call.plug_one(), call.job2(),
            call.job3(), call.plug_two(), call.job4()
        ], m.mock_calls)

        verify(runner,
               (m.job1, set()),
               (plug.one, {'one'}),
               (m.job2, set()),
               (m.job3, set()),
               (plug.three, {'three'}),
               (m.job4, set()),
               )
Example #6
0
    def test_ignore(self):
        m = Mock()

        runner = Runner()
        runner.add(m.job1, label='one')

        class MyPlug(Plug):

            @ignore()
            def helper(self):  # pragma: no cover
                m.plug_bad()

            def one(self):
                m.plug_good()

        plug = MyPlug()
        plug.add_to(runner)

        runner()

        compare([
            call.job1(),
            call.plug_good()
        ], actual=m.mock_calls)

        verify(runner,
               (m.job1, set()),
               (plug.one, {'one'}),
               )
Example #7
0
    def test_addition(self):
        m = Mock()

        def job1():
            m.job1()

        def job2():
            m.job2()

        def job3():
            m.job3()

        runner1 = Runner(job1, job2)
        runner2 = Runner(job3)
        runner = runner1 + runner2
        runner()

        verify(runner,
                    (job1, set()),
                    (job2, set()),
                    (job3, set()),
                    )
        compare([
                call.job1(),
                call.job2(),
                call.job3(),
                ], m.mock_calls)
Example #8
0
    def test_replace_for_testing(self):
        m = Mock()        
        class T1(object): pass
        class T2(object): pass

        t1 = T1()
        t2 = T2()

        def job1():
            raise Exception() # pragma: nocover

        @requires(T1)
        def job2(obj):
            raise Exception() # pragma: nocover

        @requires(T2)
        def job3(obj):
            raise Exception() # pragma: nocover

        runner = Runner(job1, job2, job3)
        runner.replace(job1, m.job1)
        m.job1.return_value = t1
        runner.replace(job2, m.job2)
        m.job2.return_value = t2
        runner.replace(job3, m.job3)
        runner()
        
        compare([
                call.job1(),
                call.job2(t1),
                call.job3(t2),
                ], m.mock_calls)
Example #9
0
    def test_explict(self):
        m = Mock()

        runner = Runner()
        runner.add(m.job1, label='one')

        class MyPlug(Plug):

            explicit = True

            def helper(self):
                m.plug_one()

            @insert()
            def one(self):
                self.helper()

        plug = MyPlug()
        plug.add_to(runner)

        runner()

        compare([
            call.job1(),
            call.plug_one()
        ], actual=m.mock_calls)

        verify(runner,
               (m.job1, set()),
               (plug.one, {'one'}),
               )
Example #10
0
    def test_extend_with_runners(self):
        m = Mock()        
        class T1(object): pass
        class T2(object): pass

        t1 = T1()
        t2 = T2()

        def job1():
            m.job1()
            return t1

        @requires(T1)
        def job2(obj):
            m.job2(obj)
            return t2

        @requires(T2)
        def job3(obj):
            m.job3(obj)

        runner1 = Runner(job1)
        runner2 = Runner(job2)
        runner3 = Runner(job3)

        runner = Runner(runner1)
        runner.extend(runner2, runner3)
        runner()
        
        compare([
                call.job1(),
                call.job2(t1),
                call.job3(t2),
                ], m.mock_calls)
Example #11
0
    def test_append(self):
        m = Mock()

        runner = Runner()
        runner.add(m.job1, label='one')

        class MyPlug(Plug):

            @append()
            def run_plug(self):
                m.do_it()

        plug = MyPlug()
        plug.add_to(runner)

        runner()

        compare([
            call.job1(),
            call.do_it()
        ], actual=m.mock_calls)

        verify(runner,
               (m.job1, {'one'}),
               (plug.run_plug, set()),
               )
Example #12
0
    def test_replace_explicit_at_end(self):
        m = Mock()
        runner = Runner(m.job1, m.job2)

        runner.replace(m.job2, m.jobnew, returns='mock')
        runner.add(m.jobnew2)
        runner()

        compare([
            call.job1(),
            call.jobnew(),
            call.jobnew2(),
        ], actual=m.mock_calls)
Example #13
0
    def test_replace_explicit_with_labels(self):
        m = Mock()

        runner = Runner(m.job0)
        runner.add_label('foo')
        runner['foo'].add(m.job1)
        runner['foo'].add(m.job2)

        runner.replace(m.job2, m.jobnew, returns='mock')

        runner()

        compare([
            call.job0(),
            call.job1(),
            call.jobnew()
        ], m.mock_calls)

        # check added_using is handled correctly
        m.reset_mock()
        runner2 = runner.clone(added_using='foo')
        runner2()

        compare([
            call.job1(),
            call.jobnew()
        ], actual=m.mock_calls)

        # check runner's label pointer is sane
        m.reset_mock()
        runner['foo'].add(m.job3)
        runner()

        compare([
            call.job0(),
            call.job1(),
            call.jobnew(),
            call.job3()
        ], actual=m.mock_calls)
Example #14
0
    def test_constructor(self):
        m = Mock()        
        def job1():
            m.job1()
        def job2():
            m.job2()
            
        runner = Runner(job1, job2)
        runner()

        compare([
                call.job1(),
                call.job2(),
                ], m.mock_calls)
Example #15
0
 def test_item_multiple(self):
     class MyDict(dict): pass
     m = Mock()
     def job1():
         m.job1()
         obj = MyDict()
         obj['the_thing'] = dict(other_thing=m.the_thing)
         return obj
     def job2(obj):
         m.job2(obj)
     runner = Runner()
     runner.add(job1)
     runner.add(job2, item(MyDict, 'the_thing', 'other_thing'))
     runner()
     compare([
             call.job1(),
             call.job2(m.the_thing),
             ], m.mock_calls)
Example #16
0
    def test_nested(self):
        class T(object):
            foo = dict(baz='bar')
        m = Mock()
        def job1():
            m.job1()
            return T()
        def job2(obj):
            m.job2(obj)
        runner = Runner()
        runner.add(job1)
        runner.add(job2, item(attr(T, 'foo'), 'baz'))
        runner()

        compare([
                call.job1(),
                call.job2('bar'),
                ], m.mock_calls)
Example #17
0
    def test_returns_type_mapping_of_none(self):
        m = Mock()        
        class T2(object): pass

        def job1():
            m.job1()
            return {T2:None}

        @requires(T2)
        def job2(obj):
            m.job2(obj)

        Runner(job1, job2)()
        
        compare([
                call.job1(),
                call.job2(None),
                ], m.mock_calls)
Example #18
0
 def test_item(self):
     class MyDict(dict): pass
     m = Mock()
     def job1():
         m.job1()
         obj = MyDict()
         obj['the_thing'] = m.the_thing
         return obj
     def job2(obj):
         m.job2(obj)
     runner = Runner()
     runner.add(job1)
     runner.add(job2, requires(item(MyDict, 'the_thing')))
     runner()
     compare([
             call.job1(),
             call.job2(m.the_thing),
             ], m.mock_calls)
Example #19
0
 def test_attr(self):
     class T(object):
         foo = 'bar'
     m = Mock()
     def job1():
         m.job1()
         return T()
     def job2(obj):
         m.job2(obj)
     runner = Runner()
     runner.add(job1)
     runner.add(job2, attr(T, 'foo'))
     runner()
     
     compare([
             call.job1(),
             call.job2('bar'),
             ], m.mock_calls)
Example #20
0
    def test_returns_type_mapping(self):
        m = Mock()        
        class T1(object): pass
        class T2(object): pass
        t = T1()

        def job1():
            m.job1()
            return {T2:t}

        @requires(T2)
        def job2(obj):
            m.job2(obj)

        Runner(job1, job2)()
        
        compare([
                call.job1(),
                call.job2(t),
                ], m.mock_calls)
Example #21
0
    def test_return_type_specified_decorator(self):
        m = Mock()
        class T1(object): pass
        class T2(object): pass
        t = T1()

        @returns(T2)
        def job1():
            m.job1()
            return t

        @requires(T2)
        def job2(obj):
            m.job2(obj)

        Runner(job1, job2)()

        compare([
                call.job1(),
                call.job2(t),
                ], m.mock_calls)
Example #22
0
    def test_constructor(self):
        m = Mock()
        def job1():
            m.job1()
        def job2():
            m.job2()

        runner = Runner(job1, job2)
        compare(job1, runner.start.obj)
        compare(job2, runner.end.obj)

        runner()

        compare([
                call.job1(),
                call.job2(),
                ], m.mock_calls)

        verify(runner,
                    (job1, set()),
                    (job2, set()))
Example #23
0
    def test_attr_multiple(self):
        class T2:
            bar = 'baz'
        class T:
            foo = T2()

        m = Mock()
        def job1():
            m.job1()
            return T()
        def job2(obj):
            m.job2(obj)
        runner = Runner()
        runner.add(job1)
        runner.add(job2, attr(T, 'foo', 'bar'))
        runner()

        compare([
                call.job1(),
                call.job2('baz'),
                ], m.mock_calls)
Example #24
0
    def test_returns_tuple(self):
        m = Mock()        
        class T1(object): pass
        class T2(object): pass

        t1 = T1()
        t2 = T2()

        def job1():
            m.job1()
            return t1, t2

        @requires(T1, T2)
        def job2(obj1, obj2):
            m.job2(obj1, obj2)

        Runner(job1, job2)()
        
        compare([
                call.job1(),
                call.job2(t1, t2),
                ], m.mock_calls)
Example #25
0
    def test_requirements_and_returns(self):
        m = Mock()

        @returns('r1')
        def job1():
            m.job1()
            return 1

        @requires('r2')
        def job3(r):
            m.job3(r)

        runner = Runner()
        runner.add(job1, label='point')
        runner.add(job3)

        class MyPlug(Plug):

            @requires('r1')
            @returns('r2')
            def point(self, r):
                m.point(r)
                return 2

        plug = MyPlug()
        plug.add_to(runner)

        runner()

        compare([
            call.job1(), call.point(1), call.job3(2),
        ], m.mock_calls)

        verify(runner,
               (job1, set()),
               (plug.point, {'point'}),
               (job3, set()),
               )
Example #26
0
    def test_add_plug(self):
        m = Mock()

        runner = Runner()
        runner.add(m.job1, label='one')

        class MyPlug(Plug):
            def one(self):
                m.plug_one()

        plug = MyPlug()
        runner.add(plug)

        runner()

        compare([
            call.job1(), call.plug_one()
        ], m.mock_calls)

        verify(runner,
               (m.job1, set()),
               (plug.one, {'one'}),
               )
Example #27
0
    def test_return_type_specified_imperative(self):
        m = Mock()
        class T1(object): pass
        class T2(object): pass
        t = T1()

        def job1():
            m.job1()
            return t

        @requires(T2)
        def job2(obj):
            m.job2(obj)

        runner = Runner()
        runner.add_returning(job1, T2)
        runner.add(job2, T2)
        runner()

        compare([
                call.job1(),
                call.job2(t),
                ], m.mock_calls)
Example #28
0
    def test_context_imperative(self):
        m = Mock()        
        class T1(object): pass
        class T2(object): pass

        t1 = T1()
        t2 = T2()

        def job1():
            m.job1()
            return t1

        def job2(obj):
            m.job2(obj)
            return t2

        def job3(t2_):
            m.job3(t2_)

        # imperative config trumps declarative
        @requires(T1)
        def job4(t2_):
            m.job4(t2_)
            
        runner = Runner()
        runner.add(job1)
        runner.add(job2, T1)
        runner.add(job3, t2_=T2)
        runner.add(job4, T2)
        runner()
        
        compare([
                call.job1(),
                call.job2(t1),
                call.job3(t2),
                call.job4(t2),
                ], m.mock_calls)