Changeset 778d53d in OpenWorkouts-current for ow/tests


Ignore:
Timestamp:
Mar 5, 2019, 11:45:32 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current
Children:
aa6dcaf
Parents:
35953eb
Message:

(#7) Show per-sport stats in the profile page:

  • Show a dropdown list of sports for which the user has activities. By default we choose the sport with most activities.
  • Show a dropdown list of years for which the user has activities. By default we show stats for the current year. If the user picks up a different year, we show the totals (distance, time, elevation, number of workouts) for that year.
  • Show the totals of all time for the chosen sport
Location:
ow/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/models/test_user.py

    r35953eb r778d53d  
    8181        assert list(root['john'].workout_ids()) == ['1', '2', '3']
    8282        assert root['john'].num_workouts == len(workouts)
     83
     84    def test_favorite_sport(self, root):
     85        assert root['john'].favorite_sport is None
     86        # add a cycling workout
     87        workout = Workout(
     88            sport='cycling',
     89            start=datetime.now(timezone.utc),
     90            duration=timedelta(minutes=120),
     91            distance=66,
     92        )
     93        root['john'].add_workout(workout)
     94        assert root['john'].favorite_sport == 'cycling'
     95        # add a running workout, both sports have same amount of workouts,
     96        # favorite is picked up reversed alphabetically
     97        workout = Workout(
     98            sport='running',
     99            start=datetime.now(timezone.utc),
     100            duration=timedelta(minutes=45),
     101            distance=5,
     102        )
     103        root['john'].add_workout(workout)
     104        assert root['john'].favorite_sport == 'running'
     105        # add another cycling workout, now that is the favorite sport
     106        workout = Workout(
     107            sport='cycling',
     108            start=datetime.now(timezone.utc),
     109            duration=timedelta(minutes=60),
     110            distance=30,
     111        )
     112        root['john'].add_workout(workout)
     113        assert root['john'].favorite_sport == 'cycling'
     114
     115    def test_activity_sports(self, root):
     116        assert root['john'].activity_sports == []
     117        workout = Workout(
     118            sport='cycling',
     119            start=datetime.now(timezone.utc),
     120            duration=timedelta(minutes=120),
     121            distance=66,
     122        )
     123        root['john'].add_workout(workout)
     124        assert root['john'].activity_sports == ['cycling']
     125        workout = Workout(
     126            sport='running',
     127            start=datetime.now(timezone.utc),
     128            duration=timedelta(minutes=45),
     129            distance=5,
     130        )
     131        root['john'].add_workout(workout)
     132        assert root['john'].activity_sports == ['cycling', 'running']
     133
     134    def test_activity_years(self, root):
     135        assert root['john'].activity_years == []
     136        workout = Workout(
     137            sport='cycling',
     138            start=datetime.now(timezone.utc),
     139            duration=timedelta(minutes=120),
     140            distance=66,
     141        )
     142        root['john'].add_workout(workout)
     143        assert root['john'].activity_years == [datetime.now(timezone.utc).year]
     144        workout = Workout(
     145            sport='running',
     146            start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc),
     147            duration=timedelta(minutes=45),
     148            distance=5,
     149        )
     150        root['john'].add_workout(workout)
     151        assert root['john'].activity_years == [
     152            datetime.now(timezone.utc).year,
     153            2018
     154        ]
     155
     156    def test_activity_months(self, root):
     157        # we have to pass a year parameter
     158        with pytest.raises(TypeError):
     159            root['john'].activity_months()
     160        now = datetime.now(timezone.utc)
     161        assert root['john'].activity_months(now.year) == []
     162        workout = Workout(
     163            sport='cycling',
     164            start=datetime.now(timezone.utc),
     165            duration=timedelta(minutes=120),
     166            distance=66,
     167        )
     168        root['john'].add_workout(workout)
     169        assert root['john'].activity_months(now.year) == [now.month]
     170        assert root['john'].activity_months(now.year-1) == []
     171        assert root['john'].activity_months(now.year+1) == []
     172        workout = Workout(
     173            sport='running',
     174            start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc),
     175            duration=timedelta(minutes=45),
     176            distance=5,
     177        )
     178        root['john'].add_workout(workout)
     179        assert root['john'].activity_months(now.year) == [now.month]
     180        assert root['john'].activity_months(2018) == [11]
     181        assert root['john'].activity_months(now.year+1) == []
    83182
    84183    def test_activity_dates_tree(self, root):
     
    552651            else:
    553652                assert stats == expected_no_stats_per_week
     653
     654    def test_sport_totals(self, root):
     655        # user has no workouts, so no totals
     656        assert root['john'].sport_totals() == {
     657            'workouts': 0,
     658            'time': timedelta(0),
     659            'distance': Decimal(0),
     660            'elevation': Decimal(0),
     661        }
     662        # add a cycling workout happening now
     663        workout = Workout(
     664            sport='cycling',
     665            start=datetime.now(timezone.utc),
     666            duration=timedelta(minutes=120),
     667            distance=66,
     668        )
     669        root['john'].add_workout(workout)
     670        # only one workout, one sport, so the default will show totals
     671        # for that sport
     672        assert root['john'].sport_totals() == {
     673            'workouts': 1,
     674            'time': timedelta(minutes=120),
     675            'distance': Decimal(66),
     676            'elevation': Decimal(0),
     677        }
     678        # Add a running workout
     679        workout = Workout(
     680            sport='running',
     681            start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc),
     682            duration=timedelta(minutes=45),
     683            distance=5,
     684        )
     685        root['john'].add_workout(workout)
     686        # the favorite sport is running now
     687        assert root['john'].sport_totals() == {
     688            'workouts': 1,
     689            'time': timedelta(minutes=45),
     690            'distance': Decimal(5),
     691            'elevation': Decimal(0),
     692        }
     693        # but we can get the totals for cycling too
     694        assert root['john'].sport_totals('cycling') == {
     695            'workouts': 1,
     696            'time': timedelta(minutes=120),
     697            'distance': Decimal(66),
     698            'elevation': Decimal(0),
     699        }
     700        # adding a new cycling workout, in a different year
     701        workout = Workout(
     702            sport='cycling',
     703            start=datetime(2017, 11, 25, 10, 00, tzinfo=timezone.utc),
     704            duration=timedelta(minutes=60),
     705            distance=32,
     706        )
     707        root['john'].add_workout(workout)
     708        # now cycling is the favorite sport
     709        assert root['john'].sport_totals() == {
     710            'workouts': 2,
     711            'time': timedelta(minutes=180),
     712            'distance': Decimal(98),
     713            'elevation': Decimal(0),
     714        }
     715        # but we can get running stats too
     716        assert root['john'].sport_totals('running') == {
     717            'workouts': 1,
     718            'time': timedelta(minutes=45),
     719            'distance': Decimal(5),
     720            'elevation': Decimal(0),
     721        }
     722        # there are no running activities for 2016
     723        assert root['john'].sport_totals('running', 2016) == {
     724            'workouts': 0,
     725            'time': timedelta(0),
     726            'distance': Decimal(0),
     727            'elevation': Decimal(0),
     728        }
     729        # and not activities for cycling in 2016 neither
     730        assert root['john'].sport_totals('cycling', 2016) == {
     731            'workouts': 0,
     732            'time': timedelta(0),
     733            'distance': Decimal(0),
     734            'elevation': Decimal(0),
     735        }
     736        # and we can get the separate totals for cycling in different years
     737        year = datetime.now(timezone.utc).year
     738        assert root['john'].sport_totals('cycling', year) == {
     739            'workouts': 1,
     740            'time': timedelta(minutes=120),
     741            'distance': Decimal(66),
     742            'elevation': Decimal(0),
     743        }
     744        assert root['john'].sport_totals('cycling', 2017) == {
     745            'workouts': 1,
     746            'time': timedelta(minutes=60),
     747            'distance': Decimal(32),
     748            'elevation': Decimal(0),
     749        }
  • ow/tests/views/test_user.py

    r35953eb r778d53d  
    4646            start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc),
    4747            duration=timedelta(minutes=60),
    48             distance=30
     48            distance=30,
     49            sport='cycling'
    4950        )
    5051        john.add_workout(workout)
     
    413414        # profile page for the current day (no workouts avalable)
    414415        response = user_views.profile(john, request)
    415         assert len(response.keys()) == 6
     416        assert len(response.keys()) == 7
    416417        current_month = datetime.now(timezone.utc).strftime('%Y-%m')
    417418        assert response['user'] == john
     
    425426            'elevation': Decimal(0)
    426427        }
     428        assert response['profile_stats'] == {
     429            'sports': ['cycling'],
     430            'years': [2015],
     431            'current_year': datetime.now(timezone.utc).year,
     432            'current_sport': 'cycling'
     433        }
    427434        # profile page for a previous date, that has workouts
    428435        request.GET['year'] = 2015
    429436        request.GET['month'] = 6
    430437        response = user_views.profile(john, request)
    431         assert len(response.keys()) == 6
     438        assert len(response.keys()) == 7
    432439        assert response['user'] == john
    433440        assert response['user_gender'] == 'Robot'
     
    446453        request.GET['week'] = 25
    447454        response = user_views.profile(john, request)
    448         assert len(response.keys()) == 6
     455        assert len(response.keys()) == 7
    449456        assert response['user'] == john
    450457        assert response['user_gender'] == 'Robot'
     
    462469        request.GET['week'] = 26
    463470        response = user_views.profile(john, request)
    464         assert len(response.keys()) == 6
     471        assert len(response.keys()) == 7
    465472        assert response['user'] == john
    466473        assert response['user_gender'] == 'Robot'
Note: See TracChangeset for help on using the changeset viewer.