Skip to content

Commit

Permalink
Fixes #7 includes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aarcro committed Aug 19, 2015
1 parent f749924 commit 1f88a4e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
5 changes: 3 additions & 2 deletions reportmail/command.py
Expand Up @@ -4,7 +4,8 @@
from reportmail.reporter import Reporter


def apply_reporter(subject, template='reportmail/command_report.txt', committer=None, reporter_cls=Reporter, additional_context=None):
def apply_reporter(subject, template='reportmail/command_report.txt', committer=None,
reporter_cls=Reporter, additional_context=None):
""" Adding a reporting feature for django command
You can use this as decorator for Command.handle.
Expand Down Expand Up @@ -34,7 +35,7 @@ def handle(self, reporter, *args, **options):
def wrapper(handle_func):
@wraps(handle_func)
def wrapped(self, *args, **options):
base_context={
base_context = {
'args': args,
'options': options,
'command': self.__module__
Expand Down
7 changes: 6 additions & 1 deletion reportmail/reporter.py
Expand Up @@ -68,6 +68,7 @@ def __init__(self, subject, template, base_context=None, committer=None):
self.stored_text = []
self.base_context = base_context if base_context is not None else {}
self.committer = committer if committer is not None else admin_mail_committer
self.aborted = False

def __enter__(self):
return self
Expand Down Expand Up @@ -100,13 +101,17 @@ def render(self):
ctx['stored_text'] = self.stored_text
return get_template(self.template).render(Context(ctx))

def abort(self):
self.aborted = True

def commit(self):
""" A interface to send the report
Internally, this method will call `self.committer` by passing
`self.subject and result of `self.render()`.
"""
self.committer(self.subject, self.render())
if not self.aborted:
self.committer(self.subject, self.render())


def console_committer(subject, body):
Expand Down
36 changes: 34 additions & 2 deletions tests/test_command.py
Expand Up @@ -5,9 +5,9 @@
@override_settings(ADMINS=(('admin', 'admin@example.com'),),
EMAIL_SUBJECT_PREFIX="")
class TestApplyReporter(TestCase):
def _makeOne(self, *args):
def _makeOne(self, *args, **kwargs):
from reportmail.command import apply_reporter
return apply_reporter(*args)
return apply_reporter(*args, **kwargs)

def test__it(self):
class DummySelf(object):
Expand Down Expand Up @@ -35,6 +35,38 @@ def wrapped(self, reporter, *args, **options):
args: arg,
options: test=option,
result:
Stored
""")

def test__additional_context(self):
class DummySelf(object):
__module__ = '__module__'
wrapper = self._makeOne("Title", additional_context={'more': 'data'})

def wrapped(self, reporter, *args, **options):
reporter.append("Stored")
return reporter, args, options

r, a, o = wrapper(wrapped)(DummySelf(), 'arg', test='option')
self.assertEqual(r.subject, "Title")
self.assertEqual(r.template, 'reportmail/command_report.txt')
self.assertEqual(r.base_context, {'args': a, 'options': o,
'command': '__module__',
'more': 'data'})
self.assertEqual(r.stored_text, ["Stored"])
self.assertEqual(a, ('arg',))
self.assertEqual(o, {'test': 'option'})

from django.core import mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Title')
# Template doesn't use the 'more' key, so can't test the rendered output
self.assertEqual(mail.outbox[0].body, """\
Report of __module__
args: arg,
options: test=option,
result:
Stored
""")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_reporter.py
Expand Up @@ -29,6 +29,21 @@ def test__render(self):
actual = target.render()
self.assertEqual(actual, "test1\ntest2\n\nadditional\n")

def test__abort(self):
self.called = False

def dummy_committer(subject, body):
self.called = True

target = self._makeOne("", '')
self.assertFalse(target.aborted)

target.abort()
self.assertTrue(target.aborted)

target.commit()
self.assertFalse(self.called)

def test__commit(self):
self.actual_subject = None
self.actual_body = None
Expand Down

1 comment on commit 1f88a4e

@aarcro
Copy link
Contributor Author

@aarcro aarcro commented on 1f88a4e Aug 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not the best at github's workflow. I'd tried to put this on it's own feature branch to have a separate pull request. But the test changes add coverage for things I added in my master branch.

:( So sloppy, hopefully you can just pull it all.

Please sign in to comment.