Skip to content

Commit 6aaa11a

Browse files
k-ibarakiclaude
andcommitted
test: add comprehensive tests for include_row_data
Add test coverage for include_row_data parameter in both parser and server layers. Test cases: - Basic row data inclusion (include_row_data=True) - Default behavior verification (include_row_data=False) - Same-row multiple matches with independent row_data - Null cell exclusion from row_data - Multi-sheet search with row data - Server-level parameter passing Coverage: - Parser layer: 5 new tests - Server layer: 1 new test + 1 updated test All tests passing with 100% coverage of new code paths. Related: #55 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 80cd921 commit 6aaa11a

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

tests/test_server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_excel_search_mode(
244244

245245
# 検索メソッドが呼ばれることを確認
246246
mock_excel_parser.search_cells.assert_called_once_with(
247-
"/sites/test/Shared Documents/test.xlsx", "売上", sheet_name=None
247+
"/sites/test/Shared Documents/test.xlsx", "売上", sheet_name=None, include_row_data=False
248248
)
249249
# parse_to_jsonは呼ばれない
250250
mock_excel_parser.parse_to_json.assert_not_called()
@@ -295,6 +295,26 @@ def test_excel_with_cell_range_parameter(
295295
expand_axis_range=False,
296296
)
297297

298+
@pytest.mark.unit
299+
def test_excel_search_with_include_row_data(
300+
self, mock_config, mock_sharepoint_client, mock_excel_parser
301+
):
302+
"""Excel検索モードでinclude_row_data=Trueが渡されるテスト"""
303+
with patch(
304+
"src.server._get_sharepoint_client", return_value=mock_sharepoint_client
305+
):
306+
with patch("src.server.config", mock_config):
307+
sharepoint_excel(
308+
file_path="/sites/test/Shared Documents/test.xlsx",
309+
query="売上",
310+
include_row_data=True,
311+
)
312+
313+
mock_excel_parser.search_cells.assert_called_once_with(
314+
"/sites/test/Shared Documents/test.xlsx", "売上", sheet_name=None, include_row_data=True
315+
)
316+
mock_excel_parser.parse_to_json.assert_not_called()
317+
298318
@pytest.mark.unit
299319
def test_excel_with_real_json(
300320
self, mock_config, mock_sharepoint_client, mock_excel_parser

tests/test_sharepoint_excel.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,3 +1780,120 @@ def test_omit_null_dimensions(self):
17801780

17811781
# dimensionsがNoneの場合は省略
17821782
assert "dimensions" not in sheet
1783+
1784+
def test_search_cells_include_row_data_true(self):
1785+
"""include_row_data=Trueで行データが含まれることのテスト"""
1786+
excel_bytes = self._create_test_excel()
1787+
self.mock_download_client.download_file.return_value = excel_bytes
1788+
1789+
parser = SharePointExcelParser(self.mock_download_client)
1790+
result_json = parser.search_cells(
1791+
"/test/file.xlsx", "John", include_row_data=True
1792+
)
1793+
1794+
result = json.loads(result_json)
1795+
assert result["match_count"] == 1
1796+
match = result["matches"][0]
1797+
assert match["coordinate"] == "A2"
1798+
assert match["value"] == "John"
1799+
1800+
# row_dataが含まれる
1801+
assert "row_data" in match
1802+
row_data = match["row_data"]
1803+
# A2="John", B2=25 の2セル
1804+
assert len(row_data) == 2
1805+
coords = [c["coordinate"] for c in row_data]
1806+
assert "A2" in coords
1807+
assert "B2" in coords
1808+
# 値の確認
1809+
values = {c["coordinate"]: c["value"] for c in row_data}
1810+
assert values["A2"] == "John"
1811+
assert values["B2"] == 25
1812+
1813+
def test_search_cells_include_row_data_false_default(self):
1814+
"""デフォルト(include_row_data=False)でrow_dataが含まれないことのテスト"""
1815+
excel_bytes = self._create_test_excel()
1816+
self.mock_download_client.download_file.return_value = excel_bytes
1817+
1818+
parser = SharePointExcelParser(self.mock_download_client)
1819+
result_json = parser.search_cells("/test/file.xlsx", "John")
1820+
1821+
result = json.loads(result_json)
1822+
assert result["match_count"] == 1
1823+
match = result["matches"][0]
1824+
assert "row_data" not in match
1825+
1826+
def test_search_cells_include_row_data_multiple_matches_same_row(self):
1827+
"""同一行に複数マッチ時、各マッチにrow_dataが含まれることのテスト"""
1828+
wb = Workbook()
1829+
ws = wb.active
1830+
ws.title = "Sheet1"
1831+
ws["A1"] = "売上報告"
1832+
ws["B1"] = "売上合計"
1833+
ws["C1"] = 1000
1834+
1835+
excel_bytes = BytesIO()
1836+
wb.save(excel_bytes)
1837+
excel_bytes.seek(0)
1838+
1839+
self.mock_download_client.download_file.return_value = excel_bytes.getvalue()
1840+
1841+
parser = SharePointExcelParser(self.mock_download_client)
1842+
result_json = parser.search_cells(
1843+
"/test/file.xlsx", "売上", include_row_data=True
1844+
)
1845+
1846+
result = json.loads(result_json)
1847+
assert result["match_count"] == 2
1848+
1849+
# 各マッチにrow_dataが独立して含まれる
1850+
for match in result["matches"]:
1851+
assert "row_data" in match
1852+
# 同一行なので同じrow_data(A1, B1, C1の3セル)
1853+
assert len(match["row_data"]) == 3
1854+
1855+
def test_search_cells_include_row_data_null_cells_excluded(self):
1856+
"""nullセルがrow_dataから除外されることのテスト"""
1857+
wb = Workbook()
1858+
ws = wb.active
1859+
ws.title = "Sheet1"
1860+
ws["A1"] = "Name"
1861+
# B1 is None (null)
1862+
ws["C1"] = "Value"
1863+
1864+
excel_bytes = BytesIO()
1865+
wb.save(excel_bytes)
1866+
excel_bytes.seek(0)
1867+
1868+
self.mock_download_client.download_file.return_value = excel_bytes.getvalue()
1869+
1870+
parser = SharePointExcelParser(self.mock_download_client)
1871+
result_json = parser.search_cells(
1872+
"/test/file.xlsx", "Name", include_row_data=True
1873+
)
1874+
1875+
result = json.loads(result_json)
1876+
assert result["match_count"] == 1
1877+
row_data = result["matches"][0]["row_data"]
1878+
# nullセルは除外される(A1とC1のみ)
1879+
coords = [c["coordinate"] for c in row_data]
1880+
assert "A1" in coords
1881+
assert "C1" in coords
1882+
assert "B1" not in coords
1883+
1884+
def test_search_cells_include_row_data_multiple_sheets(self):
1885+
"""複数シート検索時にinclude_row_dataが正しく動作すること"""
1886+
excel_bytes = self._create_multi_sheet_excel()
1887+
self.mock_download_client.download_file.return_value = excel_bytes
1888+
1889+
parser = SharePointExcelParser(self.mock_download_client)
1890+
result_json = parser.search_cells(
1891+
"/test/file.xlsx", "Data", include_row_data=True
1892+
)
1893+
1894+
result = json.loads(result_json)
1895+
assert result["match_count"] == 2
1896+
1897+
for match in result["matches"]:
1898+
assert "row_data" in match
1899+
assert len(match["row_data"]) >= 1

0 commit comments

Comments
 (0)