Exemplo n.º 1
0
    def test_assertSpyLastRaised_with_expected_exception(self):
        """Testing SpyAgency.assertSpyLastRaised with expected exception
        raised
        """
        def _do_math(_self, a, *args, **kwargs):
            if a == 1:
                raise KeyError
            elif a == 2:
                raise ValueError

        obj = MathClass()
        self.spy_on(obj.do_math, call_fake=_do_math)

        try:
            obj.do_math(1)
        except KeyError:
            pass

        try:
            obj.do_math(2)
        except ValueError:
            pass

        # These should not fail.
        self.assertSpyLastRaised(obj.do_math, ValueError)
        self.assertSpyLastRaised(obj.do_math.spy, ValueError)
Exemplo n.º 2
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)
Exemplo n.º 3
0
    def test_assertSpyLastRaised_without_expected_exception(self):
        """Testing SpyAgency.assertSpyLastRaised without expected exception
        raised
        """
        def _do_math(_self, a, *args, **kwargs):
            if a == 1:
                raise KeyError
            elif a == 2:
                raise ValueError

        obj = MathClass()
        self.spy_on(obj.do_math, call_fake=_do_math)

        try:
            obj.do_math(1)
        except KeyError:
            pass

        try:
            obj.do_math(2)
        except ValueError:
            pass

        msg = ('The last call to do_math did not raise KeyError. It last '
               'raised ValueError.')

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

        with self._check_assertion(msg):
            self.assertSpyLastRaised(obj.do_math.spy, KeyError)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def test_assertSpyLastRaisedMessage_with_expected(self):
        """Testing SpyAgency.assertSpyLastRaised with expected exception and
        message raised
        """
        def _do_math(_self, a, *args, **kwargs):
            if a == 1:
                raise AttributeError('Bad key!')
            elif a == 2:
                raise ValueError('Bad value!')

        obj = MathClass()
        self.spy_on(obj.do_math, call_fake=_do_math)

        try:
            obj.do_math(1)
        except AttributeError:
            pass

        try:
            obj.do_math(2)
        except ValueError:
            pass

        # These should not fail.
        self.assertSpyLastRaisedMessage(obj.do_math, ValueError, 'Bad value!')
        self.assertSpyLastRaisedMessage(obj.do_math.spy, ValueError,
                                        'Bad value!')
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def test_assertSpyRaisedMessage_without_expected(self):
        """Testing SpyAgency.assertSpyRaisedMessage without expected exception
        and message raised
        """
        def _do_math(_self, a, *args, **kwargs):
            if a == 1:
                raise AttributeError('Bad key!')
            elif a == 2:
                raise ValueError('Bad value!')

        obj = MathClass()
        self.spy_on(obj.do_math, call_fake=_do_math)

        try:
            obj.do_math(1)
        except AttributeError:
            pass

        try:
            obj.do_math(2)
        except ValueError:
            pass

        # Note that we may end up with different string types with different
        # prefixes on different versions of Python, so we need to repr these.
        msg = ('No call to do_math raised AttributeError with message %r.\n'
               '\n'
               'The following exceptions have been raised:\n'
               '\n'
               'Call 0:\n'
               '  exception=AttributeError\n'
               '  message=%r\n'
               '\n'
               'Call 1:\n'
               '  exception=ValueError\n'
               '  message=%r' %
               ('Bad key...', str('Bad key!'), str('Bad value!')))

        with self._check_assertion(msg):
            self.assertSpyRaisedMessage(obj.do_math, AttributeError,
                                        'Bad key...')

        with self._check_assertion(msg):
            self.assertSpyRaisedMessage(obj.do_math.spy, AttributeError,
                                        'Bad key...')

        msg = ('This call to do_math did not raise AttributeError with message'
               ' %r.\n'
               '\n'
               'It raised:\n'
               '\n'
               'exception=AttributeError\n'
               'message=%r' % ('Bad key...', str('Bad key!')))

        with self._check_assertion(msg):
            self.assertSpyRaisedMessage(obj.do_math.calls[0], AttributeError,
                                        'Bad key...')
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
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)
Exemplo n.º 11
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)
Exemplo n.º 12
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))
Exemplo n.º 13
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)
Exemplo n.º 14
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))
Exemplo n.º 15
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)
Exemplo n.º 16
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))
Exemplo n.º 17
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))
Exemplo n.º 18
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)
Exemplo n.º 19
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))
Exemplo n.º 20
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))
Exemplo n.º 21
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)
Exemplo n.º 22
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)
Exemplo n.º 23
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)
Exemplo n.º 24
0
    def test_assertSpyCallCount_without_expected_count(self):
        """Testing SpyAgency.assertSpyCallCount without expected call count"""
        obj = MathClass()
        self.spy_on(obj.do_math)

        obj.do_math()

        with self._check_assertion('do_math was called 1 time, not 2.'):
            self.assertSpyCallCount(obj.do_math, 2)

        # Let's bump and test a plural result.
        obj.do_math()

        with self._check_assertion('do_math was called 2 times, not 3.'):
            self.assertSpyCallCount(obj.do_math.spy, 3)
