diff options
author | Jelle van der Waa <jelle@vdwaa.nl> | 2018-03-25 17:04:55 +0200 |
---|---|---|
committer | Jelle van der Waa <jelle@vdwaa.nl> | 2018-03-25 17:04:55 +0200 |
commit | bbd06a35594da6865f02ac6034113e506b371fde (patch) | |
tree | 14ea1edfa7fb61ebba967998d7ecb1234d859908 /main | |
parent | 876b9b0b19c524b7dbd77619fc13d14fec5a0c93 (diff) | |
download | archweb-bbd06a35594da6865f02ac6034113e506b371fde.tar.gz archweb-bbd06a35594da6865f02ac6034113e506b371fde.zip |
main: Add more tests for donor_import
Throw a CommandError when the path is not found, similar to what
reporead does and add a test for decode_subject.
Diffstat (limited to 'main')
-rw-r--r-- | main/management/commands/donor_import.py | 5 | ||||
-rw-r--r-- | main/tests/test_donor_import.py | 22 |
2 files changed, 23 insertions, 4 deletions
diff --git a/main/management/commands/donor_import.py b/main/management/commands/donor_import.py index fdc63f33..895b316c 100644 --- a/main/management/commands/donor_import.py +++ b/main/management/commands/donor_import.py @@ -23,7 +23,7 @@ from email.header import decode_header from parse import parse from django.db.utils import Error as DBError -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandError from main.models import Donor @@ -91,8 +91,7 @@ class Command(BaseCommand): directory = options['maildir'] maildir = mailbox.Maildir(directory, create=False) except mailbox.Error: - logger.error(u"Failed to open maildir: '%s'", directory) - return 0 + raise CommandError(u"Failed to open maildir") for msg in maildir: subject = msg.get('subject', '') diff --git a/main/tests/test_donor_import.py b/main/tests/test_donor_import.py index 14609978..5aad3dc6 100644 --- a/main/tests/test_donor_import.py +++ b/main/tests/test_donor_import.py @@ -1,4 +1,10 @@ +# -*- coding: utf-8 -*- + +from email.header import Header + from django.test import SimpleTestCase +from django.core.management import call_command +from django.core.management.base import CommandError from main.management.commands.donor_import import Command @@ -20,9 +26,23 @@ class DonorImportTest(SimpleTestCase): output = self.command.parse_subject(valid) self.assertEqual(output, u'John Doe') - def test_parse_name(self): self.assertEqual(self.command.sanitize_name(u'1244'), u'') self.assertEqual(self.command.sanitize_name(u'John Doe'), u'John Doe') self.assertEqual(self.command.sanitize_name(u' John Doe '), u'John Doe') self.assertEqual(self.command.sanitize_name(u'John Doe 23'), u'John Doe') + + def test_decode_subject(self): + text = u'メイル' + subject = str(Header(text, 'utf-8')) + self.assertEqual(self.command.decode_subject(subject), text) + + def test_invalid_args(self): + with self.assertRaises(CommandError) as e: + call_command('donor_import') + self.assertIn('Error: too few arguments', str(e.exception)) + + def test_invalid_path(self): + with self.assertRaises(CommandError) as e: + call_command('donor_import', '/tmp/non-existant') + self.assertIn('Failed to open maildir', str(e.exception)) |