Skip to content

Commit 4c70616

Browse files
committed
gen: #file:csv_loaders.py のテストコードを #file:test_csv_loaders.py に pytest フォーマットで書いて
1 parent 2f3bae6 commit 4c70616

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

tests/utilities/__init__.py

Whitespace-only changes.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import os
2+
import tempfile
3+
from pathlib import Path
4+
from unittest.mock import patch
5+
6+
from langchain_core.documents import Document
7+
8+
from template_langgraph.utilities.csv_loaders import (
9+
CsvLoaderWrapper,
10+
Settings,
11+
get_csv_loader_settings,
12+
)
13+
14+
15+
class TestSettings:
16+
"""Test cases for Settings class."""
17+
18+
def test_default_settings(self):
19+
"""Test default settings values."""
20+
settings = Settings()
21+
assert settings.csv_loader_data_dir_path == "./data"
22+
23+
def test_custom_settings(self):
24+
"""Test custom settings values."""
25+
settings = Settings(csv_loader_data_dir_path="/custom/path")
26+
assert settings.csv_loader_data_dir_path == "/custom/path"
27+
28+
@patch.dict(os.environ, {"CSV_LOADER_DATA_DIR_PATH": "/env/path"})
29+
def test_env_settings(self):
30+
"""Test settings from environment variables."""
31+
settings = Settings()
32+
assert settings.csv_loader_data_dir_path == "/env/path"
33+
34+
35+
class TestGetCsvLoaderSettings:
36+
"""Test cases for get_csv_loader_settings function."""
37+
38+
def test_get_csv_loader_settings_returns_settings_instance(self):
39+
"""Test that get_csv_loader_settings returns a Settings instance."""
40+
settings = get_csv_loader_settings()
41+
assert isinstance(settings, Settings)
42+
43+
def test_get_csv_loader_settings_cached(self):
44+
"""Test that get_csv_loader_settings is cached."""
45+
settings1 = get_csv_loader_settings()
46+
settings2 = get_csv_loader_settings()
47+
assert settings1 is settings2
48+
49+
50+
class TestCsvLoaderWrapper:
51+
"""Test cases for CsvLoaderWrapper class."""
52+
53+
def test_init_with_default_settings(self):
54+
"""Test initialization with default settings."""
55+
wrapper = CsvLoaderWrapper()
56+
assert isinstance(wrapper.settings, Settings)
57+
assert wrapper.settings.csv_loader_data_dir_path == "./data"
58+
59+
def test_init_with_custom_settings(self):
60+
"""Test initialization with custom settings."""
61+
custom_settings = Settings(csv_loader_data_dir_path="/custom/path")
62+
wrapper = CsvLoaderWrapper(settings=custom_settings)
63+
assert wrapper.settings is custom_settings
64+
assert wrapper.settings.csv_loader_data_dir_path == "/custom/path"
65+
66+
def test_load_csv_docs_empty_directory(self):
67+
"""Test loading CSV documents from empty directory."""
68+
with tempfile.TemporaryDirectory() as temp_dir:
69+
settings = Settings(csv_loader_data_dir_path=temp_dir)
70+
wrapper = CsvLoaderWrapper(settings=settings)
71+
docs = wrapper.load_csv_docs()
72+
assert docs == []
73+
74+
def test_load_csv_docs_with_csv_files(self):
75+
"""Test loading CSV documents from directory with CSV files."""
76+
with tempfile.TemporaryDirectory() as temp_dir:
77+
# Create test CSV files
78+
csv_file1 = Path(temp_dir) / "test1.csv"
79+
csv_file1.write_text("name,age\nAlice,25\nBob,30\n")
80+
81+
csv_file2 = Path(temp_dir) / "test2.csv"
82+
csv_file2.write_text("city,country\nTokyo,Japan\nParis,France\n")
83+
84+
settings = Settings(csv_loader_data_dir_path=temp_dir)
85+
wrapper = CsvLoaderWrapper(settings=settings)
86+
docs = wrapper.load_csv_docs()
87+
88+
assert len(docs) == 4 # 2 rows from each CSV file
89+
assert all(isinstance(doc, Document) for doc in docs)
90+
91+
# Check that documents contain expected content
92+
doc_contents = [doc.page_content for doc in docs]
93+
assert any("Alice" in content for content in doc_contents)
94+
assert any("Bob" in content for content in doc_contents)
95+
assert any("Tokyo" in content for content in doc_contents)
96+
assert any("Paris" in content for content in doc_contents)
97+
98+
def test_load_csv_docs_with_nested_directories(self):
99+
"""Test loading CSV documents from nested directories."""
100+
with tempfile.TemporaryDirectory() as temp_dir:
101+
# Create nested directory structure
102+
nested_dir = Path(temp_dir) / "nested"
103+
nested_dir.mkdir()
104+
105+
# Create CSV file in nested directory
106+
csv_file = nested_dir / "nested_test.csv"
107+
csv_file.write_text("product,price\nApple,100\nBanana,80\n")
108+
109+
settings = Settings(csv_loader_data_dir_path=temp_dir)
110+
wrapper = CsvLoaderWrapper(settings=settings)
111+
docs = wrapper.load_csv_docs()
112+
113+
assert len(docs) == 2
114+
assert all(isinstance(doc, Document) for doc in docs)
115+
116+
# Check that documents contain expected content
117+
doc_contents = [doc.page_content for doc in docs]
118+
assert any("Apple" in content for content in doc_contents)
119+
assert any("Banana" in content for content in doc_contents)
120+
121+
def test_load_csv_docs_with_non_csv_files(self):
122+
"""Test loading CSV documents ignores non-CSV files."""
123+
with tempfile.TemporaryDirectory() as temp_dir:
124+
# Create CSV file
125+
csv_file = Path(temp_dir) / "test.csv"
126+
csv_file.write_text("name,value\ntest,123\n")
127+
128+
# Create non-CSV files that should be ignored
129+
txt_file = Path(temp_dir) / "test.txt"
130+
txt_file.write_text("This should be ignored")
131+
132+
json_file = Path(temp_dir) / "test.json"
133+
json_file.write_text('{"key": "value"}')
134+
135+
settings = Settings(csv_loader_data_dir_path=temp_dir)
136+
wrapper = CsvLoaderWrapper(settings=settings)
137+
docs = wrapper.load_csv_docs()
138+
139+
assert len(docs) == 1 # Only the CSV file should be loaded
140+
assert "test" in docs[0].page_content
141+
assert "123" in docs[0].page_content
142+
143+
def test_load_csv_docs_nonexistent_directory(self):
144+
"""Test loading CSV documents from nonexistent directory."""
145+
settings = Settings(csv_loader_data_dir_path="/nonexistent/path")
146+
wrapper = CsvLoaderWrapper(settings=settings)
147+
docs = wrapper.load_csv_docs()
148+
assert docs == []
149+
150+
def test_load_csv_docs_with_malformed_csv(self):
151+
"""Test behavior with malformed CSV files."""
152+
with tempfile.TemporaryDirectory() as temp_dir:
153+
# Create malformed CSV file
154+
csv_file = Path(temp_dir) / "malformed.csv"
155+
csv_file.write_text("header1,header2\nvalue1\nvalue2,value3,extra\n")
156+
157+
settings = Settings(csv_loader_data_dir_path=temp_dir)
158+
wrapper = CsvLoaderWrapper(settings=settings)
159+
160+
# CSVLoader should handle malformed CSV gracefully
161+
docs = wrapper.load_csv_docs()
162+
assert len(docs) >= 0 # Should not crash, but may return empty or partial results

0 commit comments

Comments
 (0)