def setUp(self): # clear database. assert environment.ENVIRONMENT == 'test' User.drop_collection() Status.drop_collection() Emotion.drop_collection() # create test fixtures. lewpen = User(first_name='Lewpen', last_name='Trippin', email='*****@*****.**', username='******') lewpen.save() smiley = User(first_name='Smiley', last_name='Face', email='*****@*****.**', username='******') smiley.save() lisa = User(first_name='Lisa', last_name='Curly', email='*****@*****.**', username='******') lisa.save() self.happy_emotion = Emotion(name='happy') self.happy_emotion.save() self.statuses = { 'lewpen-morning': Status(user=lewpen, status='Morning.', date=datetime(2012,3,24,9,0)), 'lewpen-lunch': Status(user=lewpen, status='Lunch.', date=datetime(2012,3,24,12,0), emotion=self.happy_emotion), 'lewpen-evening': Status(user=lewpen, status='Evening.', date=datetime(2012,3,24,18,30)), 'lisa-sunny': Status(user=lisa, status='Sunny morning', date=datetime(2012,3,24,7,10)), 'lisa-happy': Status(user=lisa, status='Happy day!', date=datetime(2012,3,24,20,0), emotion=self.happy_emotion), 'smiley-mic': Status(user=smiley, status='Two turntables and a microphone', date=datetime(2012,3,24,19,0)), } for name, status in self.statuses.iteritems(): status.save()
class TestAddStatus(unittest2.TestCase): def setUp(self): # clear database. assert environment.ENVIRONMENT == 'test' User.drop_collection() Status.drop_collection() Emotion.drop_collection() # create test fixtures. self.user = User(first_name='Lewpen', last_name='Trippin', email='*****@*****.**', username='******') self.user.save() self.sad_emotion = Emotion(name='sad') self.sad_emotion.save() def test_add_status(self): """Add a status for user.""" self.assertEqual([], list(Status.objects)) api = Api() api.add_status(status='I was wanting you to love me.', user=self.user) # verify that the status was inserted successfully. status = Status.objects.first() self.assertIsNotNone(status) self.assertEqual(status.status, 'I was wanting you to love me.') self.assertEqual(status.user, self.user) self.assertEqual(status.emotion, None) def test_add_status_with_emotion(self): """Add a status with an emotion.""" self.assertEqual([], list(Status.objects)) api = Api() api.add_status(status='I was wanting you to love me.', user=self.user, emotion=self.sad_emotion) # verify that the status was inserted successfully. status = Status.objects.first() self.assertIsNotNone(status) self.assertEqual(status.status, 'I was wanting you to love me.') self.assertEqual(status.user, self.user) self.assertEqual(status.emotion, self.sad_emotion) def test_add_status_with_username_as_string(self): """ Add a status with the username specified as a string. """ self.assertEqual([], list(Status.objects)) api = Api() api.add_status(status='Sated and happy.', user='******') # verify that the status was inserted successfully. status = Status.objects.first() self.assertIsNotNone(status) self.assertEqual(status.status, 'Sated and happy.') self.assertEqual(status.user, self.user) self.assertEqual(status.emotion, None) def test_add_status_with_unknown_user(self): """ Add a status with the username specified as a string. The call will raise NoSuchUserError as the user does not exist. """ self.assertEqual([], list(Status.objects)) api = Api() with self.assertRaises(NoSuchUserError): api.add_status(status='Confused. Do I not exist?', user='******') def test_add_status_with_emotion_as_string(self): """ Add a status with the emotion specified as a string. """ self.assertEqual([], list(Status.objects)) api = Api() api.add_status(status='Very sad indeed.', user=self.user, emotion='sad') # verify that the status was inserted successfully. status = Status.objects.first() self.assertIsNotNone(status) self.assertEqual(status.status, 'Very sad indeed.') self.assertEqual(status.user, self.user) self.assertEqual(status.emotion, self.sad_emotion) def test_add_status_with_nonexisting_emotion_as_string(self): """ Add a status with the emotion specified as a string. The emotion will be created as it does not exist. """ self.assertEqual([], list(Status.objects)) api = Api() api.add_status(status='Lots of energy on the keyboard.', user=self.user, emotion='happy') # verify that the status was inserted successfully. status = Status.objects.first() self.assertIsNotNone(status) self.assertEqual(status.status, 'Lots of energy on the keyboard.') self.assertEqual(status.user, self.user) self.assertEqual(status.emotion.name, 'happy') def test_add_status_sends_post_request(self): """ Verify that add_status() sends a post request to the registered listener. """ self.assertEqual([], list(Status.objects)) notifier = MockNotifier() api = Api(notifier=notifier) api.add_status(status='Feeling kinda groovy, working on a movie.', user=self.user) self.assertEqual(1, len(notifier.notifications)) self.assertEqual([{'username': self.user.username}], notifier.notifications)