当前位置: 首页>>代码示例>>Python>>正文


Python OssimDB.format_query方法代码示例

本文整理汇总了Python中OssimDB.OssimDB.format_query方法的典型用法代码示例。如果您正苦于以下问题:Python OssimDB.format_query方法的具体用法?Python OssimDB.format_query怎么用?Python OssimDB.format_query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OssimDB.OssimDB的用法示例。


在下文中一共展示了OssimDB.format_query方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: OssimDBTest

# 需要导入模块: from OssimDB import OssimDB [as 别名]
# 或者: from OssimDB.OssimDB import format_query [as 别名]

#.........这里部分代码省略.........
        mock_connection.autocommit.assert_called_with(self.__db_autocommit)
        self.assertTrue(execute_nq_rv)

    @mock.patch('OssimDB.MySQLdb', autospec=True)
    def test_execute_non_query_default_autocommit(self, mock_mysqldb):
        mock_connection = mock.create_autospec(Connection)
        mock_mysqldb.connect.return_value = mock_connection
        self.setUp()
        self.__ossim_db.connect()

        execute_nq_rv = self.__ossim_db.execute_non_query(self.__db_query)

        mock_connection.autocommit.assert_called_with(self.__db_autocommit_default)
        self.assertTrue(execute_nq_rv)

    @mock.patch('OssimDB.time', autospec=True)
    @mock.patch('OssimDB.Lock', autospec=True)
    @mock.patch('OssimDB.MySQLdb', autospec=True)
    def test_execute_non_query_retry_success(self, mock_mysqldb, mock_lock, mock_time):
        failing_attempts = 2
        total_attempts = failing_attempts + 1
        mock_lock_obj = mock.create_autospec(Lock())
        mock_connections, mock_cursors = self.prepare_multiple_failing_queries(failing_attempts)
        # Last attempt should not throw an exception
        mock_cursors.append(mock.create_autospec(Cursor))
        mock_connections.append(mock.create_autospec(Connection))
        mock_connections[total_attempts - 1].cursor.return_value = mock_cursors[total_attempts - 1]
        mock_lock.return_value = mock_lock_obj
        mock_mysqldb.connect.side_effect = mock_connections
        self.setUp()
        self.__ossim_db.connect()

        exec_non_query_rv = self.__ossim_db.execute_non_query(self.__db_query)

        self.assertTrue(mock_lock_obj.acquire.called)
        self.assert_multiple_failing_queries(mock_connections, mock_cursors, failing_attempts)
        self.assertEqual(mock_time.sleep.call_count, failing_attempts)
        # Assert last attempt (successful)
        self.assertTrue(mock_connections[total_attempts - 1].cursor.called)
        self.assertTrue(mock_cursors[total_attempts - 1].execute.called)
        self.assertTrue(mock_cursors[total_attempts - 1].close.called)
        self.assertTrue(mock_lock_obj.release.called)
        self.assertTrue(exec_non_query_rv)

    @mock.patch('OssimDB.time', autospec=True)
    @mock.patch('OssimDB.Lock', autospec=True)
    @mock.patch('OssimDB.MySQLdb', autospec=True)
    def test_execute_non_query_retry_fail(self, mock_mysqldb, mock_lock, mock_time):
        failing_attempts = 4
        mock_lock_obj = mock.create_autospec(Lock())
        mock_connections, mock_cursors = self.prepare_multiple_failing_queries(failing_attempts)
        mock_lock.return_value = mock_lock_obj
        mock_mysqldb.connect.side_effect = mock_connections
        self.setUp()
        self.__ossim_db.connect()

        exec_non_query_rv = self.__ossim_db.execute_non_query(self.__db_query)

        self.assertTrue(mock_lock_obj.acquire.called)
        self.assert_multiple_failing_queries(mock_connections, mock_cursors, failing_attempts)
        self.assertEqual(mock_time.sleep.call_count, failing_attempts - 1) # last attempt without sleep()
        self.assertTrue(mock_lock_obj.release.called)
        self.assertFalse(exec_non_query_rv)

    @mock.patch('OssimDB.escape', autospec=True)
    @mock.patch('OssimDB.conversions', autospec=True)
    def test_format_query_tuple(self, mock_conversions, mock_escape):
        query = 'dummy query with params %s and %s'
        params = ('dummy param', 1)
        mock_escape_returns = ['\'%s\'' % param for param in params]
        mock_escape.side_effect = mock_escape_returns
        expected_rv = query % tuple((escape_rv for escape_rv in mock_escape_returns))

        rv = self.__ossim_db.format_query(query, params)

        mock_escape.assert_has_calls([
            mock.call(param, mock_conversions) for param in params
        ])
        self.assertEqual(rv, expected_rv)

    @mock.patch('OssimDB.escape', autospec=True)
    @mock.patch('OssimDB.conversions', autospec=True)
    def test_format_query_dict(self, mock_conversions, mock_escape):
        query = 'dummy query with params %(one)s and %(two)s'
        params = {'two': 'param_2', 'one': 'param_1'}

        def mock_escape_side_effect(param, conversions):
            return '\'%s\'' % param

        mock_escape.side_effect = mock_escape_side_effect
        expected_rv = query % dict(
            (key, mock_escape_side_effect(value, mock_conversions)) for key, value in params.iteritems()
        )

        rv = self.__ossim_db.format_query(query, params)

        mock_escape.assert_has_calls([
            mock.call(param, mock_conversions) for _, param in params.iteritems()
        ])
        self.assertEqual(rv, expected_rv)
开发者ID:jpalanco,项目名称:alienvault-ossim,代码行数:104,代码来源:OssimDBTest.py


注:本文中的OssimDB.OssimDB.format_query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。