def test_updates_single_account(self, make_request): make_request.return_value = { 'activities-heart-intraday': { 'dataset': [{ 'time': '10:00:00', 'value': 77 }] } } other_account = FitbitAccount.objects.create(fitbit_user="******") FitbitMinuteHeartRate.objects.create(account=other_account, time=datetime.now().astimezone( pytz.UTC), heart_rate=20) FitbitMinuteHeartRate.objects.create(account=self.account, time=datetime.now().astimezone( pytz.UTC), heart_rate=88) service = FitbitDayService(account=self.account, date=date.today()) service.update_heart_rate() self.assertEqual(FitbitMinuteHeartRate.objects.count(), 2) # There should only be one of these heart_rate = FitbitMinuteHeartRate.objects.get(account=self.account) self.assertEqual(heart_rate.heart_rate, 77)
def testUpdateActivitiesOnDay(self, make_request): make_request.return_value = { 'activities': [{ 'logId': 123, 'startTime': '2018-02-14T07:07:00.000-7:00', 'duration': 1200000, 'averageHeartRate': 94, 'activityTypeId': 123456, 'activityName': 'Foo Ball' }], 'pagination': { 'next': '' } } service = FitbitDayService(account=self.account, date=date(2018, 2, 14)) service.update_activities() make_request.assert_called_with( url= 'https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2018-02-14&offset=0&limit=20&sort=asc', method=None, data=None) activity = FitbitActivity.objects.get() self.assertEqual(activity.fitbit_id, '123') self.assertEqual(activity.type.name, 'Foo Ball') self.assertEqual(activity.average_heart_rate, 94) self.assertEqual(activity.end_time, datetime(2018, 2, 14, 14, 27, tzinfo=pytz.UTC))
def get(self, request, day): date = self.parse_date(day) self.validate_date(request.user, date) try: service = FitbitDayService(date=date, user=request.user) service.update() except: return Response('Fitbit update failed', status=status.HTTP_400_BAD_REQUEST) summary = get_summary(request.user, date) serialized = DaySerializer(summary) return Response(serialized.data, status=status.HTTP_200_OK)
def fitbit_day(self): if not hasattr(self, '_fitbit_day'): try: fitbitday_service = FitbitDayService(self.date, user=self.user) self._fitbit_day = fitbitday_service.day except FitbitDayService.NoAccount: self._fitbit_day = None return self._fitbit_day
def test_process_heart_rate(self, make_request): make_request.return_value = { 'activities-heart-intraday': { 'dataset': [{ 'time': '10:00:00', 'value': 77 }, { 'time': '10:01:00', 'value': 60 }, { 'time': '11:00:00', 'value': 100 }] } } service = FitbitDayService(account=self.account, date=date.today()) service.update_heart_rate() self.assertEqual(FitbitMinuteHeartRate.objects.count(), 3)
def testUpdatesSteps(self): self.make_request.return_value = { 'activities-steps-intraday': { 'dataset': [{ 'time': '10:10:00', 'value': 5 }, { 'time': '13:15:00', 'value': 5 }, { 'time': '13:16:00', 'value': 0 }] } } service = FitbitDayService(account=self.account, date=date(2018, 2, 14)) step_count = service.update_steps() self.assertEqual(step_count, 10) self.make_request.assert_called_with( url= 'https://api.fitbit.com/1/user/-/activities/steps/date/2018-02-14/1d/1min.json', method=None, data=None) self.assertEqual(FitbitMinuteStepCount.objects.count(), 2) self.make_request.return_value = { 'activities-steps-intraday': { 'dataset': [{ 'time': '10:10:00', 'value': 15 }] } } step_count = service.update_steps() self.assertEqual(step_count, 15) self.assertEqual(FitbitMinuteStepCount.objects.count(), 1)
def testUpdatesDistance(self, make_request): make_request.return_value = { 'activities-distance-intraday': { 'dataset': [{ 'time': '10:00:00', 'value': '0.57' }, { 'time': '11:00:00', 'value': '0.33' }] } } service = FitbitDayService(account=self.account, date=date(2018, 2, 14)) distance = service.update_distance() self.assertEqual(float(distance), 0.9) make_request.assert_called_with( url= 'https://api.fitbit.com/1/user/-/activities/distance/date/2018-02-14/1d/1min.json', method=None, data=None)
def testUpdateOnlySingleAccount(self): self.make_request.return_value = { 'activities-steps-intraday': { 'dataset': [{ 'time': '10:10:00', 'value': 15 }] } } other_account = FitbitAccount.objects.create(fitbit_user="******") FitbitMinuteStepCount.objects.create(account=other_account, steps=150, time=datetime( 2018, 2, 14, 12, 12).astimezone(pytz.UTC)) service = FitbitDayService(account=self.account, date=date(2018, 2, 14)) step_count = service.update_steps() self.assertEqual(step_count, 15) self.assertEqual(FitbitMinuteStepCount.objects.count(), 2)