Пример #1
0
    def test_parse_connection_info(self, __init__mock):
        __init__mock.return_value = None
        parser = MysqlSlowQueriesParser('')

        private_user, unprivate_user, host, ip = 'test', 'test2', 'localhost', '127.0.0.1'
        line = '# User@Host: {0}[{1}] @ {2} [{3}]'.format(private_user, unprivate_user, host, ip)
        result = parser._parse_connection_info(line)
        self.assertEqual(result, (private_user, unprivate_user, host, ip))
Пример #2
0
    def test_parse_time(self, __init__mock):
        __init__mock.return_value = None
        parser = MysqlSlowQueriesParser('')

        time_str = '150825  3:31:06'
        line = '# Time: {}'.format(time_str)
        result = parser._parse_time(line)
        self.assertEqual(result, datetime.datetime.strptime(time_str, "%y%m%d %H:%M:%S"))
Пример #3
0
    def test_parse_statistics(self, __init__mock):
        __init__mock.return_value = None
        parser = MysqlSlowQueriesParser('')

        query_time, lock_time, rows_sent, rows_examined = '30.3', '1.2', '3', '131758'
        line = '# Query_time: {0}  Lock_time: {1} Rows_sent: {2}  Rows_examined: {3}'.\
            format(query_time, lock_time, rows_sent, rows_examined)
        result = parser._parse_statistics(line)
        self.assertEqual(result, (query_time, lock_time, rows_sent, rows_examined))
Пример #4
0
    def test_parse_queries(self, __init__mock, _get_next_line_mock):
        __init__mock.return_value = None
        _get_next_line_mock.return_value = '# Time: blablabla'
        parser = MysqlSlowQueriesParser('')

        line = 'use test; set timestamp=123; select blablabla;'
        result = parser._parse_queries(line)
        self.assertEqual(parser._cached_line, _get_next_line_mock.return_value)
        self.assertTrue(_get_next_line_mock.called)
        self.assertEqual(result, [line, ])
Пример #5
0
 def test_parse_headers(self, __init__mock, _get_next_line_mock):
     __init__mock.return_value = None
     line = '/usr/sbin/mysqld, Version: 5.5.40-0ubuntu0.14.04.1-log ((Ubuntu)). started with:'
     side_effects = [
         'Tcp port: 3306  Unix socket: /var/run/mysqld/mysqld.sock',
         'Time                 Id Command    Argument',
     ]
     _get_next_line_mock.side_effect = side_effects
     parser = MysqlSlowQueriesParser('')
     result = parser._parse_headers(line)
     self.assertEqual(result, side_effects[-1])
     self.assertEqual(_get_next_line_mock.call_count, 2)
Пример #6
0
    def test_parse_entry(self, __init__mock, _get_next_line_mock, _parse_time_mock,
                         _parse_connection_info_mock, _parse_statistics_mock, _parse_queries_mock):
        __init__mock.return_value = None
        _get_next_line_mock.return_value = None
        parser = MysqlSlowQueriesParser('')
        parser._cached_line = None
        self.assertIsNone(parser._parse_entry())

        _get_next_line_mock.reset_mock()
        private_user, unprivate_user, host, ip = 'test', 'test2', 'localhost', '127.0.0.1'
        query_time, lock_time, rows_sent, rows_examined = '30', '1', '3', '131758'
        queries = 'use test; set timestamp=123; select blablabla;'
        get_next_line_side_effect = [
            '# User@Host: {0}[{1}] @ {2} {3}'.format(private_user, unprivate_user, host, ip),
            '# Query_time: {0}  Lock_time: {1} Rows_sent: {2}  Rows_examined: {3}'.
                format(query_time, lock_time, rows_sent, rows_examined),
            queries,
        ]
        _get_next_line_mock.side_effect = get_next_line_side_effect

        timestamp = '150825  3:31:06'
        parser._cached_line = '# Time: {}'.format(timestamp)
        _parse_time_mock.return_value = datetime.datetime.strptime(timestamp, "%y%m%d %H:%M:%S")
        _parse_connection_info_mock.return_value = (private_user, unprivate_user, host, ip)
        _parse_statistics_mock.return_value = (query_time, lock_time, rows_sent, rows_examined)
        _parse_queries_mock.return_value = queries

        entry = parser._parse_entry()
        self.assertTrue(__init__mock.called)
        self.assertEqual(_get_next_line_mock.call_count, 3)
        _parse_time_mock.assert_called_with('# Time: {}'.format(timestamp))
        _parse_connection_info_mock.assert_called_with(get_next_line_side_effect[0])
        _parse_statistics_mock.assert_called_with(get_next_line_side_effect[1])
        _parse_queries_mock.assert_called_with(get_next_line_side_effect[2])
        self.assertEqual(entry['start_time'], _parse_time_mock.return_value)
        self.assertEqual(entry['user'], private_user)
        self.assertEqual(entry['host'], host)
        self.assertEqual(entry['query_time'], float(query_time))
        self.assertEqual(entry['lock_time'], float(lock_time))
        self.assertEqual(entry['rows_sent'], float(rows_sent))
        self.assertEqual(entry['rows_examined'], int(rows_examined))
        self.assertEqual(entry['queries_list'], queries)