Changeset c6219ed in OpenWorkouts-current for ow/tests/test_utilities.py


Ignore:
Timestamp:
Jan 22, 2019, 12:21:51 AM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
26220ba, 2d2eb0d
Parents:
02048a6 (diff), be40b02 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merged patches from darcs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/test_utilities.py

    r02048a6 rc6219ed  
    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
Note: See TracChangeset for help on using the changeset viewer.