Skip to content

Instantly share code, notes, and snippets.

@danrjohnson
Created December 11, 2014 22:07
Show Gist options
  • Save danrjohnson/98b2220a4c694dc51f88 to your computer and use it in GitHub Desktop.
Save danrjohnson/98b2220a4c694dc51f88 to your computer and use it in GitHub Desktop.
from datetime import date
from django.core.mail import send_mail
class Person(object):
def __init__(self, given_name, family_name, email, dob, *args, **kwargs):
self.given_name = given_name
self.family_name = family_name
self.email = email
self.dob = dob
self.args = args
self.kwargs = kwargs
def get_full_name(self):
"""
Display full name of Person
"""
return '%s %s' % (self.given_name, self.family_name)
def get_age(self):
"""
Returns Person's age in years as an integer
"""
today = date.today()
return today.year - self.dob.year - (
(today.month, today.day) < (self.dob.month, self.dob.day)
)
def send_birthday_email(self):
"""
Sends Person a congratulatory email if today is his or her birthday
"""
today = date.today()
if (today.month, today.day) == (self.dob.month, self.dob.day):
send_mail(
'Happy Birthday, %s!' % self.given_name,
'Now that its your birthday....',
'[email protected]',
[self.email]
)
def is_family_member(self, person_two):
"""
Naively determine if two Persons are family by comparing family_name
"""
if self.family_name == person_two.family_name:
return True
return False
from datetime import date
import mock
import unittest
from person import Person
class TestPerson(unittest.TestCase):
def setUp(self):
self.person = Person(
"Dan",
"Johnson",
"[email protected]",
date(1984, 7, 4)
)
@mock.patch('person.date')
def test_get_age(self, date_mock):
date_mock.today.return_value = date(2013, 7, 4)
self.assertEqual(
self.person.get_age(),
29
)
@mock.patch('person.date')
@mock.patch('person.send_mail')
def test_send_birthday_email_is_birthday(self, send_mail_mock, date_mock):
date_mock.today.return_value = date(2014, 7, 4)
self.person.send_birthday_email()
send_mail_mock.assert_called_once_with(
'Happy Birthday, Dan!',
'Now that its your birthday....',
'[email protected]',
['[email protected]']
)
@mock.patch('person.date')
@mock.patch('person.send_mail')
def test_send_birthday_email_not_birthday(self, send_mail_mock, date_mock):
date_mock.today.return_value = date(2014, 1, 1)
self.person.send_birthday_email()
self.assertFalse(send_mail_mock.called)
def test_is_family_member_not_same_family_name(self):
person_two = mock.Mock()
person_two.family_name = 'Davenport'
self.assertFalse(self.person.is_family_member(person_two))
def test_is_family_member_same_family_name(self):
person_two = mock.Mock()
person_two.family_name = 'Johnson'
self.assertTrue(self.person.is_family_member(person_two))
if __name__ == '__main__' and __package__ is None:
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment