Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion office/api/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ def split_excel_by_column(filepath: str, column: int, worksheet_name: str = None
Returns:
None
"""
poexcel.split_excel_by_column(filepath=filepath, column=column, worksheet_name=worksheet_name)
split_excel = getattr(poexcel, "split_excel_by_column", None)
if split_excel is None:
from office.lib.excel.SplitExcel import split_excel_by_column as split_excel

split_excel(filepath=filepath, column=column, worksheet_name=worksheet_name)


def excel2pdf(excel_path, pdf_path, sheet_id: int = 0):
Expand Down
69 changes: 69 additions & 0 deletions tests/test_code/test_excel_split_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Tests for the Excel split-by-column API wrapper."""

import importlib.util
from pathlib import Path
import sys
from types import ModuleType, SimpleNamespace
import unittest
from unittest.mock import Mock, patch


def load_excel_api(poexcel):
module_path = Path(__file__).resolve().parents[2] / "office" / "api" / "excel.py"
spec = importlib.util.spec_from_file_location("excel_api_under_test", module_path)
module = importlib.util.module_from_spec(spec)

with patch.dict(sys.modules, {"poexcel": poexcel}):
spec.loader.exec_module(module)

return module


class TestExcelSplitWrapper(unittest.TestCase):
def test_delegates_to_poexcel_when_export_is_available(self):
split_excel = Mock()
excel = load_excel_api(
SimpleNamespace(split_excel_by_column=split_excel)
)

excel.split_excel_by_column(
filepath="orders.xlsx",
column=2,
worksheet_name="Orders",
)

split_excel.assert_called_once_with(
filepath="orders.xlsx",
column=2,
worksheet_name="Orders",
)

def test_falls_back_to_bundled_implementation_when_export_is_missing(self):
split_excel = Mock()
split_module = ModuleType("office.lib.excel.SplitExcel")
split_module.split_excel_by_column = split_excel

modules = {
"office": ModuleType("office"),
"office.lib": ModuleType("office.lib"),
"office.lib.excel": ModuleType("office.lib.excel"),
"office.lib.excel.SplitExcel": split_module,
}
excel = load_excel_api(SimpleNamespace())

with patch.dict(sys.modules, modules):
excel.split_excel_by_column(
filepath="orders.xlsx",
column=2,
worksheet_name="Orders",
)

split_excel.assert_called_once_with(
filepath="orders.xlsx",
column=2,
worksheet_name="Orders",
)


if __name__ == "__main__":
unittest.main()