示例#1
0
    def test_setup_with_instance_and_op(self):
        """Testing SpyOpMatchAny set up with op=SpyOpMatchAny([...]) and op"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math,
                           op=SpyOpMatchAny([
                               {
                                   'kwargs': {
                                       'a': 1,
                                       'b': 2,
                                   },
                                   'op':
                                   SpyOpMatchInOrder([
                                       {
                                           'kwargs': {
                                               'a': 1,
                                               'b': 2,
                                               'x': 1,
                                           },
                                           'op': SpyOpReturn(123),
                                       },
                                       {
                                           'kwargs': {
                                               'a': 1,
                                               'b': 2,
                                               'x': 2,
                                           },
                                           'op': SpyOpReturn(456),
                                       },
                                   ]),
                               },
                           ]))

        self.assertEqual(obj.do_math(a=1, b=2, x=1), 123)
        self.assertEqual(obj.do_math(a=1, b=2, x=2), 456)
示例#2
0
    def test_with_function_and_op(self):
        """Testing SpyOpMatchAny with function and op"""
        def do_math(a, b, x=0):
            return a + b

        self.agency.spy_on(do_math,
                           op=SpyOpMatchAny([
                               {
                                   'args': [5, 3],
                                   'op':
                                   SpyOpMatchInOrder([
                                       {
                                           'args': [5, 3],
                                           'kwargs': {
                                               'x': 1,
                                           },
                                           'op': SpyOpReturn(123),
                                       },
                                       {
                                           'args': [5, 3],
                                           'kwargs': {
                                               'x': 2,
                                           },
                                           'op': SpyOpReturn(456),
                                       },
                                   ]),
                               },
                           ]))

        self.assertEqual(do_math(a=5, b=3, x=1), 123)
        self.assertEqual(do_math(a=5, b=3, x=2), 456)
示例#3
0
    def test_with_unbound_method_and_op(self):
        """Testing SpyOpMatchAny with unbound method and op"""
        self.agency.spy_on(MathClass.do_math,
                           owner=MathClass,
                           op=SpyOpMatchAny([
                               {
                                   'kwargs': {
                                       'a': 4,
                                       'b': 3,
                                   },
                                   'op':
                                   SpyOpMatchInOrder([
                                       {
                                           'kwargs': {
                                               'a': 4,
                                               'b': 3,
                                               'x': 1,
                                           },
                                           'op': SpyOpReturn(123),
                                       },
                                       {
                                           'kwargs': {
                                               'a': 4,
                                               'b': 3,
                                               'x': 2,
                                           },
                                           'op': SpyOpReturn(456),
                                       },
                                   ]),
                               },
                           ]))

        obj = MathClass()
        self.assertEqual(obj.do_math(a=4, b=3, x=1), 123)
        self.assertEqual(obj.do_math(a=4, b=3, x=2), 456)
示例#4
0
    def test_with_classmethod_and_op(self):
        """Testing SpyOpMatchAny with classmethod and op"""
        self.agency.spy_on(MathClass.class_do_math,
                           owner=MathClass,
                           op=SpyOpMatchAny([
                               {
                                   'kwargs': {
                                       'a': 5,
                                       'b': 3,
                                   },
                                   'op':
                                   SpyOpMatchInOrder([
                                       {
                                           'kwargs': {
                                               'a': 5,
                                               'b': 3,
                                               'x': 1,
                                           },
                                           'op': SpyOpReturn(123),
                                       },
                                       {
                                           'kwargs': {
                                               'a': 5,
                                               'b': 3,
                                               'x': 2,
                                           },
                                           'op': SpyOpReturn(456),
                                       },
                                   ]),
                               },
                           ]))

        self.assertEqual(MathClass.class_do_math(a=5, b=3, x=1), 123)
        self.assertEqual(MathClass.class_do_math(a=5, b=3, x=2), 456)
示例#5
0
    def test_with_classmethod(self):
        """Testing SpyOpReturn with classmethod"""
        self.agency.spy_on(MathClass.class_do_math,
                           owner=MathClass,
                           op=SpyOpReturn('abc123'))

        self.assertEqual(MathClass.class_do_math(5, 3), 'abc123')
示例#6
0
    def test_with_function(self):
        """Testing SpyOpReturn with function"""
        def do_math(a, b):
            return a + b

        self.agency.spy_on(do_math, op=SpyOpReturn('abc123'))

        self.assertEqual(do_math(5, 3), 'abc123')
示例#7
0
    def test_with_unbound_method(self):
        """Testing SpyOpReturn with unbound method"""
        self.agency.spy_on(MathClass.do_math,
                           owner=MathClass,
                           op=SpyOpReturn('abc123'))

        obj = MathClass()

        self.assertEqual(obj.do_math(a=4, b=3), 'abc123')
示例#8
0
    def test_with_function_and_op(self):
        """Testing SpyOpMatchInOrder with function and op"""
        def do_math(a, b):
            return a + b

        self.agency.spy_on(do_math,
                           op=SpyOpMatchInOrder([
                               {
                                   'args': [5, 3],
                                   'op': SpyOpReturn(123),
                               },
                           ]))

        self.assertEqual(do_math(5, 3), 123)
示例#9
0
    def test_with_expected_calls(self):
        """Testing SpyOpMatchInOrder with all expected calls"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math,
                           op=SpyOpMatchInOrder([
                               {
                                   'kwargs': {
                                       'a': 4,
                                       'b': 7,
                                   },
                               },
                               {
                                   'kwargs': {
                                       'a': 2,
                                       'b': 8,
                                   },
                                   'call_original': False,
                               },
                               {
                                   'kwargs': {
                                       'a': 100,
                                       'b': 200,
                                   },
                                   'op': SpyOpReturn(123),
                               },
                               {
                                   'kwargs': {
                                       'a': 5,
                                       'b': 9,
                                   },
                                   'call_fake': lambda a, b: a + b + 10,
                               },
                               {
                                   'call_fake': lambda a, b: 1001,
                               },
                           ]))

        values = [
            obj.do_math(4, 7),
            obj.do_math(a=2, b=8),
            obj.do_math(a=100, b=200),
            obj.do_math(5, b=9),
            obj.do_math(a=1, b=1),
        ]

        self.assertEqual(values, [11, None, 123, 24, 1001])