def test_week_activity_distance_into_next_week(): # has an entry for the day after the end of week, should not be counted iso_date = '2020-06-18T21:58:33.302785-07:00' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S.%f%z').date() _, start_week, end_week = charting.get_week_bookends(my_date) activity = Activity(id=1, type=1, title='title', duration=25, distance=10, iso_timestamp='2020-06-15T21:58:33.302785-07:00') activity_1 = Activity(id=2, type=2, title='title', duration=32, distance=5, iso_timestamp='2020-06-22T05:58:33.302785-07:00') activities = [activity, activity_1] week_duration = charting.calc_daily_duration_per_exercise_type( activities, start_week, end_week, sum_by='distance') assert len(week_duration) == 6 assert len(week_duration[1]) == 7 assert week_duration[1] == [10, 0, 0, 0, 0, 0, 0] assert week_duration[2] == [0, 0, 0, 0, 0, 0, 0]
def test_week_activity_distance_end_week(): # has an entry on the exact last day of the week iso_date = '2020-06-18T21:58:33.302785-07:00' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S.%f%z').date() _, start_week, end_week = charting.get_week_bookends(my_date) activity = Activity(id=1, type=1, title='title', duration=25, distance=10.1, iso_timestamp='2020-06-15T21:58:33.302785-07:00') activity_1 = Activity(id=2, type=2, title='title', duration=32, distance=5.25, iso_timestamp='2020-06-21T05:58:33.302785-07:00') activities = [activity, activity_1] week_duration = charting.calc_daily_duration_per_exercise_type( activities, start_week, end_week, sum_by='distance') assert len(week_duration) == 6 assert len(week_duration[1]) == 7 assert week_duration[1] == [10.1, 0, 0, 0, 0, 0, 0] assert week_duration[2] == [0, 0, 0, 0, 0, 0, 5.25]
def exercise_log(offset=0, sum_by='duration'): """ shows the history of all activities performed by the user newest activity is shown first :return: :rtype: """ activities = Activity.query.filter_by( user_id=current_user.get_id()).order_by(desc(Activity.timestamp)) # get hold of the activities in the last week so they can be shown on the chart start_week_date_before, start_week_date, end_week_date = charting.get_week_bookends( None, week_offset=offset) # add on 2 days to the end of week as need to take into account activities # on that date activities_this_week = Activity.query.filter( Activity.timestamp >= start_week_date_before, Activity.timestamp <= (end_week_date + timedelta(days=2)), Activity.user_id == current_user.get_id()).all() # need to consider this as want the actual date for Monday when we are charting it # do need the UTC day before for getting the data to cover cases where the local time is ahead # of UTC chart_data = charting.get_chart_dataset(activities_this_week, start_week_date, sum_by=sum_by) # look at goals and whether they are met this week goals = Goal.query.filter_by(user_id=current_user.get_id()) _, current_week_start_date, current_week_end_date = charting.get_week_bookends( None, 0) goals_percentage_met = charting.compare_weekly_totals_to_goals( goals, activities, current_week_start_date, current_week_end_date) return render_template('activity/view_log.html', user=user, activities=activities, activities_lookup=ACTIVITIES_LOOKUP, icons=ICONS_LOOKUP, chart_data=str(chart_data), start_week=start_week_date.strftime("%b %d"), end_week=end_week_date.strftime("%b %d"), sum_by=sum_by, offset=offset, goals=goals, goals_percentage_met=goals_percentage_met, title="View Exercise Log")
def test_compare_weekly_totals_to_goals_params(goals, activities, expected_percentages): iso_date = '2020-06-18T21:58:33.302785-07:00' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S.%f%z').date() _, start_week, _ = charting.get_week_bookends(my_date) percentages = charting.compare_weekly_totals_to_goals( goals, activities, start_week) assert percentages == expected_percentages
def test_calc_weekly_totals(): iso_date = '2020-06-18T21:58:33.302785-07:00' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S.%f%z').date() _, start_week, _ = charting.get_week_bookends(my_date) activities = [ Activity(id=1, type=1, title='title', duration=20, iso_timestamp='2020-06-16T04:58:33.302785+05:00'), Activity(id=2, type=4, title='title', distance=10, duration=15, iso_timestamp='2020-06-17T04:58:33.302785+05:00'), Activity(id=3, type=1, title='title', duration=34, iso_timestamp='2020-06-18T04:58:33.302785+05:00'), Activity(id=4, type=3, title='title', duration=25, iso_timestamp='2020-06-19T04:58:33.302785+05:00') ] total_count_all_activities, total_count_by_exercise_type, \ total_duration_all_activities, total_duration_by_exercise_type, \ total_distance_all_activities, total_distance_by_exercise_type = \ charting.calc_weekly_totals(activities, start_week) assert total_count_all_activities == 4 assert total_count_by_exercise_type == {1: 2, 2: 0, 3: 1, 4: 1, 5: 0, 6: 0} assert total_duration_all_activities == 94 assert total_distance_all_activities == 10 assert total_duration_by_exercise_type == { 1: 54, 2: 0, 3: 25, 4: 15, 5: 0, 6: 0 } assert total_distance_by_exercise_type == { 1: 0, 2: 0, 3: 0, 4: 10, 5: 0, 6: 0 }
def test_get_week_bookends_offset(): iso_date = '2020-06-18T21:58:33.302785-07:00' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S.%f%z').date() start_week_prior, start_week, end_week = charting.get_week_bookends( my_date, week_offset=1) assert start_week_prior.month == 6 assert start_week_prior.day == 7 assert start_week.month == 6 assert start_week.day == 8 assert end_week.month == 6 assert end_week.day == 14
def test_week_activity_distance_positive_timezone(): # has an entry for a timezone ahead of UTC iso_date = '2020-06-18T21:58:33.302785-07:00' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S.%f%z').date() _, start_week, end_week = charting.get_week_bookends(my_date) activity = Activity(id=1, type=1, title='title', duration=25, distance=10, iso_timestamp='2020-06-15T04:58:33.302785+05:00') activities = [activity] week_duration = charting.calc_daily_duration_per_exercise_type( activities, start_week, end_week, sum_by='distance') assert len(week_duration) == 6 assert len(week_duration[1]) == 7 assert week_duration[1] == [10, 0, 0, 0, 0, 0, 0]