xref: /aosp_15_r20/tools/asuite/atest/test_mapping_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2018, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the 'License');
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an 'AS IS' BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for test_mapping"""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Worker
20*c2e18aaaSAndroid Build Coastguard Workerimport unittest
21*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
22*c2e18aaaSAndroid Build Coastguard Worker
23*c2e18aaaSAndroid Build Coastguard Workerfrom atest import test_mapping
24*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_constants as uc
25*c2e18aaaSAndroid Build Coastguard Worker
26*c2e18aaaSAndroid Build Coastguard Worker
27*c2e18aaaSAndroid Build Coastguard Workerclass TestMappingUnittests(unittest.TestCase):
28*c2e18aaaSAndroid Build Coastguard Worker  """Unit tests for test_mapping.py"""
29*c2e18aaaSAndroid Build Coastguard Worker
30*c2e18aaaSAndroid Build Coastguard Worker  def test_parsing(self):
31*c2e18aaaSAndroid Build Coastguard Worker    """Test creating TestDetail object"""
32*c2e18aaaSAndroid Build Coastguard Worker    detail = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
33*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(uc.TEST_MAPPING_TEST['name'], detail.name)
34*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(detail.host)
35*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual([], detail.options)
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker  def test_parsing_with_option(self):
38*c2e18aaaSAndroid Build Coastguard Worker    """Test creating TestDetail object with option configured"""
39*c2e18aaaSAndroid Build Coastguard Worker    detail = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
40*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(uc.TEST_MAPPING_TEST_WITH_OPTION['name'], detail.name)
41*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(uc.TEST_MAPPING_TEST_WITH_OPTION_STR, str(detail))
42*c2e18aaaSAndroid Build Coastguard Worker
43*c2e18aaaSAndroid Build Coastguard Worker  def test_parsing_with_bad_option(self):
44*c2e18aaaSAndroid Build Coastguard Worker    """Test creating TestDetail object with bad option configured"""
45*c2e18aaaSAndroid Build Coastguard Worker    with self.assertRaises(Exception) as context:
46*c2e18aaaSAndroid Build Coastguard Worker      test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_BAD_OPTION)
47*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(
48*c2e18aaaSAndroid Build Coastguard Worker        'Each option can only have one key.', str(context.exception)
49*c2e18aaaSAndroid Build Coastguard Worker    )
50*c2e18aaaSAndroid Build Coastguard Worker
51*c2e18aaaSAndroid Build Coastguard Worker  def test_parsing_with_bad_host_value(self):
52*c2e18aaaSAndroid Build Coastguard Worker    """Test creating TestDetail object with bad host value configured"""
53*c2e18aaaSAndroid Build Coastguard Worker    with self.assertRaises(Exception) as context:
54*c2e18aaaSAndroid Build Coastguard Worker      test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_BAD_HOST_VALUE)
55*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(
56*c2e18aaaSAndroid Build Coastguard Worker        'host can only have boolean value.', str(context.exception)
57*c2e18aaaSAndroid Build Coastguard Worker    )
58*c2e18aaaSAndroid Build Coastguard Worker
59*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('atest.atest_utils.get_modified_files')
60*c2e18aaaSAndroid Build Coastguard Worker  def test_is_match_file_patterns(self, mock_modified_files):
61*c2e18aaaSAndroid Build Coastguard Worker    """Test mathod is_match_file_patterns."""
62*c2e18aaaSAndroid Build Coastguard Worker    test_mapping_file = ''
63*c2e18aaaSAndroid Build Coastguard Worker    test_detail = {
64*c2e18aaaSAndroid Build Coastguard Worker        'name': 'Test',
65*c2e18aaaSAndroid Build Coastguard Worker        'file_patterns': [
66*c2e18aaaSAndroid Build Coastguard Worker            '(/|^)test_fp1[^/]*\\.java',
67*c2e18aaaSAndroid Build Coastguard Worker            '(/|^)test_fp2[^/]*\\.java',
68*c2e18aaaSAndroid Build Coastguard Worker        ],
69*c2e18aaaSAndroid Build Coastguard Worker    }
70*c2e18aaaSAndroid Build Coastguard Worker    mock_modified_files.return_value = {
71*c2e18aaaSAndroid Build Coastguard Worker        '/a/b/test_fp122.java',
72*c2e18aaaSAndroid Build Coastguard Worker        '/a/b/c/d/test_fp222.java',
73*c2e18aaaSAndroid Build Coastguard Worker    }
74*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(
75*c2e18aaaSAndroid Build Coastguard Worker        test_mapping.is_match_file_patterns(test_mapping_file, test_detail)
76*c2e18aaaSAndroid Build Coastguard Worker    )
77*c2e18aaaSAndroid Build Coastguard Worker    mock_modified_files.return_value = {}
78*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(
79*c2e18aaaSAndroid Build Coastguard Worker        test_mapping.is_match_file_patterns(test_mapping_file, test_detail)
80*c2e18aaaSAndroid Build Coastguard Worker    )
81*c2e18aaaSAndroid Build Coastguard Worker    mock_modified_files.return_value = {'/a/b/test_fp3.java'}
82*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(
83*c2e18aaaSAndroid Build Coastguard Worker        test_mapping.is_match_file_patterns(test_mapping_file, test_detail)
84*c2e18aaaSAndroid Build Coastguard Worker    )
85*c2e18aaaSAndroid Build Coastguard Worker    test_mapping_file = '/a/b/TEST_MAPPING'
86*c2e18aaaSAndroid Build Coastguard Worker    mock_modified_files.return_value = {
87*c2e18aaaSAndroid Build Coastguard Worker        '/a/b/test_fp3.java',
88*c2e18aaaSAndroid Build Coastguard Worker        '/a/b/TEST_MAPPING',
89*c2e18aaaSAndroid Build Coastguard Worker    }
90*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(
91*c2e18aaaSAndroid Build Coastguard Worker        test_mapping.is_match_file_patterns(test_mapping_file, test_detail)
92*c2e18aaaSAndroid Build Coastguard Worker    )
93*c2e18aaaSAndroid Build Coastguard Worker
94*c2e18aaaSAndroid Build Coastguard Worker
95*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
96*c2e18aaaSAndroid Build Coastguard Worker  unittest.main()
97