Remove main() and __main__ from tests.
[yardstick.git] / yardstick / tests / unit / benchmark / core / test_report.py
1 ##############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka.
3 #
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
10 # Unittest for yardstick.benchmark.core.report
11
12 from __future__ import print_function
13
14 from __future__ import absolute_import
15
16 import unittest
17 import uuid
18
19 try:
20     from unittest import mock
21 except ImportError:
22     import mock
23
24 from yardstick.benchmark.core import report
25 from yardstick.cmd.commands import change_osloobj_to_paras
26
27 FAKE_YAML_NAME = 'fake_name'
28 FAKE_TASK_ID = str(uuid.uuid4())
29 FAKE_DB_FIELDKEYS = [{'fieldKey': 'fake_key'}]
30 FAKE_TIME = '0000-00-00T00:00:00.000000Z'
31 FAKE_DB_TASK = [{'fake_key': 0.000, 'time': FAKE_TIME}]
32 FAKE_TIMESTAMP = ['fake_time']
33 DUMMY_TASK_ID = 'aaaaaa-aaaaaaaa-aaaaaaaaaa-aaaaaa'
34
35
36 class ReportTestCase(unittest.TestCase):
37
38     def setUp(self):
39         super(ReportTestCase, self).setUp()
40         self.param = change_osloobj_to_paras({})
41         self.param.yaml_name = [FAKE_YAML_NAME]
42         self.param.task_id = [FAKE_TASK_ID]
43         self.rep = report.Report()
44
45     @mock.patch('yardstick.benchmark.core.report.Report._get_tasks')
46     @mock.patch('yardstick.benchmark.core.report.Report._get_fieldkeys')
47     @mock.patch('yardstick.benchmark.core.report.Report._validate')
48     def test_generate_success(self, mock_valid, mock_keys, mock_tasks):
49         mock_tasks.return_value = FAKE_DB_TASK
50         mock_keys.return_value = FAKE_DB_FIELDKEYS
51         self.rep.generate(self.param)
52         mock_valid.assert_called_once_with(FAKE_YAML_NAME, FAKE_TASK_ID)
53         self.assertEqual(1, mock_tasks.call_count)
54         self.assertEqual(1, mock_keys.call_count)
55
56     # pylint: disable=deprecated-method
57     def test_invalid_yaml_name(self):
58         self.assertRaisesRegexp(ValueError, "yaml*", self.rep._validate,
59                                 'F@KE_NAME', FAKE_TASK_ID)
60
61     # pylint: disable=deprecated-method
62     def test_invalid_task_id(self):
63         self.assertRaisesRegexp(ValueError, "task*", self.rep._validate,
64                                 FAKE_YAML_NAME, DUMMY_TASK_ID)
65
66     @mock.patch('api.utils.influx.query')
67     def test_task_not_found(self, mock_query):
68         mock_query.return_value = []
69         self.rep.yaml_name = FAKE_YAML_NAME
70         self.rep.task_id = FAKE_TASK_ID
71         # pylint: disable=deprecated-method
72         self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_fieldkeys)
73         self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_tasks)
74         # pylint: enable=deprecated-method