1#!/usr/bin/env python3
2#
3# Copyright 2018, 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 test_mapping"""
18
19# pylint: disable=line-too-long
20
21import unittest
22
23from unittest import mock
24
25import test_mapping
26import unittest_constants as uc
27
28
29class TestMappingUnittests(unittest.TestCase):
30    """Unit tests for test_mapping.py"""
31
32    def test_parsing(self):
33        """Test creating TestDetail object"""
34        detail = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
35        self.assertEqual(uc.TEST_MAPPING_TEST['name'], detail.name)
36        self.assertTrue(detail.host)
37        self.assertEqual([], detail.options)
38
39    def test_parsing_with_option(self):
40        """Test creating TestDetail object with option configured"""
41        detail = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
42        self.assertEqual(uc.TEST_MAPPING_TEST_WITH_OPTION['name'], detail.name)
43        self.assertEqual(uc.TEST_MAPPING_TEST_WITH_OPTION_STR, str(detail))
44
45    def test_parsing_with_bad_option(self):
46        """Test creating TestDetail object with bad option configured"""
47        with self.assertRaises(Exception) as context:
48            test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_BAD_OPTION)
49        self.assertEqual(
50            'Each option can only have one key.', str(context.exception))
51
52    def test_parsing_with_bad_host_value(self):
53        """Test creating TestDetail object with bad host value configured"""
54        with self.assertRaises(Exception) as context:
55            test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_BAD_HOST_VALUE)
56        self.assertEqual(
57            'host can only have boolean value.', str(context.exception))
58
59    @mock.patch("atest_utils.get_modified_files")
60    def test_is_match_file_patterns(self, mock_modified_files):
61        """Test mathod is_match_file_patterns."""
62        test_mapping_file = ''
63        test_detail = {
64            "name": "Test",
65            "file_patterns": ["(/|^)test_fp1[^/]*\\.java",
66                              "(/|^)test_fp2[^/]*\\.java"]
67        }
68        mock_modified_files.return_value = {'/a/b/test_fp122.java',
69                                            '/a/b/c/d/test_fp222.java'}
70        self.assertTrue(test_mapping.is_match_file_patterns(test_mapping_file,
71                                                            test_detail))
72        mock_modified_files.return_value = {}
73        self.assertFalse(test_mapping.is_match_file_patterns(test_mapping_file,
74                                                             test_detail))
75        mock_modified_files.return_value = {'/a/b/test_fp3.java'}
76        self.assertFalse(test_mapping.is_match_file_patterns(test_mapping_file,
77                                                             test_detail))
78        test_mapping_file = '/a/b/TEST_MAPPING'
79        mock_modified_files.return_value = {'/a/b/test_fp3.java',
80                                            '/a/b/TEST_MAPPING'}
81        self.assertTrue(test_mapping.is_match_file_patterns(test_mapping_file,
82                                                            test_detail))
83
84
85if __name__ == '__main__':
86    unittest.main()
87