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"""Unittests for aidegen_metrics."""
18
19import unittest
20from unittest import mock
21
22from aidegen import constant
23from aidegen.lib import aidegen_metrics
24from atest import atest_utils
25
26
27try:
28    from asuite.metrics import metrics
29    from asuite.metrics import metrics_utils
30except ImportError:
31    metrics = None
32    metrics_utils = None
33
34
35class AidegenMetricsUnittests(unittest.TestCase):
36    """Unit tests for aidegen_metrics.py."""
37
38    @mock.patch.object(atest_utils, 'print_data_collection_notice')
39    def test_starts_asuite_metrics(self, mock_print_data):
40        """Test starts_asuite_metrics."""
41        references = ['nothing']
42        if not metrics:
43            aidegen_metrics.starts_asuite_metrics(references)
44            self.assertFalse(mock_print_data.called)
45        else:
46            with mock.patch.object(metrics_utils, 'get_start_time') as mk_get:
47                with mock.patch.object(metrics, 'AtestStartEvent') as mk_start:
48                    aidegen_metrics.starts_asuite_metrics(references)
49                    self.assertTrue(mock_print_data.called)
50                    self.assertTrue(mk_get.called)
51                    self.assertTrue(mk_start.called)
52
53    def test_ends_asuite_metrics(self):
54        """Test ends_asuite_metrics."""
55        exit_code = constant.EXIT_CODE_NORMAL
56        if metrics_utils:
57            with mock.patch.object(metrics_utils, 'send_exit_event') as mk_send:
58                aidegen_metrics.ends_asuite_metrics(exit_code)
59                self.assertTrue(mk_send.called)
60
61    @mock.patch.object(aidegen_metrics, 'ends_asuite_metrics')
62    def test_send_exception_metrics(self, mock_ends_metrics):
63        """Test send_exception_metrics."""
64        mock_ends_metrics.return_value = True
65        aidegen_metrics.send_exception_metrics(1, '', '', 'err_test')
66        self.assertTrue(mock_ends_metrics.called)
67
68if __name__ == '__main__':
69    unittest.main()
70