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 @@ ...@@ -23,7 +23,7 @@
from parse import parse from parse import parse
from django.db.utils import Error as DBError 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 from main.models import Donor
...@@ -91,8 +91,7 @@ def handle(self, *args, **options): ...@@ -91,8 +91,7 @@ def handle(self, *args, **options):
directory = options['maildir'] directory = options['maildir']
maildir = mailbox.Maildir(directory, create=False) maildir = mailbox.Maildir(directory, create=False)
except mailbox.Error: except mailbox.Error:
logger.error(u"Failed to open maildir: '%s'", directory) raise CommandError(u"Failed to open maildir")
return 0
for msg in maildir: for msg in maildir:
subject = msg.get('subject', '') subject = msg.get('subject', '')
......
# -*- coding: utf-8 -*-
from email.header import Header
from django.test import SimpleTestCase 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 from main.management.commands.donor_import import Command
...@@ -20,9 +26,23 @@ def test_parse_subject(self): ...@@ -20,9 +26,23 @@ def test_parse_subject(self):
output = self.command.parse_subject(valid) output = self.command.parse_subject(valid)
self.assertEqual(output, u'John Doe') self.assertEqual(output, u'John Doe')
def test_parse_name(self): def test_parse_name(self):
self.assertEqual(self.command.sanitize_name(u'1244'), u'') 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 '), 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') 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))
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