2018-02-03 14:49:48 +00:00
|
|
|
import datetime
|
2018-02-02 23:37:20 +01:00
|
|
|
import os
|
2018-02-03 14:49:48 +00:00
|
|
|
import shutil
|
|
|
|
|
from unittest import mock
|
|
|
|
|
from uuid import uuid4
|
2018-02-02 23:37:20 +01:00
|
|
|
|
2018-02-03 14:49:48 +00:00
|
|
|
from dateutil import tz
|
2018-12-30 17:32:45 +00:00
|
|
|
from django.test import TestCase
|
2018-02-02 23:37:20 +01:00
|
|
|
|
|
|
|
|
from ..parsers import RasterisedDocumentParser
|
2018-11-15 20:30:23 -05:00
|
|
|
from django.conf import settings
|
2018-02-02 23:37:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestDate(TestCase):
|
2018-02-03 14:49:48 +00:00
|
|
|
|
2018-02-02 23:37:20 +01:00
|
|
|
SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")
|
2018-02-03 14:49:48 +00:00
|
|
|
SCRATCH = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])
|
|
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
MOCK_SCRATCH = "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH" # NOQA: E501
|
|
|
|
|
|
2018-02-03 14:49:48 +00:00
|
|
|
def setUp(self):
|
|
|
|
|
os.makedirs(self.SCRATCH, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
shutil.rmtree(self.SCRATCH)
|
2018-02-02 23:37:20 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_1(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = "lorem ipsum 130218 lorem ipsum"
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(document.get_date(), None)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_2(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = "lorem ipsum 2018 lorem ipsum"
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(document.get_date(), None)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_3(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = "lorem ipsum 20180213 lorem ipsum"
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(document.get_date(), None)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_4(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = "lorem ipsum 13.02.2018 lorem ipsum"
|
2018-11-15 20:30:23 -05:00
|
|
|
date = document.get_date()
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(
|
2018-11-15 20:30:23 -05:00
|
|
|
date,
|
2018-12-01 17:09:12 +00:00
|
|
|
datetime.datetime(
|
|
|
|
|
2018, 2, 13, 0, 0,
|
|
|
|
|
tzinfo=tz.gettz(settings.TIME_ZONE)
|
|
|
|
|
)
|
2018-02-18 18:00:34 +00:00
|
|
|
)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_5(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = (
|
2018-11-15 20:30:23 -05:00
|
|
|
"lorem ipsum 130218, 2018, 20180213 and lorem 13.02.2018 lorem "
|
2018-12-01 17:09:12 +00:00
|
|
|
"ipsum"
|
|
|
|
|
)
|
2018-11-15 20:30:23 -05:00
|
|
|
date = document.get_date()
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(
|
2018-11-15 20:30:23 -05:00
|
|
|
date,
|
2018-12-01 17:09:12 +00:00
|
|
|
datetime.datetime(
|
|
|
|
|
2018, 2, 13, 0, 0,
|
|
|
|
|
tzinfo=tz.gettz(settings.TIME_ZONE)
|
|
|
|
|
)
|
2018-02-18 18:00:34 +00:00
|
|
|
)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_6(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = (
|
|
|
|
|
"lorem ipsum\n"
|
|
|
|
|
"Wohnort\n"
|
|
|
|
|
"3100\n"
|
|
|
|
|
"IBAN\n"
|
|
|
|
|
"AT87 4534\n"
|
|
|
|
|
"1234\n"
|
|
|
|
|
"1234 5678\n"
|
|
|
|
|
"BIC\n"
|
|
|
|
|
"lorem ipsum"
|
|
|
|
|
)
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(document.get_date(), None)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-13 22:46:44 +01:00
|
|
|
def test_date_format_7(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-02-18 18:00:22 +00:00
|
|
|
document._text = (
|
|
|
|
|
"lorem ipsum\n"
|
|
|
|
|
"März 2019\n"
|
|
|
|
|
"lorem ipsum"
|
|
|
|
|
)
|
2018-11-15 20:30:23 -05:00
|
|
|
date = document.get_date()
|
2018-02-18 18:00:34 +00:00
|
|
|
self.assertEqual(
|
2018-11-15 20:30:23 -05:00
|
|
|
date,
|
2018-12-01 17:09:12 +00:00
|
|
|
datetime.datetime(
|
|
|
|
|
2019, 3, 1, 0, 0,
|
|
|
|
|
tzinfo=tz.gettz(settings.TIME_ZONE)
|
|
|
|
|
)
|
2018-02-18 18:00:34 +00:00
|
|
|
)
|
2018-02-13 22:46:44 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-18 22:03:20 +01:00
|
|
|
def test_date_format_8(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-12-01 17:09:12 +00:00
|
|
|
document._text = (
|
|
|
|
|
"lorem ipsum\n"
|
|
|
|
|
"Wohnort\n"
|
|
|
|
|
"3100\n"
|
|
|
|
|
"IBAN\n"
|
|
|
|
|
"AT87 4534\n"
|
|
|
|
|
"1234\n"
|
|
|
|
|
"1234 5678\n"
|
|
|
|
|
"BIC\n"
|
|
|
|
|
"lorem ipsum\n"
|
|
|
|
|
"März 2020"
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
document.get_date(),
|
|
|
|
|
datetime.datetime(
|
|
|
|
|
2020, 3, 1, 0, 0,
|
|
|
|
|
tzinfo=tz.gettz(settings.TIME_ZONE)
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-02-18 22:03:20 +01:00
|
|
|
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-02-18 22:03:20 +01:00
|
|
|
def test_date_format_9(self):
|
|
|
|
|
input_file = os.path.join(self.SAMPLE_FILES, "")
|
|
|
|
|
document = RasterisedDocumentParser(input_file)
|
2018-12-01 17:09:12 +00:00
|
|
|
document._text = (
|
|
|
|
|
"lorem ipsum\n"
|
|
|
|
|
"27. Nullmonth 2020\n"
|
|
|
|
|
"März 2020\n"
|
|
|
|
|
"lorem ipsum"
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
document.get_date(),
|
|
|
|
|
datetime.datetime(
|
|
|
|
|
2020, 3, 1, 0, 0,
|
|
|
|
|
tzinfo=tz.gettz(settings.TIME_ZONE)
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-02-18 22:03:20 +01:00
|
|
|
|
2018-10-01 20:03:27 +01:00
|
|
|
@mock.patch(
|
|
|
|
|
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
|
|
|
|
|
return_value="01-07-0590 00:00:00"
|
|
|
|
|
)
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-10-07 14:48:49 +01:00
|
|
|
def test_crazy_date_past(self, *args):
|
|
|
|
|
document = RasterisedDocumentParser("/dev/null")
|
|
|
|
|
document.get_text()
|
|
|
|
|
self.assertIsNone(document.get_date())
|
|
|
|
|
|
|
|
|
|
@mock.patch(
|
|
|
|
|
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
|
|
|
|
|
return_value="01-07-2350 00:00:00"
|
|
|
|
|
)
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-10-07 14:48:49 +01:00
|
|
|
def test_crazy_date_future(self, *args):
|
|
|
|
|
document = RasterisedDocumentParser("/dev/null")
|
|
|
|
|
document.get_text()
|
|
|
|
|
self.assertIsNone(document.get_date())
|
|
|
|
|
|
|
|
|
|
@mock.patch(
|
|
|
|
|
"paperless_tesseract.parsers.RasterisedDocumentParser.get_text",
|
|
|
|
|
return_value="01-07-0590 00:00:00"
|
|
|
|
|
)
|
2018-12-30 17:32:45 +00:00
|
|
|
@mock.patch(MOCK_SCRATCH, SCRATCH)
|
2018-10-07 14:48:49 +01:00
|
|
|
def test_crazy_date_past(self, *args):
|
2018-10-01 20:03:27 +01:00
|
|
|
document = RasterisedDocumentParser("/dev/null")
|
|
|
|
|
document.get_text()
|
|
|
|
|
self.assertIsNone(document.get_date())
|