1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import unittest 17import pandas as pd 18import json 19from src.spdx_license_matcher import SPDXLicenseMatcher 20import os 21 22class TestSPDXLicenseMatcher(unittest.TestCase): 23 @classmethod 24 def setUpClass(cls): 25 # 创建临时测试数据 26 cls.test_excel_path = 'test_oh_spdx_license_match.xlsx' 27 cls.test_json_path = 'test_spdx.json' 28 cls.output_excel_path = 'test_output.xlsx' 29 30 # Excel测试数据,包含分号分隔的许可证名 31 df = pd.DataFrame({ 32 'cc_url': ['https://example.com/license1', 'https://example.com/license2'], 33 'spdx_fixed_license_name': ['Apache License 2.0', 'Creative Commons Attribution 4.0 International;MIT License'] 34 }) 35 df.to_excel(cls.test_excel_path, index=False) 36 37 # JSON测试数据,包含格式规范化的SPDX映射 38 spdx_data = { 39 "Apache License 2.0": "Apache-2.0", 40 "Creative Commons Attribution 4.0 International": "CC-BY-4.0", 41 "MIT License": "MIT" 42 } 43 with open(cls.test_json_path, 'w', encoding='utf-8') as f: 44 json.dump(spdx_data, f) 45 46 @classmethod 47 def tearDownClass(cls): 48 # 删除临时文件 49 os.remove(cls.test_excel_path) 50 os.remove(cls.test_json_path) 51 os.remove(cls.output_excel_path) 52 53 def setUp(self): 54 # 初始化 SPDXLicenseMatcher 实例 55 self.matcher = SPDXLicenseMatcher(self.test_excel_path, self.test_json_path) 56 57 def test_load_data(self): 58 # 测试数据加载 59 self.assertIsNotNone(self.matcher.df) 60 self.assertGreater(len(self.matcher.spdx_mapping), 0) 61 62 def test_copy_url_column(self): 63 # 测试URL列复制 64 self.matcher.copy_url_column() 65 self.assertIn('match_url', self.matcher.df.columns) 66 self.assertEqual(self.matcher.df['match_url'][0], 'https://example.com/license1') 67 68 def test_match_license_column(self): 69 # 测试许可证匹配,包含分号分隔的许可证名 70 self.matcher.match_license_column() 71 self.assertIn('match_license', self.matcher.df.columns) 72 # 验证匹配结果 73 self.assertEqual(self.matcher.df['match_license'][0], 'Apache-2.0') 74 self.assertEqual(self.matcher.df['match_license'][1], 'CC-BY-4.0;MIT') 75 76 def test_save_to_excel(self): 77 # 测试保存到Excel文件 78 self.matcher.copy_url_column() 79 self.matcher.match_license_column() 80 self.matcher.save_to_excel(self.output_excel_path) 81 self.assertTrue(os.path.exists(self.output_excel_path)) 82 # 验证保存内容 83 df_saved = pd.read_excel(self.output_excel_path) 84 self.assertIn('match_license', df_saved.columns) 85 self.assertEqual(df_saved['match_license'][0], 'Apache-2.0') 86 self.assertEqual(df_saved['match_license'][1], 'CC-BY-4.0;MIT') 87 88if __name__ == '__main__': 89 unittest.main() 90