- Timestamp:
- Feb 27, 2019, 11:42:25 PM (5 years ago)
- Branches:
- current, feature/docs, master
- Children:
- a596867
- Parents:
- 24596da
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/tests/views/test_workout.py
r24596da rcef474f 1 1 import os 2 import json 2 3 from io import BytesIO 3 4 from datetime import datetime, timedelta, timezone 4 5 from cgi import FieldStorage 5 from unittest.mock import Mock, patch 6 from unittest.mock import Mock, patch, PropertyMock 6 7 7 8 import pytest … … 9 10 from pyramid.testing import DummyRequest 10 11 from pyramid.httpexceptions import HTTPFound, HTTPNotFound 12 from pyramid.response import Response 11 13 12 14 from webob.multidict import MultiDict … … 477 479 } 478 480 } 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.