Make the importer a little more robust against some types of errors

This commit is contained in:
Trenton H
2023-04-25 12:17:37 -07:00
parent 1fc9eaf360
commit a2d4d16867
3 changed files with 136 additions and 39 deletions

View File

@@ -1,6 +1,10 @@
from django.core.management.base import CommandError
from django.test import TestCase
from documents.settings import EXPORTER_FILE_NAME
from documents.settings import EXPORTER_ARCHIVE_NAME
from pathlib import Path
import tempfile
from django.core.management import call_command
from documents.management.commands.document_importer import Command
@@ -14,17 +18,17 @@ class TestImporter(TestCase):
self.assertRaises(
CommandError,
cmd._check_manifest_exists,
"/tmp/manifest.json",
Path("/tmp/manifest.json"),
)
def test_check_manifest(self):
cmd = Command()
cmd.source = "/tmp"
cmd.source = Path("/tmp")
cmd.manifest = [{"model": "documents.document"}]
with self.assertRaises(CommandError) as cm:
cmd._check_manifest()
cmd._check_manifest_valid()
self.assertIn("The manifest file contains a record", str(cm.exception))
cmd.manifest = [
@@ -32,8 +36,81 @@ class TestImporter(TestCase):
]
# self.assertRaises(CommandError, cmd._check_manifest)
with self.assertRaises(CommandError) as cm:
cmd._check_manifest()
cmd._check_manifest_valid()
self.assertIn(
'The manifest file refers to "noexist.pdf"',
str(cm.exception),
)
def test_import_permission_error(self):
"""
GIVEN:
- Original file which cannot be read from
- Archive file which cannot be read from
WHEN:
- Import is attempted
THEN:
- CommandError is raised indicating the issue
"""
with tempfile.TemporaryDirectory() as temp_dir:
# Create empty files
original_path = Path(temp_dir) / "original.pdf"
archive_path = Path(temp_dir) / "archive.pdf"
original_path.touch()
archive_path.touch()
# No read permissions
original_path.chmod(0o222)
cmd = Command()
cmd.source = Path(temp_dir)
cmd.manifest = [
{
"model": "documents.document",
EXPORTER_FILE_NAME: "original.pdf",
EXPORTER_ARCHIVE_NAME: "archive.pdf",
},
]
with self.assertRaises(CommandError) as cm:
cmd._check_manifest_valid()
self.assertInt("Failed to read from original file", str(cm.exception))
original_path.chmod(0o444)
archive_path.chmod(0o222)
with self.assertRaises(CommandError) as cm:
cmd._check_manifest_valid()
self.assertInt("Failed to read from archive file", str(cm.exception))
def test_import_source_not_existing(self):
"""
GIVEN:
- Source given doesn't exist
WHEN:
- Import is attempted
THEN:
- CommandError is raised indicating the issue
"""
with self.assertRaises(CommandError) as cm:
call_command("document_importer", Path("/tmp/notapath"))
self.assertInt("That path doesn't exist", str(cm.exception))
def test_import_source_not_readable(self):
"""
GIVEN:
- Source given isn't readable
WHEN:
- Import is attempted
THEN:
- CommandError is raised indicating the issue
"""
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir)
path.chmod(0o222)
with self.assertRaises(CommandError) as cm:
call_command("document_importer", path)
self.assertInt(
"That path doesn't appear to be readable",
str(cm.exception),
)

View File

@@ -212,7 +212,7 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
Tag.objects.all().delete()
self.assertEqual(Document.objects.count(), 0)
call_command("document_importer", self.target)
call_command("document_importer", "--no-progress-bar", self.target)
self.assertEqual(Document.objects.count(), 4)
self.assertEqual(Tag.objects.count(), 1)
self.assertEqual(Correspondent.objects.count(), 1)
@@ -541,7 +541,7 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(Document.objects.count(), 4)
Document.objects.all().delete()
self.assertEqual(Document.objects.count(), 0)
call_command("document_importer", self.target)
call_command("document_importer", "--no-progress-bar", self.target)
self.assertEqual(Document.objects.count(), 4)
def test_no_thumbnail(self):
@@ -584,7 +584,7 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(Document.objects.count(), 4)
Document.objects.all().delete()
self.assertEqual(Document.objects.count(), 0)
call_command("document_importer", self.target)
call_command("document_importer", "--no-progress-bar", self.target)
self.assertEqual(Document.objects.count(), 4)
def test_split_manifest(self):
@@ -613,7 +613,7 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(Document.objects.count(), 4)
Document.objects.all().delete()
self.assertEqual(Document.objects.count(), 0)
call_command("document_importer", self.target)
call_command("document_importer", "--no-progress-bar", self.target)
self.assertEqual(Document.objects.count(), 4)
def test_folder_prefix(self):
@@ -637,5 +637,5 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(Document.objects.count(), 4)
Document.objects.all().delete()
self.assertEqual(Document.objects.count(), 0)
call_command("document_importer", self.target)
call_command("document_importer", "--no-progress-bar", self.target)
self.assertEqual(Document.objects.count(), 4)