import unittest
import pandas as pd
import json
from src.spdx_license_matcher import SPDXLicenseMatcher
import os
class TestSPDXLicenseMatcher(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.test_excel_path = 'test_oh_spdx_license_match.xlsx'
cls.test_json_path = 'test_spdx.json'
cls.output_excel_path = 'test_output.xlsx'
df = pd.DataFrame({
'cc_url': ['https://example.com/license1', 'https://example.com/license2'],
'spdx_fixed_license_name': ['Apache License 2.0', 'Creative Commons Attribution 4.0 International;MIT License']
})
df.to_excel(cls.test_excel_path, index=False)
spdx_data = {
"Apache License 2.0": "Apache-2.0",
"Creative Commons Attribution 4.0 International": "CC-BY-4.0",
"MIT License": "MIT"
}
with open(cls.test_json_path, 'w', encoding='utf-8') as f:
json.dump(spdx_data, f)
@classmethod
def tearDownClass(cls):
os.remove(cls.test_excel_path)
os.remove(cls.test_json_path)
os.remove(cls.output_excel_path)
def setUp(self):
self.matcher = SPDXLicenseMatcher(self.test_excel_path, self.test_json_path)
def test_load_data(self):
self.assertIsNotNone(self.matcher.df)
self.assertGreater(len(self.matcher.spdx_mapping), 0)
def test_copy_url_column(self):
self.matcher.copy_url_column()
self.assertIn('match_url', self.matcher.df.columns)
self.assertEqual(self.matcher.df['match_url'][0], 'https://example.com/license1')
def test_match_license_column(self):
self.matcher.match_license_column()
self.assertIn('match_license', self.matcher.df.columns)
self.assertEqual(self.matcher.df['match_license'][0], 'Apache-2.0')
self.assertEqual(self.matcher.df['match_license'][1], 'CC-BY-4.0;MIT')
def test_save_to_excel(self):
self.matcher.copy_url_column()
self.matcher.match_license_column()
self.matcher.save_to_excel(self.output_excel_path)
self.assertTrue(os.path.exists(self.output_excel_path))
df_saved = pd.read_excel(self.output_excel_path)
self.assertIn('match_license', df_saved.columns)
self.assertEqual(df_saved['match_license'][0], 'Apache-2.0')
self.assertEqual(df_saved['match_license'][1], 'CC-BY-4.0;MIT')
if __name__ == '__main__':
unittest.main()