mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Bug fixes to support Django 1.7
This commit is contained in:
parent
e151006582
commit
b5d5207cd8
2 changed files with 35 additions and 30 deletions
|
|
@ -16,6 +16,7 @@ logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
|
args = "<[Mailbox Name (optional)]>"
|
||||||
help = "Receive incoming mail via stdin"
|
help = "Receive incoming mail via stdin"
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
|
from distutils.version import LooseVersion
|
||||||
|
import mock
|
||||||
|
|
||||||
from django.core.management import call_command, CommandError
|
from django.core.management import call_command, CommandError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
import django
|
||||||
import mock
|
|
||||||
|
|
||||||
class CommandsTestCase(TestCase):
|
class CommandsTestCase(TestCase):
|
||||||
def test_processincomingmessage_no_args(self):
|
def test_processincomingmessage_no_args(self):
|
||||||
"""Check that processincomingmessage works with no args"""
|
"""Check that processincomingmessage works with no args"""
|
||||||
|
|
||||||
|
mailbox_name = None
|
||||||
# Mock handle so that the test doesn't hang waiting for input. Note that we are only testing
|
# Mock handle so that the test doesn't hang waiting for input. Note that we are only testing
|
||||||
# the argument parsing here -- functionality should be tested elsewhere
|
# the argument parsing here -- functionality should be tested elsewhere
|
||||||
with mock.patch('django_mailbox.management.commands.processincomingmessage.Command.handle') as handle:
|
with mock.patch('django_mailbox.management.commands.processincomingmessage.Command.handle') as handle:
|
||||||
|
|
@ -13,42 +17,42 @@ class CommandsTestCase(TestCase):
|
||||||
handle.return_value = None
|
handle.return_value = None
|
||||||
|
|
||||||
call_command('processincomingmessage')
|
call_command('processincomingmessage')
|
||||||
|
args, kwargs = handle.call_args
|
||||||
|
|
||||||
# Define the arguments we expect handle to be called with
|
|
||||||
required_arguments = {
|
|
||||||
# This should be None, since it isn't given
|
|
||||||
'mailbox_name': None,
|
|
||||||
# All otheres can be anything; we don't care about them at all
|
|
||||||
'no_color': mock.ANY,
|
|
||||||
'pythonpath': mock.ANY,
|
|
||||||
'settings': mock.ANY,
|
|
||||||
'skip_checks': mock.ANY,
|
|
||||||
'traceback': mock.ANY,
|
|
||||||
'verbosity': mock.ANY
|
|
||||||
}
|
|
||||||
# Make sure that we called with the right arguments
|
# Make sure that we called with the right arguments
|
||||||
handle.assert_called_with(**required_arguments)
|
try:
|
||||||
|
self.assertEqual(kwargs['mailbox_name'], mailbox_name)
|
||||||
|
except KeyError:
|
||||||
|
# Handle Django 1.7
|
||||||
|
# It uses optparse instead of argparse, so instead of being
|
||||||
|
# set to None, mailbox_name is simply left out altogether
|
||||||
|
# Thus we expect an empty tuple here
|
||||||
|
self.assertEqual(args, tuple())
|
||||||
|
|
||||||
|
|
||||||
def test_processincomingmessage_with_arg(self):
|
def test_processincomingmessage_with_arg(self):
|
||||||
"""Check that processincomingmessage works with mailbox_name given"""
|
"""Check that processincomingmessage works with mailbox_name given"""
|
||||||
|
|
||||||
|
mailbox_name = 'foo_mailbox'
|
||||||
|
|
||||||
with mock.patch('django_mailbox.management.commands.processincomingmessage.Command.handle') as handle:
|
with mock.patch('django_mailbox.management.commands.processincomingmessage.Command.handle') as handle:
|
||||||
handle.return_value = None
|
handle.return_value = None
|
||||||
|
|
||||||
call_command('processincomingmessage', 'foo_mailbox')
|
call_command('processincomingmessage', mailbox_name)
|
||||||
|
args, kwargs = handle.call_args
|
||||||
required_arguments = {
|
try:
|
||||||
'mailbox_name': 'foo_mailbox',
|
self.assertEqual(kwargs['mailbox_name'], mailbox_name)
|
||||||
'no_color': mock.ANY,
|
except KeyError:
|
||||||
'pythonpath': mock.ANY,
|
# Handle Django 1.7
|
||||||
'settings': mock.ANY,
|
# It uses optparse instead of argparse, so instead of being
|
||||||
'skip_checks': mock.ANY,
|
# in kwargs, mailbox_name is in args
|
||||||
'traceback': mock.ANY,
|
self.assertEqual(args[0], mailbox_name)
|
||||||
'verbosity': mock.ANY
|
|
||||||
}
|
|
||||||
handle.assert_called_with(**required_arguments)
|
|
||||||
|
|
||||||
def test_processincomingmessage_too_many_args(self):
|
def test_processincomingmessage_too_many_args(self):
|
||||||
"""Check that processincomingmessage raises an error if too many args"""
|
"""Check that processincomingmessage raises an error if too many args"""
|
||||||
with self.assertRaises(CommandError):
|
# Only perform this test for Django versions greater than 1.7.*. This
|
||||||
call_command('processincomingmessage', 'foo_mailbox', 'invalid_arg')
|
# is because, with optparse, too many arguments doesn't result in an
|
||||||
|
# error, which means this test is worthless anyway
|
||||||
|
if LooseVersion(django.get_version()) >= LooseVersion('1.8'):
|
||||||
|
with self.assertRaises(CommandError):
|
||||||
|
call_command('processincomingmessage', 'foo_mailbox', 'invalid_arg')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue