Changeset ceae158 in OpenWorkouts-current


Ignore:
Timestamp:
Jan 21, 2019, 10:32:17 AM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
d1c4782
Parents:
b22a9d2
Message:

Added missing tests covering the map screenshots feature

Location:
ow/tests
Files:
1 added
3 edited

Legend:

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

    rb22a9d2 rceae158  
    1212from ow.models.root import OpenWorkouts
    1313from ow.utilities import create_blob
     14
     15from ow.tests.helpers import join
    1416
    1517
     
    553555        workout.tracking_filetype = None
    554556        assert workout.has_fit
     557
     558    @patch('ow.models.workout.os')
     559    @patch('ow.models.workout.save_map_screenshot')
     560    def test_map_screenshot_no_gpx(self, sms, os, root):
     561        workout = root['john']['1']
     562        assert workout.map_screenshot is None
     563        assert not os.path.abspath.called
     564        assert not os.path.dirname.called
     565        assert not os.path.join.called
     566        assert not os.path.exists.called
     567        assert not sms.called
     568
     569    @patch('ow.models.workout.os')
     570    @patch('ow.models.workout.save_map_screenshot')
     571    def test_map_screenshot_save(self, sms, os, root):
     572        """
     573        A workout with a tracking file has no map screenshot, one is
     574        saved to the filesystem.
     575        This test simply asserts the calls to the separate methods that
     576        look for existing screenshots and save a new one
     577        """
     578        os.path.abspath.return_value = 'current_dir'
     579        os.path.join.side_effect = join
     580        # This forces the "save screenshot" code to be run
     581        os.path.exists.return_value = False
     582
     583        workout = root['john']['1']
     584        workout.tracking_file = 'faked gpx file'
     585        workout.tracking_filetype = 'gpx'
     586
     587        uid = str(root['john'].uid)
     588        assert workout.map_screenshot == 'ow:/static/maps/' + uid + '/1.png'
     589        assert os.path.abspath.called
     590        assert os.path.dirname.called
     591        assert os.path.join.call_count == 2
     592        assert os.path.exists.called
     593        sms.assert_called_once_with(workout)
     594
     595    @patch('ow.models.workout.os')
     596    @patch('ow.models.workout.save_map_screenshot')
     597    def test_map_screenshot_do_not_save(self, sms, os, root):
     598        """
     599        A workout with a tracking file has a map screenshot, the path to that
     600        is returned without doing anything else
     601        """
     602        os.path.abspath.return_value = 'current_dir'
     603        os.path.join.side_effect = join
     604        # This forces the "save screenshot" code NOT to be run
     605        os.path.exists.return_value = True
     606
     607        workout = root['john']['1']
     608        workout.tracking_file = 'faked gpx file'
     609        workout.tracking_filetype = 'gpx'
     610
     611        uid = str(root['john'].uid)
     612        assert workout.map_screenshot == '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 == 2
     616        assert os.path.exists.called
     617        assert not sms.called
  • ow/tests/test_utilities.py

    rb22a9d2 rceae158  
    11import os
     2from datetime import timedelta
     3from unittest.mock import patch
    24from pyexpat import ExpatError
    35from xml.dom.minidom import Element
    46
    57import pytest
     8
     9from ow.models.root import OpenWorkouts
     10from ow.models.user import User
     11from ow.models.workout import Workout
    612
    713from ow.utilities import (
     
    1622    mps_to_kmph,
    1723    kmph_to_mps,
     24    save_map_screenshot
    1825)
    1926
     27from ow.tests.helpers import join
     28
    2029
    2130class TestUtilities(object):
     31
     32    @pytest.fixture
     33    def john(self):
     34        john = User(firstname='John', lastname='Doe',
     35                    email='john.doe@example.net')
     36        john.password = 's3cr3t'
     37        return john
     38
     39    @pytest.fixture
     40    def root(self, john):
     41        root = OpenWorkouts()
     42        root.add_user(john)
     43        john['1'] = Workout(
     44            duration=timedelta(minutes=60),
     45            distance=30
     46        )
     47        return root
    2248
    2349    def test_slugify(self):
     
    5480    def test_kmph_to_mps(self):
    5581        assert kmph_to_mps(30) == 30 * 0.277778
     82
     83    @patch('ow.utilities.os')
     84    @patch('ow.utilities.subprocess')
     85    def test_save_map_screenshot_no_gpx(self, subprocess, os, root, john):
     86        saved = save_map_screenshot(john['1'])
     87        assert not saved
     88        assert not os.path.abspath.called
     89        assert not os.path.dirname.called
     90        assert not os.path.join.called
     91        assert not os.path.exists.called
     92        assert not os.makedirs.called
     93        assert not subprocess.run.called
     94        # even having a fit tracking file, nothing is done
     95        john['1'].tracking_file = 'faked fit file'
     96        john['1'].tracking_filetype = 'fit'
     97        saved = save_map_screenshot(john['1'])
     98        assert not saved
     99        assert not os.path.abspath.called
     100        assert not os.path.dirname.called
     101        assert not os.path.join.called
     102        assert not os.path.exists.called
     103        assert not os.makedirs.called
     104        assert not subprocess.run.called
     105
     106    @patch('ow.utilities.os')
     107    @patch('ow.utilities.subprocess')
     108    def test_save_map_screenshot_with_gpx(self, subprocess, os, root, john):
     109        os.path.abspath.return_value = 'current_dir'
     110        os.path.join.side_effect = join
     111        # This mimics what happens when the directory for this user map
     112        # screenshots does not exist, which means we don'have to create one
     113        # (calling os.makedirs)
     114        os.path.exists.return_value = False
     115
     116        john['1'].tracking_file = 'faked gpx content'
     117        john['1'].tracking_filetype = 'gpx'
     118        saved = save_map_screenshot(john['1'])
     119        assert saved
     120        os.path.abspath.assert_called_once
     121        assert os.path.dirname.called
     122        assert os.path.join.call_count == 3
     123        assert os.path.exists.called
     124        assert os.makedirs.called
     125        subprocess.run.assert_called_once
     126
     127    @patch('ow.utilities.os')
     128    @patch('ow.utilities.subprocess')
     129    def test_save_map_screenshot_with_gpx_makedirs(
     130            self, subprocess, os, root, john):
     131        os.path.abspath.return_value = 'current_dir'
     132        os.path.join.side_effect = join
     133        # If os.path.exists returns True, makedirs is not called
     134        os.path.exists.return_value = True
     135
     136        john['1'].tracking_file = 'faked gpx content'
     137        john['1'].tracking_filetype = 'gpx'
     138        saved = save_map_screenshot(john['1'])
     139        assert saved
     140        os.path.abspath.assert_called_once
     141        assert os.path.dirname.called
     142        assert os.path.join.call_count == 3
     143        assert os.path.exists.called
     144        assert not os.makedirs.called
     145        subprocess.run.assert_called_once
    56146
    57147
  • ow/tests/views/test_workout.py

    rb22a9d2 rceae158  
    429429                assert response.content_type == 'application/xml'
    430430                assert expected_body in response.body
     431
     432    def test_workout_map_no_gpx(self, dummy_request):
     433        request = dummy_request
     434        user = request.root['john']
     435        workout = user.workouts()[0]
     436        response = workout_views.workout_map(workout, request)
     437        assert response == {'start_point': {}}
     438
     439    def test_workout_map(self, dummy_request):
     440        request = dummy_request
     441        user = request.root['john']
     442        workout = user.workouts()[0]
     443        # to ensure has_gpx returns true
     444        workout.tracking_filetype = 'gpx'
     445        gpx_file_path = os.path.join(
     446            os.path.dirname(os.path.dirname(__file__)),
     447            'fixtures/20131013.gpx')
     448        with patch.object(workout, 'tracking_file') as tf:
     449            with open(gpx_file_path, 'r') as gpx_file:
     450                tf.open.return_value = BytesIO(gpx_file.read().encode('utf-8'))
     451                response = workout_views.workout_map(workout, request)
     452                assert response == {
     453                    'start_point': {
     454                        'elevation': None,
     455                        'latitude': 37.108735040304566,
     456                        'longitude': 25.472489344630546
     457                    }
     458                }
Note: See TracChangeset for help on using the changeset viewer.