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