fixed most of the tests

This commit is contained in:
Jonas Winkler 2020-11-02 19:42:23 +01:00
parent 3a08a2d206
commit f182709fdd
3 changed files with 22 additions and 52 deletions

View File

@ -25,20 +25,20 @@ class TestPaperlessLog(TestCase):
# Debug messages are ignored by default # Debug messages are ignored by default
self.logger.debug("This is a debugging message", extra=kw) self.logger.debug("This is a debugging message", extra=kw)
self.assertEqual(Log.objects.all().count(), 0)
self.logger.info("This is an informational message", extra=kw)
self.assertEqual(Log.objects.all().count(), 1) self.assertEqual(Log.objects.all().count(), 1)
self.logger.warning("This is an warning message", extra=kw) self.logger.info("This is an informational message", extra=kw)
self.assertEqual(Log.objects.all().count(), 2) self.assertEqual(Log.objects.all().count(), 2)
self.logger.error("This is an error message", extra=kw) self.logger.warning("This is an warning message", extra=kw)
self.assertEqual(Log.objects.all().count(), 3) self.assertEqual(Log.objects.all().count(), 3)
self.logger.critical("This is a critical message", extra=kw) self.logger.error("This is an error message", extra=kw)
self.assertEqual(Log.objects.all().count(), 4) self.assertEqual(Log.objects.all().count(), 4)
self.logger.critical("This is a critical message", extra=kw)
self.assertEqual(Log.objects.all().count(), 5)
def test_groups(self): def test_groups(self):
kw1 = {"group": uuid.uuid4()} kw1 = {"group": uuid.uuid4()}
@ -48,10 +48,6 @@ class TestPaperlessLog(TestCase):
with mock.patch("logging.StreamHandler.emit") as __: with mock.patch("logging.StreamHandler.emit") as __:
# Debug messages are ignored by default
self.logger.debug("This is a debugging message", extra=kw1)
self.assertEqual(Log.objects.all().count(), 0)
self.logger.info("This is an informational message", extra=kw2) self.logger.info("This is an informational message", extra=kw2)
self.assertEqual(Log.objects.all().count(), 1) self.assertEqual(Log.objects.all().count(), 1)
self.assertEqual(Log.objects.filter(group=kw2["group"]).count(), 1) self.assertEqual(Log.objects.filter(group=kw2["group"]).count(), 1)
@ -67,18 +63,3 @@ class TestPaperlessLog(TestCase):
self.logger.critical("This is a critical message", extra=kw1) self.logger.critical("This is a critical message", extra=kw1)
self.assertEqual(Log.objects.all().count(), 4) self.assertEqual(Log.objects.all().count(), 4)
self.assertEqual(Log.objects.filter(group=kw1["group"]).count(), 2) self.assertEqual(Log.objects.filter(group=kw1["group"]).count(), 2)
def test_groupped_query(self):
kw = {"group": uuid.uuid4()}
with mock.patch("logging.StreamHandler.emit") as __:
self.logger.info("Message 0", extra=kw)
self.logger.info("Message 1", extra=kw)
self.logger.info("Message 2", extra=kw)
self.logger.info("Message 3", extra=kw)
self.assertEqual(Log.objects.all().by_group().count(), 1)
self.assertEqual(
Log.objects.all().by_group()[0]["messages"],
"Message 0\nMessage 1\nMessage 2\nMessage 3"
)

View File

