Changeset b3374f6 in OpenWorkouts-current for ow/tests/models/test_workout.py


Ignore:
Timestamp:
Feb 12, 2019, 5:55:33 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
c999b73e
Parents:
f713dbc
Message:

(#52) - Make map screenshot generation async and non-blocker of the dashboard
and user profile pages:

  • workout.map_screenshot is a property again, returns a string with the static path (suitable for use with request.static_url()) for the map screenshot file if exists, none otherwise
  • added a couple of helpers to build the proper screenshot name and full path on the filesystem
  • added a view to generate the map if needed, returning the full static url to the screenshot file in a json-encoded stream
  • added a new js ow tool that looks for workouts that still don't have a map screenshot, calling the view to generate that screenshot and updating the map screenshot when the file is ready
  • modified the dashboard and user profile templates, so a dummy/placeholder image is shown when no map screenshot is yet ready, plus setup and calls for the js tool that generates the maps.
File:
1 edited

Legend:

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

    rf713dbc rb3374f6  
    77import pytest
    88from pyramid.security import Allow, Everyone, Deny, ALL_PERMISSIONS
    9 from pyramid.testing import DummyRequest
    109
    1110from ow.models.workout import Workout
     
    573572        assert workout.has_fit
    574573
     574    def test_map_screenshot_name(self, root):
     575        workout = root['john']['1']
     576        assert workout.map_screenshot_name == (
     577            str(root['john'].uid) + '/' + str(workout.workout_id) + '.png')
     578
     579    def test_map_screenshot_path(self, root):
     580        workout = root['john']['1']
     581        assert workout.map_screenshot_path.endswith(
     582            'static/maps/' + workout.map_screenshot_name)
     583
    575584    @patch('ow.models.workout.os')
    576     @patch('ow.models.workout.save_map_screenshot')
    577     def test_map_screenshot_no_gpx(self, sms, os, root):
    578         workout = root['john']['1']
    579         request = DummyRequest()
    580         assert workout.map_screenshot(request) is None
    581         assert not os.path.abspath.called
    582         assert not os.path.dirname.called
     585    def test_map_screenshot_no_gpx(self, os, root):
     586        workout = root['john']['1']
     587        assert workout.map_screenshot is None
    583588        assert not os.path.join.called
    584589        assert not os.path.exists.called
    585         assert not sms.called
    586590
    587591    @patch('ow.models.workout.os')
    588     @patch('ow.models.workout.save_map_screenshot')
    589     def test_map_screenshot_save(self, sms, os, root):
    590         """
    591         A workout with a tracking file has no map screenshot, one is
    592         saved to the filesystem.
    593         This test simply asserts the calls to the separate methods that
    594         look for existing screenshots and save a new one
    595         """
    596         os.path.abspath.return_value = 'current_dir'
    597         os.path.join.side_effect = join
    598         # This forces the "save screenshot" code to be run
     592    def test_map_screenshot_no_shot(self, os, root):
     593        """
     594        A workout with a tracking file has no map screenshot
     595        """
     596        # This says "no screenshot found"
    599597        os.path.exists.return_value = False
    600598
     
    603601        workout.tracking_filetype = 'gpx'
    604602
    605         uid = str(root['john'].uid)
    606         request = DummyRequest()
    607         # dummyrequest can't resolve static assets without adding a lot
    608         # of boilerplate, no need for that here
    609         request.static_url = Mock()
    610         request.static_url.return_value = 'ow:/static/maps/' + uid + '/1.png'
    611         res = workout.map_screenshot(request)
    612         assert res == 'ow:/static/maps/' + uid + '/1.png'
    613         assert os.path.abspath.called
    614         assert os.path.dirname.called
    615         assert os.path.join.call_count == 3
    616         assert os.path.exists.called
    617         sms.assert_called_once_with(workout, request)
     603        assert workout.map_screenshot is None
     604        assert os.path.join.called
     605        os.path.exists.assert_called_once_with(workout.map_screenshot_path)
    618606
    619607    @patch('ow.models.workout.os')
    620     @patch('ow.models.workout.save_map_screenshot')
    621     def test_map_screenshot_do_not_save(self, sms, os, root):
     608    def test_map_screenshot_has_shot(self, os, root):
    622609        """
    623610        A workout with a tracking file has a map screenshot, the path to that
    624611        is returned without doing anything else
    625612        """
    626         os.path.abspath.return_value = 'current_dir'
     613        # This says "yeah, we have a screenshot"
     614        os.path.exists.return_value = True
     615        os.path.abspath.return_value = '/'
    627616        os.path.join.side_effect = join
    628         # This forces the "save screenshot" code NOT to be run
    629         os.path.eisxts.return_value = True
    630617
    631618        workout = root['john']['1']
    632619        workout.tracking_file = 'faked gpx file'
    633620        workout.tracking_filetype = 'gpx'
    634 
    635621        uid = str(root['john'].uid)
    636         request = DummyRequest()
    637         # dummyrequest can't resolve static assets without adding a lot
    638         # of boilerplate, no need for that here
    639         request.static_url = Mock()
    640         request.static_url.return_value = 'ow:/static/maps/' + uid + '/1.png'
    641         res = workout.map_screenshot(request)
    642         assert res == 'ow:/static/maps/' + uid + '/1.png'
    643         assert os.path.abspath.called
    644         assert os.path.dirname.called
    645         assert os.path.join.call_count == 3
    646         assert os.path.exists.called
    647         assert not sms.called
     622        assert workout.map_screenshot == 'ow:/static/maps/' + uid + '/1.png'
     623        os.path.exists.assert_called_once_with(workout.map_screenshot_path)
     624        assert os.path.join.called
Note: See TracChangeset for help on using the changeset viewer.