Example #1
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)
Example #2
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)
Example #3
0
    def test_with_unexpected_call(self):
        """Testing SpyOpMatchInOrder with unexpected call"""
        obj = MathClass()

        self.agency.spy_on(
            obj.do_math,
            op=SpyOpMatchInOrder([
                {
                    'kwargs': {
                        'a': 4,
                        'b': 7,
                    },
                },
            ]))

        expected_message = re.escape(
            "This call to do_math was not passed args=(), "
            "kwargs={'a': 4, 'b': 7}.\n"
            "\n"
            "It was called with:\n"
            "\n"
            "args=()\n"
            "kwargs={'a': 4, 'b': 9}"
        )

        with self.assertRaisesRegexp(AssertionError, expected_message):
            obj.do_math(a=4, b=9)
Example #4
0
    def test_assertSpyReturned_without_expected_return(self):
        """Testing SpyAgency.assertSpyReturned without expected return value"""
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1, b=4)
        obj.do_math(2, b=9)

        msg = ('No call to do_math returned 100.\n'
               '\n'
               'The following values have been returned:\n'
               '\n'
               'Call 0:\n'
               '  5\n'
               '\n'
               'Call 1:\n'
               '  11')

        with self._check_assertion(msg):
            self.assertSpyReturned(obj.do_math, 100)

        with self._check_assertion(msg):
            self.assertSpyReturned(obj.do_math.spy, 100)

        msg = ('This call to do_math did not return 100.\n'
               '\n'
               'It returned:\n'
               '\n'
               '5')

        with self._check_assertion(msg):
            self.assertSpyReturned(obj.do_math.calls[0], 100)
Example #5
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)
Example #6
0
    def test_assertSpyNotCalledWith_without_unexpected_arguments(self):
        """Testing SpyAgency.assertSpyNotCalledWith without unexpected
        arguments
        """
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1, b=4)
        obj.do_math(2, b=9)

        msg = ("A call to do_math was unexpectedly passed args=(), "
               "kwargs={'a': 1, 'b': 4}.\n"
               "\n"
               "The following calls were recorded:\n"
               "\n"
               "Call 0:\n"
               "  args=()\n"
               "  kwargs={'a': 1, 'b': 4}\n"
               "\n"
               "Call 1:\n"
               "  args=()\n"
               "  kwargs={'a': 2, 'b': 9}")

        with self._check_assertion(msg):
            self.assertSpyNotCalledWith(obj.do_math, a=1, b=4)

        with self._check_assertion(msg):
            self.assertSpyNotCalledWith(obj.do_math.spy, a=1, b=4)

        msg = ("This call to do_math was unexpectedly passed args=(),"
               " kwargs={'a': 2, 'b': 9}.")

        with self._check_assertion(msg):
            self.assertSpyNotCalledWith(obj.do_math.spy.calls[1], a=2, b=9)
Example #7
0
    def test_with_classmethod(self):
        """Testing SpyOpRaise with classmethod"""
        self.agency.spy_on(MathClass.class_do_math,
                           owner=MathClass,
                           op=SpyOpRaise(ValueError('foo')))

        with self.assertRaisesRegexp(ValueError, 'foo'):
            MathClass.class_do_math(5, 3)
Example #8
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')
Example #9
0
    def test_spy_on(self):
        """Testing SpyAgency mixed in with spy_on"""
        obj = MathClass()

        self.spy_on(obj.do_math)
        self.assertTrue(hasattr(obj.do_math, 'spy'))

        result = obj.do_math()
        self.assertEqual(result, 3)
Example #10
0
    def test_spy_on(self):
        """Testing SpyAgency mixed in with spy_on"""
        obj = MathClass()

        self.spy_on(obj.do_math)
        self.assertTrue(hasattr(obj.do_math, 'spy'))

        result = obj.do_math()
        self.assertEqual(result, 3)
