Changeset cef474f in OpenWorkouts-current


Ignore:
Timestamp:
Feb 27, 2019, 11:42:25 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
a596867
Parents:
24596da
Message:

Added more missing tests, raising coverage

File:
1 edited

Legend:

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

    r24596da rcef474f  
    11import os
     2import json
    23from io import BytesIO
    34from datetime import datetime, timedelta, timezone
    45from cgi import FieldStorage
    5 from unittest.mock import Mock, patch
     6from unittest.mock import Mock, patch, PropertyMock
    67
    78import pytest
     
    910from pyramid.testing import DummyRequest
    1011from pyramid.httpexceptions import HTTPFound, HTTPNotFound
     12from pyramid.response import Response
    1113
    1214from webob.multidict import MultiDict
     
    477479                    }
    478480                }
     481
     482    @patch('ow.views.workout.save_map_screenshot')
     483    def test_workout_map_shot_generate_map(self, save_map, dummy_request):
     484        """
     485        Call the view that returns the url to the screenshot of a workout
     486        tracking map, without a map being generated previously, the map is
     487        generated using save_map_screenshot
     488        """
     489        def static_url(url):
     490            return url
     491        request = dummy_request
     492        # mock static url to make testing this a bit easier
     493        request.static_url = static_url
     494        user = request.root['john']
     495        workout = user.workouts()[0]
     496        # we mock map_screenshot so the first access is None, triggering the
     497        # save_map_screenshot call. The second access returns a string we can
     498        # use with static_url for testing purposes
     499        type(workout).map_screenshot = PropertyMock(
     500            side_effect=[None, 'ow:static/maps/somemap.png'])
     501        response = workout_views.workout_map_shot(workout, request)
     502        save_map.assert_called_once_with(workout, request)
     503        assert isinstance(response, Response)
     504        assert response.content_type == 'application/json'
     505        # the body is a valid json-encoded stream
     506        obj = json.loads(response.body)
     507        assert 'ow:static/maps/somemap.png' in obj['url']
     508
     509    @patch('ow.views.workout.save_map_screenshot')
     510    def test_workout_map_shot_existing(self, save_map, dummy_request):
     511        """
     512        Call the view that returns the url to the screenshot of a workout
     513        tracking map, with an existing map already there
     514        """
     515        def static_url(url):
     516            return url
     517        request = dummy_request
     518        # mock static url to make testing this a bit easier
     519        request.static_url = static_url
     520        user = request.root['john']
     521        workout = user.workouts()[0]
     522        type(workout).map_screenshot = PropertyMock(
     523            side_effect=['ow:static/maps/somemap.png',
     524                         'ow:static/maps/somemap.png'])
     525        response = workout_views.workout_map_shot(workout, request)
     526        assert not save_map.called
     527        assert isinstance(response, Response)
     528        assert response.content_type == 'application/json'
     529        # the body is a valid json-encoded stream
     530        obj = json.loads(response.body)
     531        assert 'ow:static/maps/somemap.png' in obj['url']
Note: See TracChangeset for help on using the changeset viewer.