Repo structure modification
[functest.git] / functest / tests / unit / core / test_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 import logging
11 import mock
12 import unittest
13
14 from functest.core import TestCasesBase
15
16
17 class TestCasesBaseTesting(unittest.TestCase):
18
19     logging.disable(logging.CRITICAL)
20
21     def setUp(self):
22         self.test = TestCasesBase.TestCasesBase()
23         self.test.project = "functest"
24         self.test.case_name = "base"
25         self.test.start_time = "1"
26         self.test.stop_time = "2"
27         self.test.criteria = "100"
28         self.test.details = {"Hello":  "World"}
29
30     def test_run_unimplemented(self):
31         self.assertEqual(self.test.run(),
32                          TestCasesBase.TestCasesBase.EX_RUN_ERROR)
33
34     @mock.patch('functest.utils.functest_utils.push_results_to_db',
35                 return_value=False)
36     def _test_missing_attribute(self, mock_function):
37         self.assertEqual(self.test.push_to_db(),
38                          TestCasesBase.TestCasesBase.EX_PUSH_TO_DB_ERROR)
39         mock_function.assert_not_called()
40
41     def test_missing_case_name(self):
42         self.test.case_name = None
43         self._test_missing_attribute()
44
45     def test_missing_criteria(self):
46         self.test.criteria = None
47         self._test_missing_attribute()
48
49     def test_missing_start_time(self):
50         self.test.start_time = None
51         self._test_missing_attribute()
52
53     def test_missing_stop_time(self):
54         self.test.stop_time = None
55         self._test_missing_attribute()
56
57     @mock.patch('functest.utils.functest_utils.push_results_to_db',
58                 return_value=True)
59     def test_missing_details(self, mock_function):
60         self.test.details = None
61         self.assertEqual(self.test.push_to_db(),
62                          TestCasesBase.TestCasesBase.EX_OK)
63         mock_function.assert_called_once_with(
64             self.test.project, self.test.case_name, self.test.start_time,
65             self.test.stop_time, self.test.criteria, self.test.details)
66
67     @mock.patch('functest.utils.functest_utils.push_results_to_db',
68                 return_value=False)
69     def test_push_to_db_failed(self, mock_function):
70         self.assertEqual(self.test.push_to_db(),
71                          TestCasesBase.TestCasesBase.EX_PUSH_TO_DB_ERROR)
72         mock_function.assert_called_once_with(
73             self.test.project, self.test.case_name, self.test.start_time,
74             self.test.stop_time, self.test.criteria, self.test.details)
75
76     @mock.patch('functest.utils.functest_utils.push_results_to_db',
77                 return_value=True)
78     def test_push_to_db(self, mock_function):
79         self.assertEqual(self.test.push_to_db(),
80                          TestCasesBase.TestCasesBase.EX_OK)
81         mock_function.assert_called_once_with(
82             self.test.project, self.test.case_name, self.test.start_time,
83             self.test.stop_time, self.test.criteria, self.test.details)
84
85
86 if __name__ == "__main__":
87     unittest.main(verbosity=2)