Example #11
0
    def test_call_with_original_false(self):
        """Testing FunctionSpy calls with call_original=False"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math, call_original=False)
        result = obj.do_math()

        self.assertIsNone(result)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertTrue(obj.do_math.last_called_with(a=1, b=2))
Example #12
0
    def test_call_with_original_true_and_bound_method(self):
        """Testing FunctionSpy calls with call_original=True and bound method"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math, call_original=True)
        result = obj.do_math()

        self.assertEqual(result, 3)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertTrue(obj.do_math.last_called_with(a=1, b=2))
Example #13
0
    def test_called(self):
        """Testing FunctionSpy.called"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        self.assertFalse(obj.do_math.called)

        obj.do_math(10, 20)

        self.assertTrue(obj.do_math.called)
Example #14
0
    def test_last_called_with(self):
        """Testing FunctionSpy.last_called_with"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, 2)
        obj.do_math_mixed(3, 4)

        self.assertFalse(obj.do_math_mixed.last_called_with(1, a=2))
        self.assertTrue(obj.do_math_mixed.last_called_with(3, b=4))
Example #15
0
    def test_assertSpyCalled_with_called(self):
        """Testing SpyAgency.assertSpyCalled with spy called"""
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math()

        # These should not fail.
        self.assertSpyCalled(obj.do_math)
        self.assertSpyCalled(obj.do_math.spy)
Example #16
0
    def test_repr_and_bound_method(self):
        """Testing FunctionSpy.__repr__ and bound method"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        obj.do_math()

        self.assertEqual(repr(obj.do_math.spy),
                         '<Spy for bound method MathClass.do_math '
                         'of %r (1 call)>' % obj)
Example #17
0
    def test_with_unbound_method(self):
        """Testing SpyOpRaise with unbound method"""
        self.agency.spy_on(MathClass.do_math,
                           owner=MathClass,
                           op=SpyOpRaise(ValueError('foo')))

        obj = MathClass()

        with self.assertRaisesRegexp(ValueError, 'foo'):
            obj.do_math(a=4, b=3)
Example #18
0
    def test_call_with_fake_and_args(self):
        """Testing FunctionSpy calls with call_fake and arguments"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math_pos, call_fake=fake_do_math)
        result = obj.do_math_pos(10, 20)

        self.assertEqual(result, -10)
        self.assertEqual(len(obj.do_math_pos.calls), 1)
        self.assertEqual(obj.do_math_pos.calls[0].args, (10, 20))
        self.assertEqual(obj.do_math_pos.calls[0].kwargs, {})
Example #19
0
    def test_assertSpyCallCount_with_expected_count(self):
        """Testing SpyAgency.assertSpyCallCount with expected call count"""
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math()
        obj.do_math()

        # These should not fail.
        self.assertSpyCallCount(obj.do_math, 2)
        self.assertSpyCallCount(obj.do_math.spy, 2)
Example #20
0
    def test_assertSpyLastCalledWith_with_expected_arguments(self):
        """Testing SpyAgency.assertSpyLastCalledWith with expected arguments"""
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1, b=4)
        obj.do_math(2, b=9)

        # These should not fail.
        self.assertSpyLastCalledWith(obj.do_math, a=2, b=9)
        self.assertSpyLastCalledWith(obj.do_math.spy, a=2, b=9)
Example #21
0
    def test_last_called_with_and_keyword_args(self):
        """Testing FunctionSpy.last_called_with and keyword arguments"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(a=1, b=2)
        obj.do_math_mixed(a=3, b=4)

        self.assertTrue(obj.do_math_mixed.last_called_with(3, b=4))
        self.assertFalse(obj.do_math_mixed.last_called_with(1, b=2))
        self.assertFalse(obj.do_math_mixed.last_called_with(1, b=2, c=3))
Example #22
0
    def test_raised(self):
        """Testing FunctionSpy.raised"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        with self.assertRaises(TypeError):
            obj.do_math(1, 'a')

        self.assertTrue(obj.do_math.raised(TypeError))
        self.assertFalse(obj.do_math.raised(ValueError))
        self.assertFalse(obj.do_math.raised(None))
