mocker.ANY

Here are the examples of the python api mocker.ANY taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

9 Examples 7

Example 1

View license
    def test_storm_readonly_single_DisconnectionError(self):
        """storm_readonly retries on DisconnectionError."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(2)
        self.mocker.replay()

        calls = []

        @storm_readonly
        def function():
            """Fail with DisconnectionError once."""
            calls.append('function')
            if len(calls) < 2:
                raise DisconnectionError
        function()
        self.assertEqual(calls, ['function'] * 2)

Example 2

View license
    def test_retryable_transaction(self):
        """retryable_transaction runs the function once."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(1)
        self.mocker.replay()

        calls = []

        @retryable_transaction()
        def function():
            """Test function that succeeds."""
            calls.append('function')
        function()
        self.assertEqual(calls, ['function'])

Example 3

View license
    def test_retryable_transaction_exception(self):
        """retryable_transaction passes through exceptions."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(1)
        self.mocker.replay()

        calls = []

        @retryable_transaction()
        def function():
            """Fail with RuntimeError."""
            calls.append('function')
            raise RuntimeError
        self.assertRaises(RuntimeError, function)
        self.assertEqual(calls, ['function'])

Example 4

View license
    def test_retryable_transaction_single_DisconnectionError(self):
        """retryable_transaction retries on DisconnectionError."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(2)
        self.mocker.replay()

        calls = []

        @retryable_transaction()
        def function():
            """Fail with DisconnectionError once."""
            calls.append('function')
            if len(calls) < 2:
                raise DisconnectionError
        function()
        self.assertEqual(calls, ['function'] * 2)

Example 5

View license
    def test_retryable_transaction_single_TransactionRollbackError(self):
        """retryable_transaction retries on TransactionRollbackError."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(2)
        self.mocker.replay()

        calls = []

        @retryable_transaction()
        def function():
            """Fail with TransactionRollbackError once."""
            calls.append('function')
            if len(calls) < 2:
                raise TransactionRollbackError
        function()
        self.assertEqual(calls, ['function'] * 2)

Example 6

View license
    def test_retryable_transaction_default_max_retries(self):
        """retryable_transaction defaults to 3 retries."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(3)
        self.mocker.replay()

        calls = []

        @retryable_transaction()
        def function():
            """Fail with TransactionRollbackError."""
            calls.append('function')
            raise TransactionRollbackError
        self.assertRaises(RetryLimitReached, function)
        self.assertEqual(calls, ['function'] * 3)

Example 7

View license
    def test_retryable_transaction_original_type(self):
        """Retry failure keeps original type as well."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(3)
        self.mocker.replay()

        calls = []

        @retryable_transaction()
        def function():
            """Fail with TransactionRollbackError."""
            calls.append('function')
            raise TransactionRollbackError
        self.assertRaises(TransactionRollbackError, function)

Example 8

View license
    def test_retryable_transaction_max_retries(self):
        """retryable_transaction fails on too many retries."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(3)
        self.mocker.replay()

        calls = []

        @retryable_transaction(max_retries=3)
        def function():
            """Fail with TransactionRollbackError."""
            calls.append('function')
            raise TransactionRollbackError("message")
        try:
            function()
        except RetryLimitReached, exc:
            # The original exception message is preserved.
            self.assertEqual(
                exc.args[0],
                "Maximum retries (3) reached. Please try again. "
                "(Original error: TransactionRollbackError: message)")
            self.assertEqual(exc.extra_info,
                             "TransactionRollbackError: message")
            self.assertEqual(str(exc),
                             "Maximum retries (3) reached. Please try again. "
                             "(Original error: TransactionRollbackError: "
                             "message)")

        else:
            self.fail("RetryLimitReached exception not raised.")
        self.assertEqual(calls, ['function'] * 3)

Example 9

View license
    def test_retryable_transaction_InternalError(self):
        """Retryable_transaction retry and log on InternalError."""
        sleep = self.mocker.replace('time.sleep')
        self.expect(sleep(mocker.ANY)).count(2)
        self.mocker.replay()

        logger = logging.getLogger('storage.server.txn')
        h = MementoHandler()
        logger.addHandler(h)

        calls = []

        @retryable_transaction()
        def function():
            """Fail with InternalError."""
            if len(calls) < 1:
                calls.append('function')
                raise InternalError('internal error')
            else:
                calls.append('function')
        function()
        logger.removeHandler(h)
        self.assertEqual(calls, ['function'] * 2)
        self.assertEqual(1, len(h.records))
        self.assertIn(
            'Got an InternalError, retrying', h.records[0].getMessage())
        self.assertEqual('internal error', h.records[0].exc_info[1].message)