Changeset 2d91474 in OpenWorkouts-current for ow/tests


Ignore:
Timestamp:
Jan 15, 2019, 10:13:57 PM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
0c18869
Parents:
9bee49d
Message:

(#23) - Show workouts in the dashboard with date filtering

Location:
ow/tests
Files:
2 edited

Legend:

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

    r9bee49d r2d91474  
     1from datetime import datetime, timedelta, timezone
     2
    13import pytest
    24from pyramid.security import Allow
     
    6870        assert list(root['john'].workout_ids()) == ['1', '2', '3']
    6971        assert root['john'].num_workouts == len(workouts)
     72
     73    def test_activity_dates_tree(self, root):
     74        # first an empty test
     75        assert root['john'].activity_dates_tree == {}
     76        # now add a cycling workout in a given date (25/11/2018)
     77        workout = Workout(
     78            start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc),
     79            duration=timedelta(minutes=(60*4)),
     80            distance=115,
     81            sport='cycling')
     82        root['john'].add_workout(workout)
     83        assert root['john'].activity_dates_tree == {
     84            2018: {11: {'cycling': 1}}
     85        }
     86        # add a running workout on the same date
     87        workout = Workout(
     88            start=datetime(2018, 11, 25, 16, 30, tzinfo=timezone.utc),
     89            duration=timedelta(minutes=60),
     90            distance=12,
     91            sport='running')
     92        root['john'].add_workout(workout)
     93        assert root['john'].activity_dates_tree == {
     94            2018: {11: {'cycling': 1, 'running': 1}}
     95        }
     96        # add a swimming workout on a different date, same year
     97        workout = Workout(
     98            start=datetime(2018, 8, 15, 11, 30, tzinfo=timezone.utc),
     99            duration=timedelta(minutes=30),
     100            distance=2,
     101            sport='swimming')
     102        root['john'].add_workout(workout)
     103        assert root['john'].activity_dates_tree == {
     104            2018: {8: {'swimming': 1},
     105                   11: {'cycling': 1, 'running': 1}}
     106        }
     107        # now add some more cycling in a different year
     108        # add a swimming workout on a different date, same year
     109        workout = Workout(
     110            start=datetime(2017, 4, 15, 15, 00, tzinfo=timezone.utc),
     111            duration=timedelta(minutes=(60*3)),
     112            distance=78,
     113            sport='cycling')
     114        root['john'].add_workout(workout)
     115        assert root['john'].activity_dates_tree == {
     116            2017: {4: {'cycling': 1}},
     117            2018: {8: {'swimming': 1},
     118                   11: {'cycling': 1, 'running': 1}}
     119        }
  • ow/tests/views/test_user.py

    r9bee49d r2d91474  
    152152        request = dummy_request
    153153        response = user_views.dashboard(john, request)
    154         assert response == {}
     154        assert len(response) == 4
     155        assert 'month_name' in response.keys()
     156        # this user has a single workout, in 2015
     157        assert response['viewing_year'] == 2015
     158        assert response['viewing_month'] == 6
     159        assert response['workouts'] == [w for w in john.workouts()]
     160
     161    def test_dashboard_year(self, dummy_request, john):
     162        """
     163        Renders the user dashboard for a chosen year.
     164        """
     165        request = dummy_request
     166        # first test the year for which we know there is a workout
     167        request.GET['year'] = 2015
     168        response = user_views.dashboard(john, request)
     169        assert len(response) == 4
     170        assert 'month_name' in response.keys()
     171        # this user has a single workout, in 2015
     172        assert response['viewing_year'] == 2015
     173        assert response['viewing_month'] == 6
     174        assert response['workouts'] == [w for w in john.workouts()]
     175        # now, a year we know there is no workout info
     176        request.GET['year'] = 2000
     177        response = user_views.dashboard(john, request)
     178        assert len(response) == 4
     179        assert 'month_name' in response.keys()
     180        # this user has a single workout, in 2015
     181        assert response['viewing_year'] == 2000
     182        # we have no data for that year and we didn't ask for a certain month,
     183        # so the passing value for that is None
     184        assert response['viewing_month'] is None
     185        assert response['workouts'] == []
     186
     187    def test_dashboard_year_month(self, dummy_request, john):
     188        """
     189        Renders the user dashboard for a chosen year and month.
     190        """
     191        request = dummy_request
     192        # first test the year/month for which we know there is a workout
     193        request.GET['year'] = 2015
     194        request.GET['month'] = 6
     195        response = user_views.dashboard(john, request)
     196        assert len(response) == 4
     197        assert 'month_name' in response.keys()
     198        # this user has a single workout, in 2015
     199        assert response['viewing_year'] == 2015
     200        assert response['viewing_month'] == 6
     201        assert response['workouts'] == [w for w in john.workouts()]
     202        # now, change month to one without values
     203        request.GET['month'] = 2
     204        response = user_views.dashboard(john, request)
     205        assert len(response) == 4
     206        assert 'month_name' in response.keys()
     207        # this user has a single workout, in 2015
     208        assert response['viewing_year'] == 2015
     209        assert response['viewing_month'] == 2
     210        assert response['workouts'] == []
     211        # now the month with data, but in a different year
     212        request.GET['year'] = 2010
     213        request.GET['month'] = 6
     214        response = user_views.dashboard(john, request)
     215        assert len(response) == 4
     216        assert 'month_name' in response.keys()
     217        # this user has a single workout, in 2015
     218        assert response['viewing_year'] == 2010
     219        assert response['viewing_month'] == 6
     220        assert response['workouts'] == []
     221
     222    def test_dashboard_month(self, dummy_request, john):
     223        """
     224        Passing a month without a year when rendering the dashboard. The last
     225        year for which workout data is available is assumed
     226        """
     227        request = dummy_request
     228        # Set a month without workout data
     229        request.GET['month'] = 5
     230        response = user_views.dashboard(john, request)
     231        assert len(response) == 4
     232        assert 'month_name' in response.keys()
     233        # this user has a single workout, in 2015
     234        assert response['viewing_year'] == 2015
     235        assert response['viewing_month'] == 5
     236        assert response['workouts'] == []
     237        # now a month with data
     238        request.GET['month'] = 6
     239        response = user_views.dashboard(john, request)
     240        assert len(response) == 4
     241        assert 'month_name' in response.keys()
     242        # this user has a single workout, in 2015
     243        assert response['viewing_year'] == 2015
     244        assert response['viewing_month'] == 6
     245        assert response['workouts'] == [w for w in john.workouts()]
    155246
    156247    def test_profile(self, dummy_request, john):
Note: See TracChangeset for help on using the changeset viewer.