Exemplo n.º 25
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)
Exemplo n.º 26
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, '...')
Exemplo n.º 27
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)
Exemplo n.º 28
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)
Exemplo n.º 29
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])
Exemplo n.º 30
0
    def test_assertSpyCalledWith_with_expected_arguments(self):
        """Testing SpyAgency.assertSpyCalledWith 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.assertSpyCalledWith(obj.do_math, a=1, b=4)
        self.assertSpyCalledWith(obj.do_math.calls[0], a=1, b=4)
        self.assertSpyCalledWith(obj.do_math.spy, a=2, b=9)
        self.assertSpyCalledWith(obj.do_math.spy.calls[1], a=2, b=9)

        # Check the aliases.
        self.assert_spy_called_with(obj.do_math, a=1, b=4)
        kgb.asserts.assert_spy_called_with(obj.do_math, a=1, b=4)
Exemplo n.º 31
0
    def test_assertSpyReturned_with_expected_return(self):
        """Testing SpyAgency.assertSpyReturned 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.assertSpyReturned(obj.do_math, 5)
        self.assertSpyReturned(obj.do_math.calls[0], 5)
        self.assertSpyReturned(obj.do_math.spy, 11)
        self.assertSpyReturned(obj.do_math.spy.calls[1], 11)

        # Check the aliases.
        self.assert_spy_returned(obj.do_math, 5)
        kgb.asserts.assert_spy_returned(obj.do_math, 5)
Exemplo n.º 32
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')
Exemplo n.º 33
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)
Exemplo n.º 34
0
    def test_raised_with_message(self):
        """Testing SpyCall.raised_with_message"""
        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_with_message(
            TypeError,
            "unsupported operand type(s) for +: 'int' and '%s'"
            % text_type.__name__))
        self.assertFalse(call.raised_with_message(
            ValueError,
            "unsupported operand type(s) for +: 'int' and '%s'"
            % text_type.__name__))
        self.assertFalse(call.raised_with_message(TypeError, None))
Exemplo n.º 35
0
    def test_raised_with_message(self):
        """Testing SpyCall.raised_with_message"""
        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_with_message(
            TypeError,
            "unsupported operand type(s) for +: 'int' and '%s'"
            % text_type.__name__))
        self.assertFalse(call.raised_with_message(
            ValueError,
            "unsupported operand type(s) for +: 'int' and '%s'"
            % text_type.__name__))
        self.assertFalse(call.raised_with_message(TypeError, None))
Exemplo n.º 36
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)
Exemplo n.º 37
0
    def test_assertSpyRaised_without_expected_exception(self):
        """Testing SpyAgency.assertSpyRaised without expected exception raised
        """
        def _do_math(_self, a, *args, **kwargs):
            if a == 1:
                raise KeyError
            elif a == 2:
                raise ValueError

        obj = MathClass()
        self.spy_on(obj.do_math, call_fake=_do_math)

        # First test without any exceptions raised
        try:
            obj.do_math(1)
        except KeyError:
            pass

        try:
            obj.do_math(2)
        except ValueError:
            pass

        msg = ('No call to do_math raised AttributeError.\n'
               '\n'
               'The following exceptions have been raised:\n'
               '\n'
               'Call 0:\n'
               '  KeyError\n'
               '\n'
               'Call 1:\n'
               '  ValueError')

        with self._check_assertion(msg):
            self.assertSpyRaised(obj.do_math, AttributeError)

        with self._check_assertion(msg):
            self.assertSpyRaised(obj.do_math.spy, AttributeError)

        msg = ('This call to do_math did not raise AttributeError. It raised '
               'KeyError.')

        with self._check_assertion(msg):
            self.assertSpyRaised(obj.do_math.calls[0], AttributeError)
Exemplo n.º 38
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))
Exemplo n.º 39
0
    def test_with_unexpected_call(self):
        """Testing SpyOpMatchAny with unexpected call"""
        obj = MathClass()

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

        expected_message = re.escape(
            'do_math was not called with any expected arguments.')

        with self.assertRaisesRegexp(AssertionError, expected_message):
            obj.do_math(a=4, b=9)
Exemplo n.º 40
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))
Exemplo n.º 41
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'))
Exemplo n.º 42
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'))
Exemplo n.º 43
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'))
Exemplo n.º 44
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'))
Exemplo n.º 45
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))
Exemplo n.º 46
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))
Exemplo n.º 47
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
        })
Exemplo n.º 48
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,
        })
Exemplo n.º 49
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
        })
Exemplo n.º 50
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,
        })