Example #23
0
    def test_spy_on(self):
        """Testing spy_on context manager"""
        obj = MathClass()

        with spy_on(obj.do_math):
            self.assertTrue(hasattr(obj.do_math, 'spy'))

            result = obj.do_math()
            self.assertEqual(result, 3)

        self.assertFalse(hasattr(obj.do_math, 'spy'))
Example #24
0
    def test_last_returned(self):
        """Testing FunctionSpy.last_returned"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        obj.do_math(1, 2)
        obj.do_math(3, 4)

        self.assertFalse(obj.do_math.last_returned(3))
        self.assertTrue(obj.do_math.last_returned(7))
        self.assertFalse(obj.do_math.last_returned(None))
Example #25
0
    def test_spy_on(self):
        """Testing spy_on context manager"""
        obj = MathClass()

        with spy_on(obj.do_math):
            self.assertTrue(hasattr(obj.do_math, 'spy'))

            result = obj.do_math()
            self.assertEqual(result, 3)

        self.assertFalse(hasattr(obj.do_math, 'spy'))
Example #26
0
    def test_assertSpyLastReturned_with_expected_return(self):
        """Testing SpyAgency.assertSpyLastReturned with expected return value
        """
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1, b=4)
        obj.do_math(2, b=9)

        # These should not fail.
        self.assertSpyLastReturned(obj.do_math, 11)
        self.assertSpyLastReturned(obj.do_math.spy, 11)
Example #27
0
    def test_raised(self):
        """Testing SpyCall.raised"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        with self.assertRaises(TypeError):
            obj.do_math(1, 'a')

        call = obj.do_math.calls[0]
        self.assertTrue(call.raised(TypeError))
        self.assertFalse(call.raised(ValueError))
        self.assertFalse(call.raised(None))
Example #28
0
    def test_returned(self):
        """Testing SpyCall.returned"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, 2)
        obj.do_math_mixed(3, 4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.returned(3))
        self.assertFalse(call.returned(7))
        self.assertFalse(call.returned(None))
Example #29
0
    def test_called_with_and_partial_args(self):
        """Testing SpyCall.called_with and partial arguments"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, 2)
        obj.do_math_mixed(3, 4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.called_with(1))
        self.assertFalse(call.called_with(1, 2, 3))
        self.assertFalse(call.called_with(3))
Example #30
0
    def test_expose_spy(self):
        """Testing spy_on exposes `spy` via context manager"""
        obj = MathClass()

        with spy_on(obj.do_math) as spy:
            self.assertTrue(hasattr(obj.do_math, 'spy'))
            self.assertIs(obj.do_math.spy, spy)

            result = obj.do_math()
            self.assertEqual(result, 3)

        self.assertFalse(hasattr(obj.do_math, 'spy'))
Example #31
0
    def test_last_called_with_and_partial_args(self):
        """Testing FunctionSpy.called_with and partial arguments"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, 2)
        obj.do_math_mixed(3, 4)

        self.assertTrue(obj.do_math_mixed.last_called_with(3))
        self.assertTrue(obj.do_math_mixed.last_called_with(3, b=4))
        self.assertFalse(obj.do_math_mixed.last_called_with(3, b=4, c=5))
        self.assertFalse(obj.do_math_mixed.last_called_with(1, b=2))
Example #32
0
    def test_raised(self):
        """Testing SpyCall.raised"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        with self.assertRaises(TypeError):
            obj.do_math(1, 'a')

        call = obj.do_math.calls[0]
        self.assertTrue(call.raised(TypeError))
        self.assertFalse(call.raised(ValueError))
        self.assertFalse(call.raised(None))
Example #33
0
    def test_call_with_fake_and_unbound_method(self):
        """Testing FunctionSpy calls with call_fake and unbound method"""
        self.agency.spy_on(MathClass.do_math, call_fake=fake_do_math)

        obj = MathClass()
        result = obj.do_math()

        self.assertEqual(result, -1)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertTrue(obj.do_math.last_called_with(
            a=1,
            b=2))
