This commit is contained in:
2025-08-22 19:03:51 +08:00
parent 438cb8a1d9
commit da723409ca
9 changed files with 512 additions and 0 deletions

19
toolbox/tests/__init__.py Normal file
View File

@@ -0,0 +1,19 @@
# 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 os
from pathlib import Path
PACKAGE_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_FILE_PATH = Path(os.path.join(PACKAGE_DIR, 'files'))

View File

@@ -0,0 +1,41 @@
# 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/>.
from toolbox.models.config import AESConfig, SingleExcelConfigItem, RangeExcelConfigItem
from toolbox.tests import TEST_FILE_PATH
def test_config_model():
aesc = AESConfig(TEST_FILE_PATH / 'test_config_model_01.json')
a = aesc.get_config('A')
assert isinstance(a, SingleExcelConfigItem)
assert a.position == 'H1'
b = aesc.get_config('B')
assert isinstance(b, SingleExcelConfigItem)
assert b.position == 'D10'
c = aesc.get_config('C')
assert isinstance(c, RangeExcelConfigItem)
assert c.position == 'K'
assert c.fposition.format(1) == 'K1'
assert c.start == 2
assert c.end == 5
d = aesc.get_config('D')
assert isinstance(d, RangeExcelConfigItem)
assert d.position == 'Q'
assert d.fposition.format(d.start) == 'Q22'
assert d.end is None

View File

@@ -0,0 +1,79 @@
# 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工程管理12'
assert cis[1].full_name == '22工程管理12'
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