-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set valid=false for invalid backends (#171)
* fix: set valid=false for invalid backends Signed-off-by: Michele Dolfi <[email protected]> * Add test case for InputDocument Signed-off-by: Christoph Auer <[email protected]> --------- Signed-off-by: Michele Dolfi <[email protected]> Signed-off-by: Christoph Auer <[email protected]> Co-authored-by: Christoph Auer <[email protected]>
- Loading branch information
1 parent
b8d2286
commit 3496b48
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from io import BytesIO | ||
from pathlib import Path | ||
|
||
from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend | ||
from docling.datamodel.base_models import DocumentStream, InputFormat | ||
from docling.datamodel.document import InputDocument | ||
|
||
|
||
def test_in_doc_from_valid_path(): | ||
|
||
test_doc_path = Path("./tests/data/2206.01062.pdf") | ||
doc = _make_input_doc(test_doc_path) | ||
assert doc.valid == True | ||
|
||
|
||
def test_in_doc_from_invalid_path(): | ||
test_doc_path = Path("./tests/does/not/exist.pdf") | ||
|
||
doc = _make_input_doc(test_doc_path) | ||
|
||
assert doc.valid == False | ||
|
||
|
||
def test_in_doc_from_valid_buf(): | ||
|
||
buf = BytesIO(Path("./tests/data/2206.01062.pdf").open("rb").read()) | ||
stream = DocumentStream(name="my_doc.pdf", stream=buf) | ||
|
||
doc = _make_input_doc_from_stream(stream) | ||
assert doc.valid == True | ||
|
||
|
||
def test_in_doc_from_invalid_buf(): | ||
|
||
buf = BytesIO(b"") | ||
stream = DocumentStream(name="my_doc.pdf", stream=buf) | ||
|
||
doc = _make_input_doc_from_stream(stream) | ||
assert doc.valid == False | ||
|
||
|
||
def _make_input_doc(path): | ||
in_doc = InputDocument( | ||
path_or_stream=path, | ||
format=InputFormat.PDF, | ||
backend=PyPdfiumDocumentBackend, | ||
) | ||
return in_doc | ||
|
||
|
||
def _make_input_doc_from_stream(doc_stream): | ||
in_doc = InputDocument( | ||
path_or_stream=doc_stream.stream, | ||
format=InputFormat.PDF, | ||
filename=doc_stream.name, | ||
backend=PyPdfiumDocumentBackend, | ||
) | ||
return in_doc |