Example #34
0
    def test_expose_spy(self):
        """Testing spy_on exposes `spy` via context manager"""
        obj = MathClass()

        with spy_on(obj.do_math) as spy:
            self.assertTrue(hasattr(obj.do_math, 'spy'))
            self.assertIs(obj.do_math.spy, spy)

            result = obj.do_math()
            self.assertEqual(result, 3)

        self.assertFalse(hasattr(obj.do_math, 'spy'))
Example #35
0
    def test_call_with_original_true_and_unbound_method_args_for_kwargs(self):
        """Testing FunctionSpy calls with call_original=True and unbound
        method with all positional arguments in place of keyword arguments
        """
        self.agency.spy_on(MathClass.do_math, call_original=True)

        obj = MathClass()
        result = obj.do_math(10, 20)

        self.assertEqual(result, 30)
        self.assertEqual(len(MathClass.do_math.calls), 1)
        self.assertTrue(MathClass.do_math.last_called_with(a=10, b=20))
Example #36
0
    def test_called_with_and_partial_args(self):
        """Testing SpyCall.called_with and partial arguments"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, 2)
        obj.do_math_mixed(3, 4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.called_with(1))
        self.assertFalse(call.called_with(1, 2, 3))
        self.assertFalse(call.called_with(3))
Example #37
0
    def test_returned(self):
        """Testing SpyCall.returned"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, 2)
        obj.do_math_mixed(3, 4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.returned(3))
        self.assertFalse(call.returned(7))
        self.assertFalse(call.returned(None))
Example #38
0
    def test_call_with_all_original_false_and_args(self):
        """Testing FunctionSpy calls with call_original=False and positional
        arguments
        """
        obj = MathClass()

        self.agency.spy_on(obj.do_math_pos, call_original=False)
        result = obj.do_math_pos(10, 20)

        self.assertIsNone(result)
        self.assertEqual(len(obj.do_math_pos.calls), 1)
        self.assertEqual(obj.do_math_pos.calls[0].args, (10, 20))
        self.assertEqual(obj.do_math_pos.calls[0].kwargs, {})
Example #39
0
    def test_last_call(self):
        """Testing FunctionSpy.last_call"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        obj.do_math(10, 20)
        obj.do_math(20, 30)

        self.assertEqual(len(obj.do_math.calls), 2)

        last_call = obj.do_math.last_call
        self.assertNotEqual(last_call, None)
        self.assertTrue(last_call.called_with(a=20, b=30))
Example #40
0
    def test_called_with(self):
        """Testing SpyCall.called_with"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, b=2)
        obj.do_math_mixed(3, b=4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.called_with(1, b=2))
        self.assertTrue(call.called_with(a=1, b=2))
        self.assertFalse(call.called_with(3, b=4))
        self.assertFalse(call.called_with(1, 2))
Example #41
0
    def test_last_raised_with_message(self):
        """Testing FunctionSpy.last_raised_with_message"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        with self.assertRaises(TypeError):
            obj.do_math(1, 'a')

        self.assertTrue(obj.do_math.last_raised_with_message(
            TypeError,
            "unsupported operand type(s) for +: 'int' and '%s'"
            % text_type.__name__))
        self.assertFalse(obj.do_math.last_raised_with_message(TypeError, None))
Example #42
0
    def test_assertSpyLastRaised_with_expected_no_exception(self):
        """Testing SpyAgency.assertSpyLastRaised with expected completion
        without raising
        """
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1)
        obj.do_math(2)

        # These should not fail.
        self.assertSpyLastRaised(obj.do_math, None)
        self.assertSpyLastRaised(obj.do_math.spy, None)
Example #43
0
    def test_called_with(self):
        """Testing SpyCall.called_with"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(1, b=2)
        obj.do_math_mixed(3, b=4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.called_with(1, b=2))
        self.assertTrue(call.called_with(a=1, b=2))
        self.assertFalse(call.called_with(3, b=4))
        self.assertFalse(call.called_with(1, 2))
