1#!/usr/bin/env python3
2#
3# Copyright 2019, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittest for atest_execution_info."""
18
19# pylint: disable=line-too-long
20
21import time
22import unittest
23
24import atest_execution_info as aei
25import result_reporter
26
27from test_runners import test_runner_base
28
29RESULT_TEST_TEMPLATE = test_runner_base.TestResult(
30    runner_name='someRunner',
31    group_name='someModule',
32    test_name='someClassName#sostName',
33    status=test_runner_base.PASSED_STATUS,
34    details=None,
35    test_count=1,
36    test_time='(10ms)',
37    runner_total=None,
38    group_total=2,
39    additional_info={},
40    test_run_name='com.android.UnitTests'
41)
42
43# pylint: disable=protected-access
44class AtestRunInfoUnittests(unittest.TestCase):
45    """Unit tests for atest_execution_info.py"""
46
47    def test_arrange_test_result_one_module(self):
48        """Test _arrange_test_result method with only one module."""
49        pass_1 = self._create_test_result(status=test_runner_base.PASSED_STATUS)
50        pass_2 = self._create_test_result(status=test_runner_base.PASSED_STATUS)
51        pass_3 = self._create_test_result(status=test_runner_base.PASSED_STATUS)
52        fail_1 = self._create_test_result(status=test_runner_base.FAILED_STATUS)
53        fail_2 = self._create_test_result(status=test_runner_base.FAILED_STATUS)
54        ignore_1 = self._create_test_result(status=test_runner_base.IGNORED_STATUS)
55        reporter_1 = result_reporter.ResultReporter()
56        reporter_1.all_test_results.extend([pass_1, pass_2, pass_3])
57        reporter_2 = result_reporter.ResultReporter()
58        reporter_2.all_test_results.extend([fail_1, fail_2, ignore_1])
59        info_dict = {}
60        aei.AtestExecutionInfo._arrange_test_result(info_dict, [reporter_1, reporter_2])
61        expect_summary = {aei._STATUS_IGNORED_KEY : 1,
62                          aei._STATUS_FAILED_KEY : 2,
63                          aei._STATUS_PASSED_KEY : 3}
64        self.assertEqual(expect_summary, info_dict[aei._TOTAL_SUMMARY_KEY])
65
66    def test_arrange_test_result_multi_module(self):
67        """Test _arrange_test_result method with multi module."""
68        group_a_pass_1 = self._create_test_result(group_name='grpup_a',
69                                                  status=test_runner_base.PASSED_STATUS)
70        group_b_pass_1 = self._create_test_result(group_name='grpup_b',
71                                                  status=test_runner_base.PASSED_STATUS)
72        group_c_pass_1 = self._create_test_result(group_name='grpup_c',
73                                                  status=test_runner_base.PASSED_STATUS)
74        group_b_fail_1 = self._create_test_result(group_name='grpup_b',
75                                                  status=test_runner_base.FAILED_STATUS)
76        group_c_fail_1 = self._create_test_result(group_name='grpup_c',
77                                                  status=test_runner_base.FAILED_STATUS)
78        group_c_ignore_1 = self._create_test_result(group_name='grpup_c',
79                                                    status=test_runner_base.IGNORED_STATUS)
80        reporter_1 = result_reporter.ResultReporter()
81        reporter_1.all_test_results.extend([group_a_pass_1, group_b_pass_1, group_c_pass_1])
82        reporter_2 = result_reporter.ResultReporter()
83        reporter_2.all_test_results.extend([group_b_fail_1, group_c_fail_1, group_c_ignore_1])
84
85        info_dict = {}
86        aei.AtestExecutionInfo._arrange_test_result(info_dict, [reporter_1, reporter_2])
87        expect_group_a_summary = {aei._STATUS_IGNORED_KEY : 0,
88                                  aei._STATUS_FAILED_KEY : 0,
89                                  aei._STATUS_PASSED_KEY : 1}
90        self.assertEqual(
91            expect_group_a_summary,
92            info_dict[aei._TEST_RUNNER_KEY]['someRunner']['grpup_a'][aei._SUMMARY_KEY])
93
94        expect_group_b_summary = {aei._STATUS_IGNORED_KEY : 0,
95                                  aei._STATUS_FAILED_KEY : 1,
96                                  aei._STATUS_PASSED_KEY : 1}
97        self.assertEqual(
98            expect_group_b_summary,
99            info_dict[aei._TEST_RUNNER_KEY]['someRunner']['grpup_b'][aei._SUMMARY_KEY])
100
101        expect_group_c_summary = {aei._STATUS_IGNORED_KEY : 1,
102                                  aei._STATUS_FAILED_KEY : 1,
103                                  aei._STATUS_PASSED_KEY : 1}
104        self.assertEqual(
105            expect_group_c_summary,
106            info_dict[aei._TEST_RUNNER_KEY]['someRunner']['grpup_c'][aei._SUMMARY_KEY])
107
108        expect_total_summary = {aei._STATUS_IGNORED_KEY : 1,
109                                aei._STATUS_FAILED_KEY : 2,
110                                aei._STATUS_PASSED_KEY : 3}
111        self.assertEqual(expect_total_summary, info_dict[aei._TOTAL_SUMMARY_KEY])
112
113    def test_preparation_time(self):
114        """Test preparation_time method."""
115        start_time = time.time()
116        aei.PREPARE_END_TIME = None
117        self.assertTrue(aei.preparation_time(start_time) is None)
118        aei.PREPARE_END_TIME = time.time()
119        self.assertFalse(aei.preparation_time(start_time) is None)
120
121    def test_arrange_test_result_multi_runner(self):
122        """Test _arrange_test_result method with multi runner."""
123        runner_a_pass_1 = self._create_test_result(runner_name='runner_a',
124                                                   status=test_runner_base.PASSED_STATUS)
125        runner_a_pass_2 = self._create_test_result(runner_name='runner_a',
126                                                   status=test_runner_base.PASSED_STATUS)
127        runner_a_pass_3 = self._create_test_result(runner_name='runner_a',
128                                                   status=test_runner_base.PASSED_STATUS)
129        runner_b_fail_1 = self._create_test_result(runner_name='runner_b',
130                                                   status=test_runner_base.FAILED_STATUS)
131        runner_b_fail_2 = self._create_test_result(runner_name='runner_b',
132                                                   status=test_runner_base.FAILED_STATUS)
133        runner_b_ignore_1 = self._create_test_result(runner_name='runner_b',
134                                                     status=test_runner_base.IGNORED_STATUS)
135
136        reporter_1 = result_reporter.ResultReporter()
137        reporter_1.all_test_results.extend([runner_a_pass_1, runner_a_pass_2, runner_a_pass_3])
138        reporter_2 = result_reporter.ResultReporter()
139        reporter_2.all_test_results.extend([runner_b_fail_1, runner_b_fail_2, runner_b_ignore_1])
140        info_dict = {}
141        aei.AtestExecutionInfo._arrange_test_result(info_dict, [reporter_1, reporter_2])
142        expect_group_a_summary = {aei._STATUS_IGNORED_KEY : 0,
143                                  aei._STATUS_FAILED_KEY : 0,
144                                  aei._STATUS_PASSED_KEY : 3}
145        self.assertEqual(
146            expect_group_a_summary,
147            info_dict[aei._TEST_RUNNER_KEY]['runner_a']['someModule'][aei._SUMMARY_KEY])
148
149        expect_group_b_summary = {aei._STATUS_IGNORED_KEY : 1,
150                                  aei._STATUS_FAILED_KEY : 2,
151                                  aei._STATUS_PASSED_KEY : 0}
152        self.assertEqual(
153            expect_group_b_summary,
154            info_dict[aei._TEST_RUNNER_KEY]['runner_b']['someModule'][aei._SUMMARY_KEY])
155
156        expect_total_summary = {aei._STATUS_IGNORED_KEY : 1,
157                                aei._STATUS_FAILED_KEY : 2,
158                                aei._STATUS_PASSED_KEY : 3}
159        self.assertEqual(expect_total_summary, info_dict[aei._TOTAL_SUMMARY_KEY])
160
161    def _create_test_result(self, **kwargs):
162        """A Helper to create TestResult"""
163        test_info = test_runner_base.TestResult(**RESULT_TEST_TEMPLATE._asdict())
164        return test_info._replace(**kwargs)
165
166if __name__ == "__main__":
167    unittest.main()
168