@ -5,7 +5,7 @@ from unittest import mock
from uuid import uuid4 from uuid import uuid4
from dateutil import tz from dateutil import tz
from django.test import TestCase from django.test import TestCase, override_settings
from ..parsers import RasterisedDocumentParser from ..parsers import RasterisedDocumentParser
from django.conf import settings from django.conf import settings
@ -16,36 +16,34 @@ class TestDate(TestCase):
SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples") SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")
SCRATCH = "/tmp/paperless-tests-{}".format(str(uuid4())[:8]) SCRATCH = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])
MOCK_SCRATCH = "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH" # NOQA: E501
def setUp(self): def setUp(self):
os.makedirs(self.SCRATCH, exist_ok=True) os.makedirs(self.SCRATCH, exist_ok=True)
def tearDown(self): def tearDown(self):
shutil.rmtree(self.SCRATCH) shutil.rmtree(self.SCRATCH)
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_1(self): def test_date_format_1(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
document._text = "lorem ipsum 130218 lorem ipsum" document._text = "lorem ipsum 130218 lorem ipsum"
self.assertEqual(document.get_date(), None) self.assertEqual(document.get_date(), None)
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_2(self): def test_date_format_2(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
document._text = "lorem ipsum 2018 lorem ipsum" document._text = "lorem ipsum 2018 lorem ipsum"
self.assertEqual(document.get_date(), None) self.assertEqual(document.get_date(), None)
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_3(self): def test_date_format_3(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
document._text = "lorem ipsum 20180213 lorem ipsum" document._text = "lorem ipsum 20180213 lorem ipsum"
self.assertEqual(document.get_date(), None) self.assertEqual(document.get_date(), None)
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_4(self): def test_date_format_4(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
@ -59,7 +57,7 @@ class TestDate(TestCase):
) )
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_5(self): def test_date_format_5(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
@ -76,7 +74,7 @@ class TestDate(TestCase):
) )
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_6(self): def test_date_format_6(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
@ -93,7 +91,7 @@ class TestDate(TestCase):
) )
self.assertEqual(document.get_date(), None) self.assertEqual(document.get_date(), None)
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_7(self): def test_date_format_7(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
@ -111,7 +109,7 @@ class TestDate(TestCase):
) )
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_8(self): def test_date_format_8(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
@ -135,7 +133,7 @@ class TestDate(TestCase):
) )
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_date_format_9(self): def test_date_format_9(self):
input_file = os.path.join(self.SAMPLE_FILES, "") input_file = os.path.join(self.SAMPLE_FILES, "")
document = RasterisedDocumentParser(input_file, None) document = RasterisedDocumentParser(input_file, None)
@ -157,7 +155,7 @@ class TestDate(TestCase):
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text", "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
return_value="01-07-0590 00:00:00" return_value="01-07-0590 00:00:00"
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_crazy_date_past(self, *args): def test_crazy_date_past(self, *args):
document = RasterisedDocumentParser("/dev/null", None) document = RasterisedDocumentParser("/dev/null", None)
document.get_text() document.get_text()
@ -167,7 +165,7 @@ class TestDate(TestCase):
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text", "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
return_value="01-07-2350 00:00:00" return_value="01-07-2350 00:00:00"
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_crazy_date_future(self, *args): def test_crazy_date_future(self, *args):
document = RasterisedDocumentParser("/dev/null", None) document = RasterisedDocumentParser("/dev/null", None)
document.get_text() document.get_text()
@ -177,7 +175,7 @@ class TestDate(TestCase):
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text", "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
return_value="20 408000l 2475" return_value="20 408000l 2475"
) )
@mock.patch(MOCK_SCRATCH, SCRATCH) @override_settings(SCRATCH_DIR=SCRATCH)
def test_crazy_date_with_spaces(self, *args): def test_crazy_date_with_spaces(self, *args):
document = RasterisedDocumentParser("/dev/null", None) document = RasterisedDocumentParser("/dev/null", None)
document.get_text() document.get_text()
@ -187,13 +185,8 @@ class TestDate(TestCase):
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text", "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
return_value="No date in here" return_value="No date in here"
) )
@mock.patch( @override_settings(FILENAME_DATE_ORDER="YMD")
"paperless_tesseract.parsers.RasterisedDocumentParser." @override_settings(SCRATCH_DIR=SCRATCH)
"FILENAME_DATE_ORDER",
new_callable=mock.PropertyMock,
return_value="YMD"
)
@mock.patch(MOCK_SCRATCH, SCRATCH)
def test_filename_date_parse_invalid(self, *args): def test_filename_date_parse_invalid(self, *args):
document = RasterisedDocumentParser("/tmp/20 408000l 2475 - test.pdf", None) document = RasterisedDocumentParser("/tmp/20 408000l 2475 - test.pdf", None)
document.get_text() document.get_text()

View File

@ -62,10 +62,6 @@ class TestOCR(TestCase):
) )
@skipIf(not TESSERACT_INSTALLED, "Tesseract not installed. Skipping") @skipIf(not TESSERACT_INSTALLED, "Tesseract not installed. Skipping")
@mock.patch(
"paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",
SAMPLE_FILES
)
@mock.patch("paperless_tesseract.parsers.pyocr", FakePyOcr) @mock.patch("paperless_tesseract.parsers.pyocr", FakePyOcr)
def test_image_to_string_with_text_free_page(self): def test_image_to_string_with_text_free_page(self):
""" """
@ -77,4 +73,4 @@ class TestOCR(TestCase):
text-free pages are now handled correctly so long as we work around text-free pages are now handled correctly so long as we work around
this weird exception. this weird exception.
""" """
image_to_string(["no-text.png", "en"]) image_to_string([os.path.join(self.SAMPLE_FILES, "no-text.png"), "en"])