Changeset 24596da in OpenWorkouts-current for ow


Ignore:
Timestamp:
Feb 27, 2019, 6:28:07 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
cef474f
Parents:
68d00f5
Message:

Added missing tests, raised overall coverage

Location:
ow
Files:
5 edited

Legend:

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

    r68d00f5 r24596da  
    112112            user = root.get_user_by_email(Mock())
    113113
     114    def test_get_user_by_nickname(self, root, john):
     115        # set a nickname for john
     116        john.nickname = 'JohnDoe'
     117        root.reindex(john)
     118        user = root.get_user_by_nickname('JohnDoe')
     119        assert user == john
     120        user = root.get_user_by_nickname('NonExistant')
     121        assert user is None
     122        # passing in None
     123        user = root.get_user_by_nickname(None)
     124        assert user is None
     125        # passing in something that is not None or a string will break
     126        # the query code
     127        with pytest.raises(TypeError):
     128            user = root.get_user_by_nickname(False)
     129        with pytest.raises(TypeError):
     130            user = root.get_user_by_nickname(Mock())
     131
    114132    def test_users(self, root, john):
    115133        assert root.users == [john]
  • ow/tests/models/test_user.py

    r68d00f5 r24596da  
    348348            'time': timedelta(0)
    349349        }
     350
     351    def test_yearly_stats(self, root):
     352        expected_no_stats_per_month = {
     353            'workouts': 0,
     354            'time': timedelta(0),
     355            'distance': Decimal(0),
     356            'elevation': Decimal(0),
     357            'sports': {}
     358        }
     359
     360        yearly_stats = root['john'].yearly_stats
     361        for month, stats in yearly_stats.items():
     362            assert stats == expected_no_stats_per_month
     363
     364        # add a cycling workout
     365        start_date = datetime.now(timezone.utc)
     366        workout = Workout(
     367            start=start_date,
     368            duration=timedelta(minutes=(60*4)),
     369            uphill=1200,
     370            distance=115,
     371            sport='cycling')
     372        root['john'].add_workout(workout)
     373
     374        yearly_stats = root['john'].yearly_stats
     375        for month, stats in yearly_stats.items():
     376            if month == (start_date.year, start_date.month):
     377                assert stats == {
     378                    'workouts': 1,
     379                    'time': timedelta(minutes=(60*4)),
     380                    'distance': Decimal(115),
     381                    'elevation': Decimal(1200),
     382                    'sports': {
     383                        'cycling': {'distance': Decimal(115),
     384                                    'elevation': Decimal(1200),
     385                                    'time': timedelta(minutes=(60*4)),
     386                                    'workouts': 1}
     387                    }
     388                }
     389            else:
     390                assert stats == expected_no_stats_per_month
     391
     392        # add a second cycling workout
     393        workout = Workout(
     394            start=start_date,
     395            duration=timedelta(minutes=(30)),
     396            uphill=500,
     397            distance=20,
     398            sport='cycling')
     399        root['john'].add_workout(workout)
     400
     401        yearly_stats = root['john'].yearly_stats
     402        for month, stats in yearly_stats.items():
     403            if month == (start_date.year, start_date.month):
     404                assert stats == {
     405                    'workouts': 2,
     406                    'time': timedelta(minutes=((60*4)+30)),
     407                    'distance': Decimal(115+20),
     408                    'elevation': Decimal(1200+500),
     409                    'sports': {
     410                        'cycling': {'distance': Decimal(115+20),
     411                                    'elevation': Decimal(1200+500),
     412                                    'time': timedelta(minutes=((60*4)+30)),
     413                                    'workouts': 2}
     414                    }
     415                }
     416            else:
     417                assert stats == expected_no_stats_per_month
     418
     419        # add a running workout
     420        workout = Workout(
     421            start=start_date,
     422            duration=timedelta(minutes=(60)),
     423            uphill=200,
     424            distance=5,
     425            sport='running')
     426        root['john'].add_workout(workout)
     427
     428        yearly_stats = root['john'].yearly_stats
     429        for month, stats in yearly_stats.items():
     430            if month == (start_date.year, start_date.month):
     431                assert stats == {
     432                    'workouts': 3,
     433                    'time': timedelta(minutes=((60*4)+30+60)),
     434                    'distance': Decimal(115+20+5),
     435                    'elevation': Decimal(1200+500+200),
     436                    'sports': {
     437                        'cycling': {'distance': Decimal(115+20),
     438                                    'elevation': Decimal(1200+500),
     439                                    'time': timedelta(minutes=((60*4)+30)),
     440                                    'workouts': 2},
     441                        'running': {'distance': Decimal(5),
     442                                    'elevation': Decimal(200),
     443                                    'time': timedelta(minutes=(60)),
     444                                    'workouts': 1}
     445                    }
     446                }
     447            else:
     448                assert stats == expected_no_stats_per_month
     449
     450    def test_weekly_year_stats(self, root):
     451        expected_no_stats_per_week = {
     452            'workouts': 0,
     453            'time': timedelta(0),
     454            'distance': Decimal(0),
     455            'elevation': Decimal(0),
     456            'sports': {}
     457        }
     458
     459        weekly_year_stats = root['john'].weekly_year_stats
     460        for week, stats in weekly_year_stats.items():
     461            assert stats == expected_no_stats_per_week
     462
     463        # add a cycling workout
     464        start_date = datetime.now(timezone.utc)
     465        workout = Workout(
     466            start=start_date,
     467            duration=timedelta(minutes=(60*4)),
     468            uphill=1200,
     469            distance=115,
     470            sport='cycling')
     471        root['john'].add_workout(workout)
     472
     473        weekly_year_stats = root['john'].weekly_year_stats
     474        workout_week = (start_date.year, start_date.month,
     475                        start_date.isocalendar()[1])
     476        for week, stats in weekly_year_stats.items():
     477            if week[:3] == workout_week:
     478                assert stats == {
     479                    'workouts': 1,
     480                    'time': timedelta(minutes=(60*4)),
     481                    'distance': Decimal(115),
     482                    'elevation': Decimal(1200),
     483                    'sports': {
     484                        'cycling': {'distance': Decimal(115),
     485                                    'elevation': Decimal(1200),
     486                                    'time': timedelta(minutes=(60*4)),
     487                                    'workouts': 1}
     488                    }
     489                }
     490            else:
     491                assert stats == expected_no_stats_per_week
     492
     493        # add a second cycling workout
     494        workout = Workout(
     495            start=start_date,
     496            duration=timedelta(minutes=(30)),
     497            uphill=500,
     498            distance=20,
     499            sport='cycling')
     500        root['john'].add_workout(workout)
     501
     502        weekly_year_stats = root['john'].weekly_year_stats
     503        workout_week = (start_date.year, start_date.month,
     504                        start_date.isocalendar()[1])
     505        for week, stats in weekly_year_stats.items():
     506            if week[:3] == workout_week:
     507                assert stats == {
     508                    'workouts': 2,
     509                    'time': timedelta(minutes=((60*4)+30)),
     510                    'distance': Decimal(115+20),
     511                    'elevation': Decimal(1200+500),
     512                    'sports': {
     513                        'cycling': {'distance': Decimal(115+20),
     514                                    'elevation': Decimal(1200+500),
     515                                    'time': timedelta(minutes=((60*4)+30)),
     516                                    'workouts': 2}
     517                    }
     518                }
     519            else:
     520                assert stats == expected_no_stats_per_week
     521
     522        # add a running workout
     523        workout = Workout(
     524            start=start_date,
     525            duration=timedelta(minutes=(60)),
     526            uphill=200,
     527            distance=5,
     528            sport='running')
     529        root['john'].add_workout(workout)
     530
     531        weekly_year_stats = root['john'].weekly_year_stats
     532        workout_week = (start_date.year, start_date.month,
     533                        start_date.isocalendar()[1])
     534        for week, stats in weekly_year_stats.items():
     535            if week[:3] == workout_week:
     536                assert stats == {
     537                    'workouts': 3,
     538                    'time': timedelta(minutes=((60*4)+30+60)),
     539                    'distance': Decimal(115+20+5),
     540                    'elevation': Decimal(1200+500+200),
     541                    'sports': {
     542                        'cycling': {'distance': Decimal(115+20),
     543                                    'elevation': Decimal(1200+500),
     544                                    'time': timedelta(minutes=((60*4)+30)),
     545                                    'workouts': 2},
     546                        'running': {'distance': Decimal(5),
     547                                    'elevation': Decimal(200),
     548                                    'time': timedelta(minutes=(60)),
     549                                    'workouts': 1}
     550                    }
     551                }
     552            else:
     553                assert stats == expected_no_stats_per_week
  • ow/tests/views/test_user.py

    r68d00f5 r24596da  
    1313
    1414from pyramid.testing import DummyRequest
    15 from pyramid.httpexceptions import HTTPFound
     15from pyramid.httpexceptions import HTTPFound, HTTPNotFound
    1616from pyramid.response import Response
    1717
     
    475475        }
    476476
     477    def test_profile_with_nickname(self, dummy_request, john):
     478        """
     479        Loading the profile page using the user nickname
     480        """
     481        request = dummy_request
     482        john.nickname = 'JohnDoe'
     483        request.root.reindex(john)
     484        # first load using the uuid
     485        response_uuid = user_views.profile(john, request)
     486        # now, do it with the nickname
     487        request.subpath = ['JohnDoe']
     488        response_nickname = user_views.profile(request.root, request)
     489        assert response_uuid == response_nickname
     490        # try an unknown nickname, HTTPNotFound is returned
     491        request.subpath = ['Invalid']
     492        response_nickname = user_views.profile(request.root, request)
     493        assert isinstance(response_nickname, HTTPNotFound)
     494
    477495    def test_login_get(self, dummy_request):
    478496        """
     
    675693        returned_image = Image.open(BytesIO(response.body))
    676694        assert original_image.size == returned_image.size
     695
     696    def test_profile_picture_none(self, dummy_request, john):
     697        """
     698        GET request for an user without a profile picture
     699        """
     700        request = dummy_request
     701        user = john
     702        response = user_views.profile_picture(user, request)
     703        assert isinstance(response, HTTPNotFound)
    677704
    678705    def test_edit_profile_get(self, dummy_request, john):
     
    10021029                day['time'] == '00'
    10031030                day['workouts'] == 0
     1031
     1032    def test_last_months_stats(self, dummy_request, john):
     1033        request = dummy_request
     1034        user = john
     1035        response = user_views.last_months_stats(user, request)
     1036        assert isinstance(response, Response)
     1037        assert response.content_type == 'application/json'
     1038        # the body is a valid json-encoded stream
     1039        obj = json.loads(response.body)
     1040        assert len(obj) == 13
     1041        for month in obj:
     1042            assert len(month.keys()) == 7
     1043            assert 'id' in month.keys()
     1044            assert 'name' in month.keys()
     1045            assert month['distance'] == 0
     1046            assert month['elevation'] == 0
     1047            assert month['time'] == '00'
     1048            assert month['workouts'] == 0
     1049            assert str(user.uid) in month['url']
     1050            assert 'profile' in month['url']
     1051            assert 'year' in month['url']
     1052            assert 'month' in month['url']
     1053
     1054        # try now with a workout
     1055        workout_start = datetime.now(timezone.utc)
     1056        workout_start_id = (
     1057            str(workout_start.year) + '-' + str(workout_start.month).zfill(2))
     1058        workout = Workout(
     1059            sport='cycling',
     1060            start=workout_start,
     1061            duration=timedelta(minutes=60),
     1062            distance=30,
     1063            uphill=540
     1064        )
     1065        user.add_workout(workout)
     1066        response = user_views.last_months_stats(user, request)
     1067        assert isinstance(response, Response)
     1068        assert response.content_type == 'application/json'
     1069        # the body is a valid json-encoded stream
     1070        obj = json.loads(response.body)
     1071        assert len(obj) == 13
     1072        for month in obj:
     1073            assert len(month.keys()) == 7
     1074            assert 'id' in month.keys()
     1075            assert 'name' in month.keys()
     1076            assert str(user.uid) in month['url']
     1077            assert 'profile' in month['url']
     1078            assert 'year' in month['url']
     1079            assert 'month' in month['url']
     1080            if month['id'] == workout_start_id:
     1081                assert month['distance'] == 30
     1082                assert month['elevation'] == 540
     1083                assert month['time'] == '01'
     1084                assert month['workouts'] == 1
     1085            else:
     1086                assert month['distance'] == 0
     1087                assert month['elevation'] == 0
     1088                assert month['time'] == '00'
     1089                assert month['workouts'] == 0
     1090
     1091    def test_last_weeks_stats(self, dummy_request, john):
     1092        request = dummy_request
     1093        user = john
     1094        response = user_views.last_weeks_stats(user, request)
     1095        assert isinstance(response, Response)
     1096        assert response.content_type == 'application/json'
     1097        # the body is a valid json-encoded stream
     1098        obj = json.loads(response.body)
     1099        assert len(obj) == 68
     1100        for week in obj:
     1101            assert len(week.keys()) == 8
     1102            assert 'id' in week.keys()
     1103            assert 'name' in week.keys()
     1104            assert 'week' in week.keys()
     1105            assert week['distance'] == 0
     1106            assert week['elevation'] == 0
     1107            assert week['time'] == '00'
     1108            assert week['workouts'] == 0
     1109            assert str(user.uid) in week['url']
     1110            assert 'profile' in week['url']
     1111            assert 'year' in week['url']
     1112            assert 'month' in week['url']
     1113            assert 'week' in week['url']
     1114
     1115        # try now with a workout
     1116        workout_start = datetime.now(timezone.utc)
     1117        workout_start_id = str(workout_start.year)
     1118        workout_start_id += '-' + str(workout_start.month).zfill(2)
     1119        workout_start_id += '-' + str(workout_start.isocalendar()[1])
     1120        workout = Workout(
     1121            sport='cycling',
     1122            start=workout_start,
     1123            duration=timedelta(minutes=60),
     1124            distance=30,
     1125            uphill=540
     1126        )
     1127        user.add_workout(workout)
     1128        response = user_views.last_weeks_stats(user, request)
     1129        assert isinstance(response, Response)
     1130        assert response.content_type == 'application/json'
     1131        # the body is a valid json-encoded stream
     1132        obj = json.loads(response.body)
     1133        assert len(obj) == 68
     1134        for week in obj:
     1135            assert len(week.keys()) == 8
     1136            assert 'id' in week.keys()
     1137            assert 'name' in week.keys()
     1138            assert 'week' in week.keys()
     1139            assert str(user.uid) in week['url']
     1140            assert 'profile' in week['url']
     1141            assert 'year' in week['url']
     1142            assert 'month' in week['url']
     1143            assert 'week' in week['url']
     1144            if week['id'] == workout_start_id:
     1145                assert week['distance'] == 30
     1146                assert week['elevation'] == 540
     1147                assert week['time'] == '01'
     1148                assert week['workouts'] == 1
     1149            else:
     1150                assert week['distance'] == 0
     1151                assert week['elevation'] == 0
     1152                assert week['time'] == '00'
     1153                assert week['workouts'] == 0
  • ow/utilities.py

    r68d00f5 r24596da  
    279279        if day.day in week:
    280280            return weeks.index(week)
    281     return None
    282281
    283282
  • ow/views/user.py

    r68d00f5 r24596da  
    302302    user_gender = _('Unknown')
    303303    for g in get_gender_names():
    304         if g[0] == context.gender:
     304        if g[0] == user.gender:
    305305            user_gender = localizer.translate(g[1])
    306306
Note: See TracChangeset for help on using the changeset viewer.