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