mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Chore: Initial conversion to pytest fixtures (#7110)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from django.conf import settings
|
||||
from PIL import Image
|
||||
@@ -15,7 +15,7 @@ class TextDocumentParser(DocumentParser):
|
||||
|
||||
logging_name = "paperless.parsing.text"
|
||||
|
||||
def get_thumbnail(self, document_path, mime_type, file_name=None):
|
||||
def get_thumbnail(self, document_path: Path, mime_type, file_name=None) -> Path:
|
||||
text = self.read_file_handle_unicode_errors(document_path)
|
||||
|
||||
img = Image.new("RGB", (500, 700), color="white")
|
||||
@@ -27,7 +27,7 @@ class TextDocumentParser(DocumentParser):
|
||||
)
|
||||
draw.text((5, 5), text, font=font, fill="black")
|
||||
|
||||
out_path = os.path.join(self.tempdir, "thumb.webp")
|
||||
out_path = self.tempdir / "thumb.webp"
|
||||
img.save(out_path, format="WEBP")
|
||||
|
||||
return out_path
|
||||
|
30
src/paperless_text/tests/conftest.py
Normal file
30
src/paperless_text/tests/conftest.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from collections.abc import Generator
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from paperless_text.parsers import TextDocumentParser
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def sample_dir() -> Path:
|
||||
return (Path(__file__).parent / Path("samples")).resolve()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def text_parser() -> Generator[TextDocumentParser, None, None]:
|
||||
try:
|
||||
parser = TextDocumentParser(logging_group=None)
|
||||
yield parser
|
||||
finally:
|
||||
parser.cleanup()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def sample_txt_file(sample_dir: Path) -> Path:
|
||||
return sample_dir / "test.txt"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def malformed_txt_file(sample_dir: Path) -> Path:
|
||||
return sample_dir / "decode_error.txt"
|
@@ -1,37 +1,26 @@
|
||||
from pathlib import Path
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import FileSystemAssertsMixin
|
||||
from paperless_text.parsers import TextDocumentParser
|
||||
|
||||
|
||||
class TestTextParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
SAMPLE_DIR = Path(__file__).resolve().parent / "samples"
|
||||
|
||||
def test_thumbnail(self):
|
||||
parser = TextDocumentParser(None)
|
||||
|
||||
class TestTextParser:
|
||||
def test_thumbnail(self, text_parser: TextDocumentParser, sample_txt_file: Path):
|
||||
# just make sure that it does not crash
|
||||
f = parser.get_thumbnail(
|
||||
self.SAMPLE_DIR / "test.txt",
|
||||
"text/plain",
|
||||
)
|
||||
self.assertIsFile(f)
|
||||
f = text_parser.get_thumbnail(sample_txt_file, "text/plain")
|
||||
assert f.exists()
|
||||
assert f.is_file()
|
||||
|
||||
def test_parse(self):
|
||||
parser = TextDocumentParser(None)
|
||||
def test_parse(self, text_parser: TextDocumentParser, sample_txt_file: Path):
|
||||
text_parser.parse(sample_txt_file, "text/plain")
|
||||
|
||||
parser.parse(
|
||||
self.SAMPLE_DIR / "test.txt",
|
||||
"text/plain",
|
||||
)
|
||||
assert text_parser.get_text() == "This is a test file.\n"
|
||||
assert text_parser.get_archive_path() is None
|
||||
|
||||
self.assertEqual(parser.get_text(), "This is a test file.\n")
|
||||
self.assertIsNone(parser.get_archive_path())
|
||||
|
||||
def test_parse_invalid_bytes(self):
|
||||
def test_parse_invalid_bytes(
|
||||
self,
|
||||
text_parser: TextDocumentParser,
|
||||
malformed_txt_file: Path,
|
||||
):
|
||||
"""
|
||||
GIVEN:
|
||||
- Text file which contains invalid UTF bytes
|
||||
@@ -41,12 +30,8 @@ class TestTextParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
- Parsing continues
|
||||
- Invalid bytes are removed
|
||||
"""
|
||||
parser = TextDocumentParser(None)
|
||||
|
||||
parser.parse(
|
||||
self.SAMPLE_DIR / "decode_error.txt",
|
||||
"text/plain",
|
||||
)
|
||||
text_parser.parse(malformed_txt_file, "text/plain")
|
||||
|
||||
self.assertEqual(parser.get_text(), "Pantothens<EFBFBD>ure\n")
|
||||
self.assertIsNone(parser.get_archive_path())
|
||||
assert text_parser.get_text() == "Pantothens<EFBFBD>ure\n"
|
||||
assert text_parser.get_archive_path() is None
|
||||
|
Reference in New Issue
Block a user