80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
# Copyright (c) 2025 Jeffrey Hsu - JITToolBox
|
||
# #
|
||
# This program is free software: you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
# #
|
||
# This program is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
# #
|
||
# You should have received a copy of the GNU General Public License
|
||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
import shutil
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
from openpyxl.workbook import Workbook
|
||
|
||
from toolbox.services.excel_service import ExcelService, AchievementExcelService
|
||
from toolbox.tests import TEST_FILE_PATH
|
||
|
||
SAVE_TEMP_FILE = False
|
||
|
||
|
||
class TestExcelService:
|
||
es = ExcelService(TEST_FILE_PATH / 'test_excel_services_01.xlsx')
|
||
es.open(data_only=True)
|
||
|
||
def test_open(self):
|
||
assert isinstance(self.es._workbook, Workbook)
|
||
assert self.es._sheet is None
|
||
|
||
def test_open_failed(self):
|
||
with pytest.raises(FileNotFoundError):
|
||
ExcelService(TEST_FILE_PATH / 'non_existent_file.xlsx').open()
|
||
|
||
def test_active_sheet(self):
|
||
self.es.active_sheet('Sheet1')
|
||
assert self.es._sheet.title == 'Sheet1'
|
||
self.es.active_sheet('Sheet2')
|
||
assert self.es._sheet.title == 'Sheet2'
|
||
|
||
def test_cur_active_sheet(self):
|
||
self.es.active_sheet('Sheet1')
|
||
assert self.es.cur_active_sheet.title == 'Sheet1'
|
||
self.es.active_sheet('Sheet2')
|
||
assert self.es.cur_active_sheet.title == 'Sheet2'
|
||
|
||
def test_save_and_close(self):
|
||
temp_excel_file = TEST_FILE_PATH / 'test_excel_services_01_temp.xlsx'
|
||
shutil.copy(self.es._file_path, temp_excel_file)
|
||
|
||
self.es.active_sheet('Sheet1').cur_active_sheet['A1'] = 'Modified'
|
||
self.es.save().close()
|
||
|
||
es2 = ExcelService(temp_excel_file).open().active_sheet('Sheet1')
|
||
assert es2.cur_active_sheet['A1'].value == 'Modified'
|
||
es2.close()
|
||
|
||
if not SAVE_TEMP_FILE:
|
||
Path(TEST_FILE_PATH / 'test_excel_services_01_temp.xlsx').unlink()
|
||
|
||
|
||
class TestAchievementExcelService:
|
||
aes = AchievementExcelService(TEST_FILE_PATH / 'test_achievement_excel_service_01.xlsm')
|
||
aes.load_config(TEST_FILE_PATH / 'test_achievement.default.excel_01.json')
|
||
|
||
def test_read_class_info(self):
|
||
cis = self.aes.read_class_info()
|
||
assert len(cis) == 2
|
||
assert cis[0].full_name == '22工程管理(1)(2)'
|
||
assert cis[1].full_name == '22工程管理(1)(2)'
|
||
|
||
assert cis[0].class_name == '22工程管理(1)'
|
||
assert cis[0].class_number == 34
|
||
assert cis[1].class_name == '22工程管理(2)'
|
||
assert cis[1].class_number == 36
|