Example #44
0
    def test_called_with_and_keyword_args(self):
        """Testing SpyCall.called_with and keyword arguments"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(a=1, b=2)
        obj.do_math_mixed(a=3, b=4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.called_with(1, b=2))
        self.assertTrue(call.called_with(a=1, b=2))
        self.assertFalse(call.called_with(1, 2))
        self.assertFalse(call.called_with(3, b=4))
Example #45
0
    def test_call_with_original_true_and_bound_method_args(self):
        """Testing FunctionSpy calls with call_original=True and bound method
        with all positional arguments
        """
        obj = MathClass()

        self.agency.spy_on(obj.do_math_pos, call_original=True)
        result = obj.do_math_pos(10, 20)

        self.assertEqual(result, 30)
        self.assertEqual(len(obj.do_math_pos.calls), 1)
        self.assertEqual(obj.do_math_pos.calls[0].args, (10, 20))
        self.assertEqual(len(obj.do_math_pos.calls[0].kwargs), 0)
Example #46
0
    def test_called_with_and_keyword_args(self):
        """Testing SpyCall.called_with and keyword arguments"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math_mixed)

        obj.do_math_mixed(a=1, b=2)
        obj.do_math_mixed(a=3, b=4)

        call = obj.do_math_mixed.calls[0]
        self.assertTrue(call.called_with(1, b=2))
        self.assertTrue(call.called_with(a=1, b=2))
        self.assertFalse(call.called_with(1, 2))
        self.assertFalse(call.called_with(3, b=4))
Example #47
0
    def test_reset_calls(self):
        """Testing FunctionSpy.reset_calls"""
        obj = MathClass()
        self.agency.spy_on(obj.do_math)

        obj.do_math(1, 2)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertEqual(obj.do_math.last_call, obj.do_math.calls[-1])
        self.assertTrue(obj.do_math.called)

        obj.do_math.reset_calls()
        self.assertEqual(len(obj.do_math.calls), 0)
        self.assertIsNone(obj.do_math.last_call)
        self.assertFalse(obj.do_math.called)
Example #48
0
    def test_call_with_original_false_and_kwargs(self):
        """Testing FunctionSpy calls with call_original=False and keyword arguments"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math, call_original=False)
        result = obj.do_math(a=10, b=20)

        self.assertEqual(result, None)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertEqual(len(obj.do_math.calls[0].args), 0)
        self.assertEqual(obj.do_math.calls[0].kwargs, {
            'a': 10,
            'b': 20
        })
Example #49
0
    def test_assertSpyNotCalledWith_with_unexpected_arguments(self):
        """Testing SpyAgency.assertSpyNotCalledWith with unexpected arguments
        """
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1, b=4)
        obj.do_math(2, b=9)

        # These should not fail.
        self.assertSpyNotCalledWith(obj.do_math, a=1, b=3)
        self.assertSpyNotCalledWith(obj.do_math.calls[0], a=1, b=3)
        self.assertSpyNotCalledWith(obj.do_math.spy, a=1, b=9)
        self.assertSpyNotCalledWith(obj.do_math.spy.calls[1], a=1, b=9)
Example #50
0
    def test_setup_with_instance(self):
        """Testing SpyOpMatchInOrder set up with op=SpyOpMatchInOrder([...])"""
        obj = MathClass()

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

        self.assertEqual(obj.do_math(a=1, b=2), 3)
Example #51
0
    def test_with_unbound_method(self):
        """Testing SpyOpMatchAny with unbound method"""
        self.agency.spy_on(MathClass.do_math,
                           owner=MathClass,
                           op=SpyOpMatchAny([
                               {
                                   'kwargs': {
                                       'a': 4,
                                       'b': 3,
                                   },
                               },
                           ]))

        obj = MathClass()
        self.assertEqual(obj.do_math(a=4, b=3), 7)
Example #52
0
    def test_call_with_fake_and_kwargs(self):
        """Testing FunctionSpy calls with call_fake and keyword arguments"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math, call_fake=fake_do_math)
        result = obj.do_math(a=10, b=20)

        print(obj.do_math.calls)
        self.assertEqual(result, -10)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertEqual(len(obj.do_math.calls[0].args), 0)
        self.assertEqual(obj.do_math.calls[0].kwargs, {
            'a': 10,
            'b': 20,
        })
