Changeset 0dedfbe in OpenWorkouts-current for ow


Ignore:
Timestamp:
Apr 22, 2019, 10:47:55 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current
Children:
a6fa857
Parents:
42baca4
Message:

(#39) Duplicated workouts, fixed broken tests, added more tests coverage

Location:
ow/tests
Files:
3 edited

Legend:

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

    r42baca4 r0dedfbe  
    22from unittest.mock import Mock
    33from datetime import datetime, timedelta, timezone
     4from decimal import Decimal
    45
    56import pytest
     
    175176        john.add_workout(workout)
    176177        assert root.sports_json == json.dumps(["cycling", "running"])
     178
     179    def test_get_workout_by_hash(self, root, john):
     180        # non existant hash
     181        found = root.get_workout_by_hash('non-existant-hash')
     182        assert found is None
     183        # existing workout
     184        workout = root[str(john.uid)]['1']
     185        found = root.get_workout_by_hash(workout.hashed)
     186        assert found == workout
     187        # a workout that was not added to the system
     188        workout = Workout(
     189            start_time=datetime.now(timezone.utc),
     190            duration=timedelta(seconds=3600),
     191            distance=Decimal(30)
     192        )
     193        found = root.get_workout_by_hash(workout.hashed)
     194        assert found is None
  • ow/tests/models/test_workout.py

    r42baca4 r0dedfbe  
    143143        workout.distance = 44.44444444
    144144        assert workout.rounded_distance == 44.44
     145
     146    def test_hashed(self, root):
     147        # first test a workout that is attached to a user
     148        workout = root['john']['1']
     149        assert workout.hashed == (
     150            str(workout.owner.uid) +
     151            workout.start.strftime('%Y%m%d%H%M%S') +
     152            str(workout.duration.seconds) +
     153            str(workout.distance)
     154        )
     155        # now a workout that is not (no owner info)
     156        workout = Workout(
     157            start_time=datetime.now(timezone.utc),
     158            duration=timedelta(seconds=3600),
     159            distance=Decimal(30)
     160        )
     161        assert workout.hashed == (
     162            workout.start.strftime('%Y%m%d%H%M%S') +
     163            str(workout.duration.seconds) +
     164            str(workout.distance)
     165        )
     166        # now an empty workout...
     167        workout = Workout()
     168        with pytest.raises(AttributeError):
     169            assert workout.hashed == (
     170                workout.start.strftime('%Y%m%d%H%M%S') +
     171                str(workout.duration.seconds) +
     172                str(workout.distance)
     173            )
    145174
    146175    def test_trimmed_notes(self):
  • ow/tests/views/test_workout.py

    r42baca4 r0dedfbe  
    182182        assert len(response['form'].form.errors) == 0
    183183        assert isinstance(response['form'].form.schema, UploadedWorkoutSchema)
     184        assert response['duplicate'] is None
    184185
    185186    def test_add_workout_post_invalid(self, dummy_request):
     
    196197        # Only one required field in this case, the tracking file
    197198        assert len(response['form'].form.errors) == 1
     199        assert response['duplicate'] is None
    198200
    199201    def test_add_workout_post_invalid_bytes(self, dummy_request):
     
    214216        # Only one required field in this case, the tracking file
    215217        assert len(response['form'].form.errors) == 1
     218        assert response['duplicate'] is None
    216219
    217220    @pytest.mark.parametrize('filename', gpx_filenames)
     
    234237        assert response.location.endswith('/2/')
    235238        assert len(request.root['john'].workouts()) == 2
     239        self.close_uploaded_file(uploaded_file)
     240
     241    def test_add_workout_post_duplicate(self, dummy_request):
     242        """
     243        POST request, first add a workout uploading one of the sample
     244        tracking files, then try to upload it again.
     245        """
     246        # first, upload the workout
     247        filename = self.gpx_filenames[0]
     248        request = dummy_request
     249        uploaded_file = self.open_uploaded_file(filename)
     250        filestorage = self.create_filestorage(uploaded_file)
     251        user = request.root['john']
     252        request.method = 'POST'
     253        request.POST = MultiDict({
     254            'tracking_file': filestorage,
     255            'submit': True,
     256            })
     257        assert len(request.root['john'].workouts()) == 1
     258        response = workout_views.add_workout(user, request)
     259        assert isinstance(response, HTTPFound)
     260        assert response.location.endswith('/2/')
     261        assert len(request.root['john'].workouts()) == 2
     262        self.close_uploaded_file(uploaded_file)
     263        # now, try to upload it again
     264        uploaded_file = self.open_uploaded_file(filename)
     265        filestorage = self.create_filestorage(uploaded_file)
     266        user = request.root['john']
     267        request.method = 'POST'
     268        request.POST = MultiDict({
     269            'tracking_file': filestorage,
     270            'submit': True,
     271            })
     272        response = workout_views.add_workout(user, request)
     273        assert response['duplicate'] == request.root['john']['2']
     274        assert len(request.root['john'].workouts()) == 2
     275        self.close_uploaded_file(uploaded_file)
     276        # finally, override the duplicate prevention code and save it
     277        uploaded_file = self.open_uploaded_file(filename)
     278        filestorage = self.create_filestorage(uploaded_file)
     279        user = request.root['john']
     280        request.method = 'POST'
     281        request.POST = MultiDict({
     282            'tracking_file': filestorage,
     283            'allow_duplicates': 'on',
     284            'submit': True,
     285            })
     286        response = workout_views.add_workout(user, request)
     287        assert isinstance(response, HTTPFound)
     288        assert response.location.endswith('/3/')
     289        assert len(request.root['john'].workouts()) == 3
    236290        self.close_uploaded_file(uploaded_file)
    237291
Note: See TracChangeset for help on using the changeset viewer.