Skip to content

Commit 8203084

Browse files
feat: Add basic pytest setup with initial tests for wdoc module
1 parent ae0bb6d commit 8203084

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Enable pytest to import from this directory

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
from pathlib import Path
3+
import tempfile
4+
5+
@pytest.fixture
6+
def temp_dir():
7+
"""Provide a temporary directory that is cleaned up after the test."""
8+
with tempfile.TemporaryDirectory() as tmpdirname:
9+
yield Path(tmpdirname)
10+
11+
@pytest.fixture
12+
def sample_text_file(temp_dir):
13+
"""Create a sample text file for testing."""
14+
file_path = temp_dir / "sample.txt"
15+
with open(file_path, "w") as f:
16+
f.write("This is a test document.\nIt has multiple lines.\nFor testing purposes.")
17+
return file_path
18+
19+
@pytest.fixture
20+
def sample_pdf_file(temp_dir):
21+
"""Create a sample PDF file path for testing."""
22+
return temp_dir / "sample.pdf"

tests/test_wdoc.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import pytest
2+
from pathlib import Path
3+
from wdoc.wdoc import wdoc
4+
5+
def test_wdoc_version():
6+
"""Test that wdoc has a valid version string."""
7+
assert isinstance(wdoc.VERSION, str)
8+
assert len(wdoc.VERSION.split(".")) == 3
9+
10+
def test_fail_parse_small_file_text(sample_text_file):
11+
"""Test that a too small text file parsing fails."""
12+
# should fail because the file is too small
13+
with pytest.raises(Exception):
14+
wdoc.parse_file(
15+
path=str(sample_text_file),
16+
filetype="txt",
17+
debug=False,
18+
verbose=False
19+
)
20+
21+
def test_parse_file_text(sample_text_file):
22+
"""Test basic text file parsing."""
23+
# make a bigger text file
24+
f = Path(sample_text_file)
25+
content = f.read_text()
26+
f.write_text(50 * (content + "\n"))
27+
docs = wdoc.parse_file(
28+
path=str(sample_text_file),
29+
filetype="txt",
30+
debug=False,
31+
verbose=False
32+
)
33+
assert len(docs) > 0
34+
assert docs[0].page_content.startswith("This is a test document")
35+
assert "multiple lines" in docs[0].page_content
36+
37+
def test_parse_file_only_text(sample_text_file):
38+
"""Test text-only output from parse_file."""
39+
f = Path(sample_text_file)
40+
content = f.read_text()
41+
f.write_text(50 * (content + "\n"))
42+
text = wdoc.parse_file(
43+
path=str(sample_text_file),
44+
filetype="txt",
45+
only_text=True,
46+
debug=False,
47+
verbose=False
48+
)
49+
assert isinstance(text, str)
50+
assert text.startswith("This is a test document")
51+
assert "multiple lines" in text
52+
53+
def test_invalid_filetype():
54+
"""Test that invalid filetype raises an error."""
55+
with pytest.raises(Exception):
56+
wdoc.parse_file(
57+
path="dummy.txt",
58+
filetype="invalid_type",
59+
debug=False,
60+
verbose=False
61+
)

0 commit comments

Comments
 (0)