Skip to content

Commit

Permalink
complete import_historic testnig
Browse files Browse the repository at this point in the history
  • Loading branch information
brad committed Dec 25, 2014
1 parent 382454d commit 185a98c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 20 deletions.
12 changes: 11 additions & 1 deletion misfitapp/tests/responses/goal_goals.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"goals": []
"goals": [{
"id":"51a4189acf12e53f81000001",
"date":"2014-10-05",
"points":300,
"targetPoints":800
}, {
"id":"51a4189acf12e53f81000002",
"date":"2014-10-06",
"points":500,
"targetPoints":1000
}]
}
20 changes: 19 additions & 1 deletion misfitapp/tests/responses/session_sessions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
{
"sessions": []
"sessions": [{
"id":"51a4189acf12e53f82000001",
"activityType":"Cycling",
"startTime":"2014-05-19T10:26:54-04:00",
"duration":900,
"points":210.8,
"steps":1406,
"calories":25.7325,
"distance":0.5125
}, {
"id":"51a4189acf12e53f82000002",
"activityType":"Swimming",
"startTime":"2014-05-20T18:26:54-04:00",
"duration":700,
"points":235.2,
"steps":1306,
"calories":205.76,
"distance":0.75
}]
}
17 changes: 16 additions & 1 deletion misfitapp/tests/responses/sleep_sleeps.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
{
"sleeps": []
"sleeps": [{
"id":"51a4189acf12e53f80000003",
"autoDetected": false,
"startTime":"2014-05-19T23:26:54+07:00",
"duration": 0,
"sleepDetails":[
{
"datetime":"2014-05-19T23:26:54+07:00",
"value":2
},
{
"datetime":"2014-05-19T23:59:22+07:00",
"value":1
}
]
}]
}
59 changes: 42 additions & 17 deletions misfitapp/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ def sns_subscribe(*args):


class JsonMock:
def __init__(self, file_name_base=None):
def __init__(self, file_name_base=None, date_range=None):
""" Build the response template """
self.headers = {'content-type': 'application/json; charset=utf-8'}
self.response_tmpl = {'status_code': 200, 'headers': self.headers}
self.file_name_base = file_name_base
self.date_range = date_range

def json_file(self):
response = self.response_tmpl
Expand All @@ -80,14 +81,21 @@ def device_http(self, url, *args):
path='/move/resource/v1/user/me/activity/goals/.*')
def goal_http(self, url, *args):
""" Method to return the contents of a goal json file """
self.file_name_base = 'goal_' + url.path.split('/')[-2]
return self.json_file()
if not self.file_name_base:
self.file_name_base = 'goal_' + url.path.split('/')[-2]
response = self.json_file()
# Reset file_name_base for future requests
self.file_name_base = None
else:
response = self.json_file()
return response

@urlmatch(scheme='https', netloc=r'api\.misfitwearables\.com',
path='/move/resource/v1/user/me/activity/sessions/.*')
def session_http(self, url, *args):
""" Method to return the contents of a session json file """
self.file_name_base = 'session_' + url.path.split('/')[-2]
if not self.file_name_base:
self.file_name_base = 'session_' + url.path.split('/')[-2]
return self.json_file()

@urlmatch(scheme='https', netloc=r'api\.misfitwearables\.com',
Expand All @@ -101,8 +109,20 @@ def profile_http(self, url, *args):
path='/move/resource/v1/user/me/activity/sleeps/.*')
def sleep_http(self, url, *args):
""" Method to return the contents of a sleep json file """
self.file_name_base = 'sleep_' + url.path.split('/')[-2]
return self.json_file()
if not self.file_name_base:
self.file_name_base = 'sleep_' + url.path.split('/')[-2]
if self.date_range:
# If a date range was specified, only return data when the query
# contains the specified range
if (url.query.find('start_date=%s' % self.date_range[0]) > -1 and
url.query.find('end_date=%s' % self.date_range[1]) > -1):
return self.json_file()
else:
response = self.response_tmpl
response['content'] = '{"sleeps": []}'.encode('utf8')
return response
else:
return self.json_file()

@urlmatch(scheme='https', netloc=r'api\.misfitwearables\.com',
path='/move/resource/v1/user/me/activity/summary/.*')
Expand All @@ -125,19 +145,30 @@ def setUp(self):
def test_import_historical(self, verify_signature_mock):
eq_(Profile.objects.filter(user=self.user).count(), 0)
eq_(Device.objects.filter(user=self.user).count(), 0)
eq_(Goal.objects.filter(user=self.user).count(), 0)
eq_(Summary.objects.filter(user=self.user).count(), 0)
eq_(Session.objects.filter(user=self.user).count(), 0)
eq_(Sleep.objects.filter(user=self.user).count(), 0)
sleep_range = ('2014-05-01', '2014-05-31')
sleep_mock = JsonMock('sleep_sleeps', date_range=sleep_range)
with HTTMock(JsonMock().profile_http,
JsonMock().device_http,
JsonMock('summary_summaries').summary_http,
JsonMock().goal_http,
JsonMock().session_http,
JsonMock().sleep_http,
JsonMock('summary_detail').summary_http,
JsonMock('goal_goals').goal_http,
JsonMock('session_sessions').session_http,
sleep_mock.sleep_http
):
with patch('celery.app.task.Task.delay') as mock_delay:
mock_delay.side_effect = lambda arg1, arg2: import_historical_cls(arg1, arg2)
import_historical(self.misfit_user)

eq_(Profile.objects.filter(user=self.user).count(), 1)
eq_(Device.objects.filter(user=self.user).count(), 1)
eq_(Goal.objects.filter(user=self.user).count(), 2)
eq_(Summary.objects.filter(user=self.user).count(), 3)
eq_(Session.objects.filter(user=self.user).count(), 2)
eq_(Sleep.objects.filter(user=self.user).count(), 1)
eq_(SleepSegment.objects.filter(sleep__user=self.user).count(), 2)

@freeze_time("2014-07-02 10:52:00", tz_offset=0)
@patch('misfit.notification.MisfitNotification.verify_signature')
Expand All @@ -157,13 +188,7 @@ def test_import_historical_rate_limit(self, mock_rand, mock_dbg, mock_dev,
exc = misfit_exceptions.MisfitRateLimitError(429, '', resp)
mock_dev.side_effect = exc
mock_retry.side_effect = BaseException
with HTTMock(JsonMock().profile_http,
JsonMock().device_http,
JsonMock('summary_summaries').summary_http,
JsonMock().goal_http,
JsonMock().session_http,
JsonMock().sleep_http,
):
with HTTMock(JsonMock().profile_http, JsonMock().device_http):
try:
import_historical(self.misfit_user)
assert False, 'Should have thrown an exception'
Expand Down

0 comments on commit 185a98c

Please sign in to comment.