1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
15 from api.utils import influx
17 import six.moves.configparser as ConfigParser
20 class GetDataDbClientTestCase(unittest.TestCase):
22 @mock.patch('api.utils.influx.ConfigParser')
23 def test_get_data_db_client_dispatcher_not_influxdb(self, mock_parser):
24 mock_parser.ConfigParser().get.return_value = 'file'
25 # reset exception to avoid
26 # TypeError: catching classes that do not inherit from BaseException
27 mock_parser.NoOptionError = ConfigParser.NoOptionError
29 influx.get_data_db_client()
30 except Exception as e:
31 self.assertIsInstance(e, RuntimeError)
34 class GetIpTestCase(unittest.TestCase):
36 def test_get_url(self):
37 url = 'http://localhost:8086/hello'
38 output = influx._get_ip(url)
41 self.assertEqual(result, output)
44 class WriteDataTestCase(unittest.TestCase):
46 @mock.patch('api.utils.influx.get_data_db_client')
47 def test_write_data(self, mock_get_client):
48 measurement = 'tasklist'
50 timestamp = datetime.datetime.now()
51 tags = {'task_id': str(uuid.uuid4())}
53 influx._write_data(measurement, field, timestamp, tags)
54 mock_get_client.assert_called_with()
57 class WriteDataTasklistTestCase(unittest.TestCase):
59 @mock.patch('api.utils.influx._write_data')
60 def test_write_data_tasklist(self, mock_write_data):
61 task_id = str(uuid.uuid4())
62 timestamp = datetime.datetime.now()
64 influx.write_data_tasklist(task_id, timestamp, status)
66 field = {'status': status, 'error': ''}
67 tags = {'task_id': task_id}
68 mock_write_data.assert_called_with('tasklist', field, timestamp, tags)
71 class QueryTestCase(unittest.TestCase):
73 @mock.patch('api.utils.influx.ConfigParser')
74 def test_query_dispatcher_not_influxdb(self, mock_parser):
75 mock_parser.ConfigParser().get.return_value = 'file'
76 # reset exception to avoid
77 # TypeError: catching classes that do not inherit from BaseException
78 mock_parser.NoOptionError = ConfigParser.NoOptionError
80 sql = 'select * form tasklist'
82 except Exception as e:
83 self.assertIsInstance(e, RuntimeError)
90 if __name__ == '__main__':