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


Ignore:
Timestamp:
Feb 11, 2019, 6:55:55 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
02aee97
Parents:
93bbb89
Message:

(#52) - screenshots of the workouts maps were corrupted randomly.

Replaced our screenshot_map shell script that was calling chrome headless
directly with some python code running splinter + selenium webdriver.

Using chromedriver in such environment we can first visit the map site
for the given workout, then wait a bit for it to completely load, then
take the screenshot.

I've also removed all traces of screenshot_map from the code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/test_utilities.py

    r93bbb89 rd6f8304  
    11import os
    22from datetime import timedelta, datetime
    3 from unittest.mock import patch
     3from unittest.mock import patch, Mock
    44from pyexpat import ExpatError
    55from xml.dom.minidom import Element
    66
    77import pytest
     8from pyramid.testing import DummyRequest
    89
    910from ow.models.root import OpenWorkouts
     
    8384        assert kmph_to_mps(30) == 30 * 0.277778
    8485
     86    @patch('ow.utilities.shutil')
    8587    @patch('ow.utilities.os')
    86     @patch('ow.utilities.subprocess')
    87     def test_save_map_screenshot_no_gpx(self, subprocess, os, root, john):
    88         saved = save_map_screenshot(john['1'])
     88    @patch('ow.utilities.Browser')
     89    def test_save_map_screenshot_no_gpx(
     90            self, Browser, os, shutil, root, john):
     91        request = DummyRequest()
     92        saved = save_map_screenshot(john['1'], request)
    8993        assert not saved
     94        assert not Browser.called
    9095        assert not os.path.abspath.called
    9196        assert not os.path.dirname.called
     
    9398        assert not os.path.exists.called
    9499        assert not os.makedirs.called
    95         assert not subprocess.run.called
     100        assert not shutil.move.called
    96101        # even having a fit tracking file, nothing is done
    97102        john['1'].tracking_file = 'faked fit file'
    98103        john['1'].tracking_filetype = 'fit'
    99         saved = save_map_screenshot(john['1'])
     104        saved = save_map_screenshot(john['1'], request)
    100105        assert not saved
     106        assert not Browser.called
    101107        assert not os.path.abspath.called
    102108        assert not os.path.dirname.called
     
    104110        assert not os.path.exists.called
    105111        assert not os.makedirs.called
    106         assert not subprocess.run.called
    107 
     112        assert not shutil.move.called
     113
     114    @patch('ow.utilities.shutil')
    108115    @patch('ow.utilities.os')
    109     @patch('ow.utilities.subprocess')
    110     def test_save_map_screenshot_with_gpx(self, subprocess, os, root, john):
     116    @patch('ow.utilities.Browser')
     117    def test_save_map_screenshot_with_gpx(
     118            self, Browser, os, shutil, root, john):
     119        request = DummyRequest()
     120        browser = Mock()
     121        Browser.return_value = browser
    111122        os.path.abspath.return_value = 'current_dir'
    112123        os.path.join.side_effect = join
     
    116127        os.path.exists.return_value = False
    117128
     129        map_url = request.resource_url(john['1'], 'map')
     130
    118131        john['1'].tracking_file = 'faked gpx content'
    119132        john['1'].tracking_filetype = 'gpx'
    120         saved = save_map_screenshot(john['1'])
     133        saved = save_map_screenshot(john['1'], request)
    121134        assert saved
     135        Browser.assert_called_once_with('chrome', headless=True)
     136        browser.driver.set_window_size.assert_called_once_with(1300, 436)
     137        browser.visit.assert_called_once_with(map_url)
     138        browser.screenshot.assert_called_once
    122139        os.path.abspath.assert_called_once
    123140        assert os.path.dirname.called
    124         assert os.path.join.call_count == 3
     141        assert os.path.join.call_count == 2
    125142        assert os.path.exists.called
    126143        assert os.makedirs.called
    127         subprocess.run.assert_called_once
    128 
     144        os.shutil.move.assert_called_once
     145
     146    @patch('ow.utilities.shutil')
    129147    @patch('ow.utilities.os')
    130     @patch('ow.utilities.subprocess')
     148    @patch('ow.utilities.Browser')
    131149    def test_save_map_screenshot_with_gpx_makedirs(
    132             self, subprocess, os, root, john):
     150            self, Browser, os, shutil, root, john):
     151        request = DummyRequest()
     152        browser = Mock()
     153        Browser.return_value = browser
    133154        os.path.abspath.return_value = 'current_dir'
    134155        os.path.join.side_effect = join
     
    136157        os.path.exists.return_value = True
    137158
     159        map_url = request.resource_url(john['1'], 'map')
     160
    138161        john['1'].tracking_file = 'faked gpx content'
    139162        john['1'].tracking_filetype = 'gpx'
    140         saved = save_map_screenshot(john['1'])
     163        saved = save_map_screenshot(john['1'], request)
    141164        assert saved
     165        Browser.assert_called_once_with('chrome', headless=True)
     166        browser.driver.set_window_size.assert_called_once_with(1300, 436)
     167        browser.visit.assert_called_once_with(map_url)
     168        browser.screenshot.assert_called_once
    142169        os.path.abspath.assert_called_once
    143170        assert os.path.dirname.called
    144         assert os.path.join.call_count == 3
     171        assert os.path.join.call_count == 2
    145172        assert os.path.exists.called
    146173        assert not os.makedirs.called
    147         subprocess.run.assert_called_once
     174        os.shutil.move.assert_called_once
    148175
    149176    def test_timedelta_to_hms(self):
Note: See TracChangeset for help on using the changeset viewer.