Example #53
0
    def test_assertSpyLastRaised_without_raised(self):
        """Testing SpyAgency.assertSpyLastRaised without exception raised"""
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1)
        obj.do_math(2)

        msg = 'The last call to do_math did not raise an exception.'

        with self._check_assertion(msg):
            self.assertSpyLastRaised(obj.do_math, KeyError)

        with self._check_assertion(msg):
            self.assertSpyLastRaised(obj.do_math.spy, KeyError)
Example #54
0
    def test_call_with_original_true_and_bound_method_kwargs(self):
        """Testing FunctionSpy calls with call_original=True and bound method
        with all keyword arguments
        """
        obj = MathClass()

        self.agency.spy_on(obj.do_math, call_original=True)
        result = obj.do_math(a=10, b=20)

        self.assertEqual(result, 30)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertEqual(len(obj.do_math.calls[0].args), 0)
        self.assertEqual(obj.do_math.calls[0].kwargs, {
            'a': 10,
            'b': 20
        })
Example #55
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')
Example #56
0
    def test_call_with_all_original_false_and_args_for_kwargs(self):
        """Testing FunctionSpy calls with call_original=False and positional
        arguments in place of keyword arguments
        """
        obj = MathClass()

        self.agency.spy_on(obj.do_math, call_original=False)
        result = obj.do_math(10, 20)

        self.assertEqual(result, None)
        self.assertEqual(len(obj.do_math.calls), 1)
        self.assertEqual(obj.do_math.calls[0].args, ())
        self.assertEqual(obj.do_math.calls[0].kwargs, {
            'a': 10,
            'b': 20,
        })
Example #57
0
    def test_setup_with_instance(self):
        """Testing SpyOpMatchAny set up with op=SpyOpMatchAny([...])"""
        obj = MathClass()

        self.agency.spy_on(obj.do_math,
                           op=SpyOpMatchAny([
                               {
                                   'kwargs': {
                                       'a': 1,
                                       'b': 2,
                                   },
                                   'call_fake': lambda a, b: a - b,
                               },
                           ]))

        self.assertEqual(obj.do_math(a=1, b=2), -1)
Example #58
0
    def test_assertSpyRaisedMessage_without_raised(self):
        """Testing SpyAgency.assertSpyRaisedMessage without exception raised
        """
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math(1)
        obj.do_math(2)

        msg = 'No call to do_math raised an exception.'

        with self._check_assertion(msg):
            self.assertSpyRaisedMessage(obj.do_math, KeyError, '...')

        with self._check_assertion(msg):
            self.assertSpyRaisedMessage(obj.do_math.spy, KeyError, '...')
Example #59
0
def test_pytest_plugin(spy_agency):
    """Testing pytest spy_agency fixture"""
    assert isinstance(spy_agency, SpyAgency)

    obj = MathClass()

    spy = spy_agency.spy_on(obj.do_math)
    assert spy_agency.spies == {spy}
Example #60
0
    def test_call_with_original_true_and_classmethod(self):
        """Testing FunctionSpy calls with call_original=True and classmethod"""
        self.agency.spy_on(MathClass.class_do_math, call_original=True)
        result = MathClass.class_do_math()

        self.assertEqual(result, 10)
        self.assertEqual(len(MathClass.class_do_math.calls), 1)
        self.assertTrue(MathClass.class_do_math.last_called_with(a=2, b=5))