Commit bbd06a35 authored by Jelle van der Waa's avatar Jelle van der Waa 🚧
Browse files

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.
parent 876b9b0b
......@@ -23,7 +23,7 @@
from parse import parse
from django.db.utils import Error as DBError
from import BaseCommand
from 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', '')
# -*- coding: utf-8 -*-
from email.header import Header
from django.test import SimpleTestCase
from import call_command
from import CommandError
from 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:
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))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment