Skip to content

Commit 13da1be

Browse files
authored
Merge pull request #2 from hasan-py/ci-cd-unit-test
test: ci unit test test added
2 parents 55154cf + e61cf19 commit 13da1be

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Unit Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: "3.10"
23+
24+
- name: Install dependencies
25+
run: pip install -r requirements.txt
26+
27+
- name: Run tests
28+
run: pytest

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/env
2-
/chat-with-pdf
2+
/chat-with-pdf
3+
/.pytest_cache
4+
/__pycache__

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ langchain_core
33
langchain_community
44
langchain_ollama
55
pdfplumber
6+
pytest
7+
langchain

test_pdf_rag.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import pytest
3+
import tempfile
4+
from unittest.mock import MagicMock, patch
5+
from pdf_rag import upload_pdf, load_pdf, split_text, index_docs, retrieve_docs, answer_question
6+
from langchain_core.documents import Document
7+
8+
@pytest.fixture
9+
def sample_pdf():
10+
"""Creates a temporary sample PDF file for testing."""
11+
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
12+
temp_pdf.write(b"%PDF-1.4 Sample PDF Content")
13+
temp_pdf.close()
14+
yield temp_pdf.name
15+
os.remove(temp_pdf.name)
16+
17+
18+
def test_upload_pdf(sample_pdf):
19+
"""Test the upload_pdf function."""
20+
mock_file = MagicMock()
21+
mock_file.name = "test.pdf"
22+
mock_file.getbuffer.return_value = open(sample_pdf, "rb").read()
23+
24+
saved_path = upload_pdf(mock_file)
25+
assert os.path.exists(saved_path)
26+
os.remove(saved_path)
27+
28+
29+
@patch("pdf_rag.PDFPlumberLoader.load")
30+
def test_load_pdf(mock_pdf_loader, sample_pdf):
31+
"""Test loading a PDF."""
32+
mock_pdf_loader.return_value = [Document(page_content="Sample text")]
33+
result = load_pdf(sample_pdf)
34+
assert isinstance(result, list)
35+
assert result[0].page_content == "Sample text"
36+
37+
38+
def test_split_text():
39+
"""Test text splitting."""
40+
documents = [Document(page_content="Sample text that needs splitting.")]
41+
chunks = split_text(documents)
42+
assert isinstance(chunks, list)
43+
assert len(chunks) > 0
44+
45+
46+
@patch("pdf_rag.InMemoryVectorStore.add_documents")
47+
def test_index_docs(mock_add_docs):
48+
"""Test document indexing."""
49+
documents = [Document(page_content="Indexed text")]
50+
index_docs(documents)
51+
mock_add_docs.assert_called_once()
52+
53+
54+
@patch("pdf_rag.InMemoryVectorStore.similarity_search")
55+
def test_retrieve_docs(mock_search):
56+
"""Test document retrieval."""
57+
mock_search.return_value = [Document(page_content="Relevant text")]
58+
result = retrieve_docs("test query")
59+
assert isinstance(result, list)
60+
assert len(result) > 0
61+
62+
63+
@patch("pdf_rag.OllamaLLM.invoke")
64+
def test_answer_question(mock_invoke):
65+
"""Test question answering."""
66+
mock_invoke.return_value = "This is an answer."
67+
docs = [Document(page_content="Context text")]
68+
result = answer_question("What is this?", docs)
69+
assert result == "This is an answer."

0 commit comments

Comments
 (0)