2016-02-11 12:25:23 +00:00
|
|
|
|
from django.test import TestCase
|
2017-03-28 21:01:50 +00:00
|
|
|
|
from unittest import mock
|
2018-02-25 19:42:03 +01:00
|
|
|
|
from tempfile import TemporaryDirectory
|
2016-02-11 12:25:23 +00:00
|
|
|
|
|
2017-03-28 21:01:50 +00:00
|
|
|
|
from ..consumer import Consumer
|
2016-08-16 19:13:37 +01:00
|
|
|
|
from ..models import FileInfo
|
2016-02-11 12:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-03-28 21:01:50 +00:00
|
|
|
|
class TestConsumer(TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
class DummyParser(object):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test__get_parser_class_1_parser(self):
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
self._get_consumer()._get_parser_class("doc.pdf"),
|
|
|
|
|
|
self.DummyParser
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@mock.patch("documents.consumer.os.makedirs")
|
|
|
|
|
|
@mock.patch("documents.consumer.os.path.exists", return_value=True)
|
|
|
|
|
|
@mock.patch("documents.consumer.document_consumer_declaration.send")
|
|
|
|
|
|
def test__get_parser_class_n_parsers(self, m, *args):
|
|
|
|
|
|
|
|
|
|
|
|
class DummyParser1(object):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DummyParser2(object):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
m.return_value = (
|
|
|
|
|
|
(None, lambda _: {"weight": 0, "parser": DummyParser1}),
|
|
|
|
|
|
(None, lambda _: {"weight": 1, "parser": DummyParser2}),
|
|
|
|
|
|
)
|
2018-02-25 19:42:03 +01:00
|
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
Consumer(consume=tmpdir)._get_parser_class("doc.pdf"),
|
|
|
|
|
|
DummyParser2
|
|
|
|
|
|
)
|
2017-03-28 21:01:50 +00:00
|
|
|
|
|
|
|
|
|
|
@mock.patch("documents.consumer.os.makedirs")
|
|
|
|
|
|
@mock.patch("documents.consumer.os.path.exists", return_value=True)
|
|
|
|
|
|
@mock.patch("documents.consumer.document_consumer_declaration.send")
|
|
|
|
|
|
def test__get_parser_class_0_parsers(self, m, *args):
|
|
|
|
|
|
m.return_value = ((None, lambda _: None),)
|
2018-02-25 19:42:03 +01:00
|
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
|
|
self.assertIsNone(
|
|
|
|
|
|
Consumer(consume=tmpdir)._get_parser_class("doc.pdf")
|
|
|
|
|
|
)
|
2017-03-28 21:01:50 +00:00
|
|
|
|
|
|
|
|
|
|
@mock.patch("documents.consumer.os.makedirs")
|
|
|
|
|
|
@mock.patch("documents.consumer.os.path.exists", return_value=True)
|
|
|
|
|
|
@mock.patch("documents.consumer.document_consumer_declaration.send")
|
|
|
|
|
|
def _get_consumer(self, m, *args):
|
|
|
|
|
|
m.return_value = (
|
|
|
|
|
|
(None, lambda _: {"weight": 0, "parser": self.DummyParser}),
|
|
|
|
|
|
)
|
2018-02-25 19:42:03 +01:00
|
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
|
|
return Consumer(consume=tmpdir)
|
2017-03-28 21:01:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-20 18:11:51 +01:00
|
|
|
|
class TestAttributes(TestCase):
|
2016-02-21 00:14:50 +00:00
|
|
|
|
|
2016-02-11 22:05:55 +00:00
|
|
|
|
TAGS = ("tag1", "tag2", "tag3")
|
2016-03-24 19:18:33 +00:00
|
|
|
|
EXTENSIONS = (
|
2017-07-15 17:47:17 +01:00
|
|
|
|
"pdf", "png", "jpg", "jpeg", "gif", "tiff", "tif",
|
|
|
|
|
|
"PDF", "PNG", "JPG", "JPEG", "GIF", "TIFF", "TIF",
|
|
|
|
|
|
"PdF", "PnG", "JpG", "JPeG", "GiF", "TiFf", "TiF",
|
2016-02-23 20:15:13 +00:00
|
|
|
|
)
|
2016-02-21 00:14:50 +00:00
|
|
|
|
|
2016-02-11 22:05:55 +00:00
|
|
|
|
def _test_guess_attributes_from_name(self, path, sender, title, tags):
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
for extension in self.EXTENSIONS:
|
|
|
|
|
|
|
|
|
|
|
|
f = path.format(extension)
|
2016-03-07 21:37:18 +02:00
|
|
|
|
file_info = FileInfo.from_path(f)
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
if sender:
|
|
|
|
|
|
self.assertEqual(file_info.correspondent.name, sender, f)
|
|
|
|
|
|
else:
|
|
|
|
|
|
self.assertIsNone(file_info.correspondent, f)
|
|
|
|
|
|
|
2016-03-07 21:37:18 +02:00
|
|
|
|
self.assertEqual(file_info.title, title, f)
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
2016-03-07 21:37:18 +02:00
|
|
|
|
self.assertEqual(tuple([t.slug for t in file_info.tags]), tags, f)
|
2016-03-24 19:18:33 +00:00
|
|
|
|
if extension.lower() == "jpeg":
|
|
|
|
|
|
self.assertEqual(file_info.extension, "jpg", f)
|
2017-07-15 17:47:17 +01:00
|
|
|
|
elif extension.lower() == "tif":
|
|
|
|
|
|
self.assertEqual(file_info.extension, "tiff", f)
|
2016-02-23 20:15:13 +00:00
|
|
|
|
else:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
self.assertEqual(file_info.extension, extension.lower(), f)
|
2016-02-11 12:25:23 +00:00
|
|
|
|
|
2016-02-11 22:05:55 +00:00
|
|
|
|
def test_guess_attributes_from_name0(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Sender - Title.{}", "Sender", "Title", ())
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name1(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Spaced Sender - Title.{}", "Spaced Sender", "Title", ())
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name2(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Sender - Spaced Title.{}", "Sender", "Spaced Title", ())
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name3(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Dashed-Sender - Title.{}", "Dashed-Sender", "Title", ())
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name4(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Sender - Dashed-Title.{}", "Sender", "Dashed-Title", ())
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name5(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Sender - Title - tag1,tag2,tag3.{}",
|
|
|
|
|
|
"Sender",
|
|
|
|
|
|
"Title",
|
|
|
|
|
|
self.TAGS
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name6(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Spaced Sender - Title - tag1,tag2,tag3.{}",
|
|
|
|
|
|
"Spaced Sender",
|
|
|
|
|
|
"Title",
|
|
|
|
|
|
self.TAGS
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name7(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Sender - Spaced Title - tag1,tag2,tag3.{}",
|
|
|
|
|
|
"Sender",
|
|
|
|
|
|
"Spaced Title",
|
|
|
|
|
|
self.TAGS
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name8(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Dashed-Sender - Title - tag1,tag2,tag3.{}",
|
|
|
|
|
|
"Dashed-Sender",
|
|
|
|
|
|
"Title",
|
|
|
|
|
|
self.TAGS
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name9(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Sender - Dashed-Title - tag1,tag2,tag3.{}",
|
|
|
|
|
|
"Sender",
|
|
|
|
|
|
"Dashed-Title",
|
|
|
|
|
|
self.TAGS
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name10(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
"/path/to/Σενδερ - Τιτλε - tag1,tag2,tag3.{}",
|
|
|
|
|
|
"Σενδερ",
|
|
|
|
|
|
"Τιτλε",
|
|
|
|
|
|
self.TAGS
|
2016-02-11 12:25:23 +00:00
|
|
|
|
)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
|
2016-03-07 21:48:47 +02:00
|
|
|
|
def test_guess_attributes_from_name_when_correspondent_empty(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
'/path/to/ - weird empty correspondent but should not break.{}',
|
|
|
|
|
|
None,
|
2016-03-24 19:18:33 +00:00
|
|
|
|
'weird empty correspondent but should not break',
|
2016-03-07 21:48:47 +02:00
|
|
|
|
()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name_when_title_starts_with_dash(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
'/path/to/- weird but should not break.{}',
|
|
|
|
|
|
None,
|
|
|
|
|
|
'- weird but should not break',
|
|
|
|
|
|
()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name_when_title_ends_with_dash(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
'/path/to/weird but should not break -.{}',
|
|
|
|
|
|
None,
|
|
|
|
|
|
'weird but should not break -',
|
|
|
|
|
|
()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guess_attributes_from_name_when_title_is_empty(self):
|
|
|
|
|
|
self._test_guess_attributes_from_name(
|
|
|
|
|
|
'/path/to/weird correspondent but should not break - .{}',
|
|
|
|
|
|
'weird correspondent but should not break',
|
|
|
|
|
|
'',
|
|
|
|
|
|
()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2016-03-07 21:42:52 +02:00
|
|
|
|
|
2016-10-26 09:32:59 +00:00
|
|
|
|
class TestFieldPermutations(TestCase):
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
valid_dates = (
|
|
|
|
|
|
"20150102030405Z",
|
|
|
|
|
|
"20150102Z",
|
|
|
|
|
|
)
|
|
|
|
|
|
valid_correspondents = [
|
|
|
|
|
|
"timmy",
|
|
|
|
|
|
"Dr. McWheelie",
|
|
|
|
|
|
"Dash Gor-don",
|
|
|
|
|
|
"ο Θερμαστής",
|
|
|
|
|
|
""
|
|
|
|
|
|
]
|
|
|
|
|
|
valid_titles = ["title", "Title w Spaces", "Title a-dash", "Τίτλος", ""]
|
|
|
|
|
|
valid_tags = ["tag", "tig,tag", "tag1,tag2,tag-3"]
|
|
|
|
|
|
valid_extensions = ["pdf", "png", "jpg", "jpeg", "gif"]
|
|
|
|
|
|
|
|
|
|
|
|
def _test_guessed_attributes(self, filename, created=None,
|
|
|
|
|
|
correspondent=None, title=None,
|
|
|
|
|
|
extension=None, tags=None):
|
|
|
|
|
|
|
|
|
|
|
|
info = FileInfo.from_path(filename)
|
|
|
|
|
|
|
|
|
|
|
|
# Created
|
|
|
|
|
|
if created is None:
|
|
|
|
|
|
self.assertIsNone(info.created, filename)
|
|
|
|
|
|
else:
|
|
|
|
|
|
self.assertEqual(info.created.year, int(created[:4]), filename)
|
|
|
|
|
|
self.assertEqual(info.created.month, int(created[4:6]), filename)
|
|
|
|
|
|
self.assertEqual(info.created.day, int(created[6:8]), filename)
|
|
|
|
|
|
|
|
|
|
|
|
# Correspondent
|
|
|
|
|
|
if correspondent:
|
|
|
|
|
|
self.assertEqual(info.correspondent.name, correspondent, filename)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
else:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
self.assertEqual(info.correspondent, None, filename)
|
|
|
|
|
|
|
|
|
|
|
|
# Title
|
|
|
|
|
|
self.assertEqual(info.title, title, filename)
|
|
|
|
|
|
|
|
|
|
|
|
# Tags
|
2016-03-07 21:42:52 +02:00
|
|
|
|
if tags is None:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
self.assertEqual(info.tags, (), filename)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
else:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
[t.slug for t in info.tags], tags.split(','),
|
|
|
|
|
|
filename
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Extension
|
|
|
|
|
|
if extension == 'jpeg':
|
|
|
|
|
|
extension = 'jpg'
|
|
|
|
|
|
self.assertEqual(info.extension, extension, filename)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
|
|
|
|
|
|
def test_just_title(self):
|
2016-03-24 19:18:33 +00:00
|
|
|
|
template = '/path/to/{title}.{extension}'
|
2016-03-07 21:42:52 +02:00
|
|
|
|
for title in self.valid_titles:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
for extension in self.valid_extensions:
|
|
|
|
|
|
spec = dict(title=title, extension=extension)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
filename = template.format(**spec)
|
|
|
|
|
|
self._test_guessed_attributes(filename, **spec)
|
|
|
|
|
|
|
|
|
|
|
|
def test_title_and_correspondent(self):
|
2016-03-24 19:18:33 +00:00
|
|
|
|
template = '/path/to/{correspondent} - {title}.{extension}'
|
2016-03-07 21:42:52 +02:00
|
|
|
|
for correspondent in self.valid_correspondents:
|
|
|
|
|
|
for title in self.valid_titles:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
for extension in self.valid_extensions:
|
2016-03-07 21:42:52 +02:00
|
|
|
|
spec = dict(correspondent=correspondent, title=title,
|
2016-03-24 19:18:33 +00:00
|
|
|
|
extension=extension)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
filename = template.format(**spec)
|
|
|
|
|
|
self._test_guessed_attributes(filename, **spec)
|
|
|
|
|
|
|
|
|
|
|
|
def test_title_and_correspondent_and_tags(self):
|
2016-03-24 19:18:33 +00:00
|
|
|
|
template = '/path/to/{correspondent} - {title} - {tags}.{extension}'
|
2016-03-07 21:42:52 +02:00
|
|
|
|
for correspondent in self.valid_correspondents:
|
|
|
|
|
|
for title in self.valid_titles:
|
|
|
|
|
|
for tags in self.valid_tags:
|
2016-03-24 19:18:33 +00:00
|
|
|
|
for extension in self.valid_extensions:
|
2016-03-07 21:42:52 +02:00
|
|
|
|
spec = dict(correspondent=correspondent, title=title,
|
2016-03-24 19:18:33 +00:00
|
|
|
|
tags=tags, extension=extension)
|
2016-03-07 21:42:52 +02:00
|
|
|
|
filename = template.format(**spec)
|
|
|
|
|
|
self._test_guessed_attributes(filename, **spec)
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
def test_created_and_correspondent_and_title_and_tags(self):
|
|
|
|
|
|
|
2018-04-22 16:28:21 +01:00
|
|
|
|
template = (
|
|
|
|
|
|
"/path/to/{created} - "
|
|
|
|
|
|
"{correspondent} - "
|
|
|
|
|
|
"{title} - "
|
|
|
|
|
|
"{tags}"
|
|
|
|
|
|
".{extension}"
|
|
|
|
|
|
)
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
for created in self.valid_dates:
|
|
|
|
|
|
for correspondent in self.valid_correspondents:
|
|
|
|
|
|
for title in self.valid_titles:
|
|
|
|
|
|
for tags in self.valid_tags:
|
|
|
|
|
|
for extension in self.valid_extensions:
|
|
|
|
|
|
spec = {
|
|
|
|
|
|
"created": created,
|
|
|
|
|
|
"correspondent": correspondent,
|
|
|
|
|
|
"title": title,
|
|
|
|
|
|
"tags": tags,
|
|
|
|
|
|
"extension": extension
|
|
|
|
|
|
}
|
|
|
|
|
|
self._test_guessed_attributes(
|
|
|
|
|
|
template.format(**spec), **spec)
|
|
|
|
|
|
|
|
|
|
|
|
def test_created_and_correspondent_and_title(self):
|
|
|
|
|
|
|
2018-04-22 16:28:21 +01:00
|
|
|
|
template = "/path/to/{created} - {correspondent} - {title}.{extension}"
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
for created in self.valid_dates:
|
|
|
|
|
|
for correspondent in self.valid_correspondents:
|
|
|
|
|
|
for title in self.valid_titles:
|
|
|
|
|
|
|
|
|
|
|
|
# Skip cases where title looks like a tag as we can't
|
|
|
|
|
|
# accommodate such cases.
|
|
|
|
|
|
if title.lower() == title:
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
for extension in self.valid_extensions:
|
|
|
|
|
|
spec = {
|
|
|
|
|
|
"created": created,
|
|
|
|
|
|
"correspondent": correspondent,
|
|
|
|
|
|
"title": title,
|
|
|
|
|
|
"extension": extension
|
|
|
|
|
|
}
|
|
|
|
|
|
self._test_guessed_attributes(
|
|
|
|
|
|
template.format(**spec), **spec)
|
|
|
|
|
|
|
|
|
|
|
|
def test_created_and_title(self):
|
|
|
|
|
|
|
2018-04-22 16:28:21 +01:00
|
|
|
|
template = "/path/to/{created} - {title}.{extension}"
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
for created in self.valid_dates:
|
|
|
|
|
|
for title in self.valid_titles:
|
|
|
|
|
|
for extension in self.valid_extensions:
|
|
|
|
|
|
spec = {
|
|
|
|
|
|
"created": created,
|
|
|
|
|
|
"title": title,
|
|
|
|
|
|
"extension": extension
|
|
|
|
|
|
}
|
|
|
|
|
|
self._test_guessed_attributes(
|
|
|
|
|
|
template.format(**spec), **spec)
|
|
|
|
|
|
|
|
|
|
|
|
def test_created_and_title_and_tags(self):
|
|
|
|
|
|
|
2018-04-22 16:28:21 +01:00
|
|
|
|
template = "/path/to/{created} - {title} - {tags}.{extension}"
|
2016-03-24 19:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
for created in self.valid_dates:
|
|
|
|
|
|
for title in self.valid_titles:
|
|
|
|
|
|
for tags in self.valid_tags:
|
|
|
|
|
|
for extension in self.valid_extensions:
|
|
|
|
|
|
spec = {
|
|
|
|
|
|
"created": created,
|
|
|
|
|
|
"title": title,
|
|
|
|
|
|
"tags": tags,
|
|
|
|
|
|
"extension": extension
|
|
|
|
|
|
}
|
|
|
|
|
|
self._test_guessed_attributes(
|
|
|
|
|
|
template.format(**spec), **spec)
|
2018-04-22 16:27:43 +01:00
|
|
|
|
|
|
|
|
|
|
def test_invalid_date_format(self):
|
|
|
|
|
|
info = FileInfo.from_path("/path/to/06112017Z - title.pdf")
|
|
|
|
|
|
self.assertEqual(info.title, "title")
|
|
|
|
|
|
self.assertIsNone(info.created)
|