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