diff --git a/src/paperless_tesseract/tests/samples/tests_date_1.pdf b/src/paperless_tesseract/tests/samples/tests_date_1.pdf new file mode 100644 index 000000000..629125956 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_1.pdf differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_1.png b/src/paperless_tesseract/tests/samples/tests_date_1.png new file mode 100644 index 000000000..4a7671635 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_1.png differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_2.pdf b/src/paperless_tesseract/tests/samples/tests_date_2.pdf new file mode 100644 index 000000000..ae51a950c Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_2.pdf differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_2.png b/src/paperless_tesseract/tests/samples/tests_date_2.png new file mode 100644 index 000000000..f01bdfed1 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_2.png differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_3.pdf b/src/paperless_tesseract/tests/samples/tests_date_3.pdf new file mode 100644 index 000000000..0270ae097 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_3.pdf differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_3.png b/src/paperless_tesseract/tests/samples/tests_date_3.png new file mode 100644 index 000000000..7af752cbc Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_3.png differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_4.pdf b/src/paperless_tesseract/tests/samples/tests_date_4.pdf new file mode 100644 index 000000000..e235ad215 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_4.pdf differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_4.png b/src/paperless_tesseract/tests/samples/tests_date_4.png new file mode 100644 index 000000000..c76a55dfb Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_4.png differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_5.pdf b/src/paperless_tesseract/tests/samples/tests_date_5.pdf new file mode 100644 index 000000000..717ea4ef4 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_5.pdf differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_5.png b/src/paperless_tesseract/tests/samples/tests_date_5.png new file mode 100644 index 000000000..97afeec01 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_5.png differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_6.pdf b/src/paperless_tesseract/tests/samples/tests_date_6.pdf new file mode 100644 index 000000000..bda2dc280 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_6.pdf differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_6.png b/src/paperless_tesseract/tests/samples/tests_date_6.png new file mode 100644 index 000000000..425105d35 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_6.png differ diff --git a/src/paperless_tesseract/tests/samples/tests_date_7.pdf b/src/paperless_tesseract/tests/samples/tests_date_7.pdf new file mode 100644 index 000000000..c10f66883 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/tests_date_7.pdf differ diff --git a/src/paperless_tesseract/tests/test_date.py b/src/paperless_tesseract/tests/test_date.py new file mode 100644 index 000000000..019d5199a --- /dev/null +++ b/src/paperless_tesseract/tests/test_date.py @@ -0,0 +1,206 @@ +import os +from unittest import skipIf, mock + +import pyocr +from django.test import TestCase + +from ..parsers import RasterisedDocumentParser +import datetime +from dateutil import tz + + +class TestDate(TestCase): + SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples") + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_1_pdf(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_1.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 4, 1, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_1_png(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_1.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 4, 1, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_2_pdf(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_2.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2013, 2, 1, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_2_png(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_2.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), + datetime.datetime(2013, 2, 1, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_3_pdf(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_3.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 10, 5, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_3_png(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_3.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 10, 5, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_4_pdf(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_4.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 10, 5, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_4_png(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_4.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 10, 5, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_5_pdf(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_5.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 12, 17, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_5_png(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_5.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 12, 17, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_6_pdf_us(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_6.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + document.DATE_ORDER = "MDY" + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 12, 17, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_6_png_us(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_6.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + document.DATE_ORDER = "MDY" + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 12, 17, 0, 0, + tzinfo=tz.tzutc())) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_6_pdf_eu(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_6.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), None) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_6_png_eu(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_6.png") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), False) + self.assertEqual(document.get_date(), None) + + @mock.patch( + "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH", + SAMPLE_FILES + ) + def test_get_text_7_pdf(self): + input_file = os.path.join(self.SAMPLE_FILES, "tests_date_7.pdf") + document = RasterisedDocumentParser(input_file) + document.get_text() + self.assertEqual(document._is_ocred(), True) + self.assertEqual(document.get_date(), + datetime.datetime(2018, 4, 1, 0, 0, + tzinfo=tz.tzutc()))