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.
This commit is contained in:
Jelle van der Waa 2018-03-25 17:04:55 +02:00
parent 876b9b0b19
commit bbd06a3559
2 changed files with 23 additions and 4 deletions

View File

@ -23,7 +23,7 @@
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 @@ def handle(self, *args, **options):
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', '')

View File

@ -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 @@ def test_parse_subject(self):
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))