Skip to content

Commit

Permalink
some more tests, bug fix in task failure
Browse files Browse the repository at this point in the history
  • Loading branch information
brad committed Dec 24, 2014
1 parent 6439249 commit a15461a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion misfitapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def process_notification(content):
retry_after_secs = (reset - arrow.now()).seconds
logger.debug('Rate limit reached, will try again in %i seconds' %
retry_after_secs)
raise process_notification.retry(e, countdown=retry_after_secs)
raise process_notification.retry(countdown=retry_after_secs)
except Exception:
exc = sys.exc_info()[1]
logger.exception("Unknown exception processing notification: %s" % exc)
Expand Down
6 changes: 6 additions & 0 deletions misfitapp/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def test_profile(self):
}
p = Profile(**data)
p.save()
self.assertEqual(p.email, data['email'])
self.assertEqual('%s' % p, p.email)

def test_device(self):
""" Test the Device Model """
Expand All @@ -67,6 +69,8 @@ def test_device(self):
}
d = Device(**data)
d.save()
self.assertEqual(d.serial_number, data['serial_number'])
self.assertEqual('%s' % d, 'shine: %s' % data['serial_number'])

def test_goal(self):
""" Test the Goal Model """
Expand All @@ -78,6 +82,7 @@ def test_goal(self):
}
g = Goal(**data)
g.save()
self.assertEqual('%s' % g, '%s 2014-12-12 64.2 of 200' % data['id'])

def test_session(self):
""" Test the Session Model """
Expand All @@ -90,6 +95,7 @@ def test_session(self):
}
s = Session(**data)
s.save()
self.assertEqual('%s' % s, '2014-12-12 22:00:01 300 soccer')

def test_sleep(self):
""" Test the Sleep and Sleep Segment model """
Expand Down
32 changes: 30 additions & 2 deletions misfitapp/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ def test_subscription_confirmation(self, verify_signature_mock):
self.assertEqual(Profile.objects.count(), 0)
self.assertEqual(Summary.objects.count(), 0)

@freeze_time("2014-07-02 10:52:00", tz_offset=0)
@patch('logging.Logger.debug')
@patch('misfit.notification.MisfitNotification.verify_signature')
def test_notification(self, verify_signature_mock):
def test_notification(self, verify_signature_mock, debug_mock):
"""
Check that a task gets created to handle notification
"""
Expand All @@ -222,10 +224,36 @@ def test_notification(self, verify_signature_mock):
eq_(Profile.objects.filter(user=self.user).count(), 1)
eq_(Summary.objects.filter(user=self.user).count(), 3)

# Check that we fail gracefully when the user doesn't exist on our end
# Check that we fail gracefully when we hit the rate limit
Goal.objects.all().delete()
Profile.objects.all().delete()
Summary.objects.all().delete()
with HTTMock(JsonMock().profile_http):
with patch('celery.app.task.Task.delay') as mock_delay:
mock_delay.side_effect = lambda arg: process_notification(arg)
with patch('misfit.Misfit.goal') as mock_goal:
resp = MagicMock()
resp.headers = {'x-ratelimit-reset': 1404298869}
mock_goal.side_effect = \
misfit_exceptions.MisfitRateLimitError(429, '', resp)
with patch('celery.app.task.Task.retry') as mock_retry:
mock_retry.side_effect = BaseException
try:
self.client.post(notify_url, data=content,
content_type='application/json')
assert False, 'We should have raised an exception'
except BaseException:
assert True
mock_delay.assert_called_once_with(content)
mock_goal.assert_called_once_with(
object_id='51a4189acf12e53f81000001')
mock_retry.assert_called_once_with(countdown=549)
eq_(Goal.objects.filter(user=self.user).count(), 0)
eq_(Profile.objects.filter(user=self.user).count(), 1)
eq_(Summary.objects.filter(user=self.user).count(), 0)

# Check that we fail gracefully when the user doesn't exist on our end
Profile.objects.all().delete()
MisfitUser.objects.all().delete()
with HTTMock(JsonMock().goal_http, JsonMock().profile_http,
JsonMock('summary_detail').summary_http):
Expand Down

0 comments on commit a15461a

Please sign in to comment.