Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweaks in test pdf ingestor #38

Closed
wants to merge 1 commit into from
Closed
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: 4 additions & 2 deletions querent/ingestors/pdfs/pdf_ingestor_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ async def ingest(
if current_file:
# Process the collected bytes of the previous file
text = await self.extract_and_process_pdf(
CollectedBytes(file=current_file, data=collected_bytes)
CollectedBytes(file=current_file,
data=collected_bytes)
)
yield text
collected_bytes = b"" # Reset collected bytes for the new file
Expand All @@ -56,6 +57,7 @@ async def ingest(
yield text

except Exception as e:
print(e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do error handling later

yield []

async def extract_and_process_pdf(self, collected_bytes: CollectedBytes) -> List[str]:
Expand All @@ -66,7 +68,7 @@ async def extract_text_from_pdf(self, collected_bytes: CollectedBytes) -> str:
pdf = fitz.open(stream=collected_bytes.data, filetype="pdf")
text = ""
for page in pdf:
text += page.getText()
text += page.get_text()
return text

async def process_data(self, text: str) -> List[str]:
Expand Down
15 changes: 9 additions & 6 deletions tests/test_pdf_ingestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,33 @@
from querent.ingestors.ingestor_manager import IngestorFactoryManager
import pytest


@pytest.mark.asyncio
async def test_collect_and_ingest_pdf():
# Set up the collector
collector_factory = FSCollectorFactory()
uri = Uri("file://" + str(Path("./tests/data/pdf/").resolve()))
config = FSCollectorConfig(root_path=uri.path)
collector = collector_factory.resolve(uri, config)

# Set up the ingestor
ingestor_factory_manager = IngestorFactoryManager()
ingestor_factory = await ingestor_factory_manager.get_factory("pdf") # Notice the use of await here
# Notice the use of await here
ingestor_factory = await ingestor_factory_manager.get_factory("pdf")
ingestor = await ingestor_factory.create("pdf", [])

# Collect and ingest the PDF
ingested_call = ingestor.ingest(collector.poll())
counter = 0

async def poll_and_print():
counter = 0
async for ingested in ingested_call:
assert ingested is not None
if len(ingested) == 0:
if len(ingested) == 0:
counter += 1
assert counter == 1
assert counter == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it should be 1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is read and we should get atleast one ingested collected result


await poll_and_print() # Notice the use of await here


Expand Down
Loading