Skip to content

Commit

Permalink
Proper mocking of get requests in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaith committed Mar 10, 2015
1 parent 40f0ce5 commit f4326b9
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions talks/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,43 @@
from rest_framework.test import APIRequestFactory, APIClient
from talks.events import factories, models
from talks.events.models import EVENT_PUBLISHED, PersonEvent, ROLES_SPEAKER
from django.conf import settings

FUTURE_DATE_STRING = "2018-01-01 19:00"
PAST_DATE_STRING = "2011-01-01 20:00"

TOPIC_1429860_MOCK_RESPONSE = {"_links":{"self":{"href":"/search?uri=http://id.worldcat.org/fast/1429860"}},"_embedded":{"concepts":[{"uri":"http://id.worldcat.org/fast/1429860","prefLabel":"Biodiversity","altLabels":["Biotic diversity","Diversification, Biological","Diversity, Biotic","Biological diversity","Diversity, Biological","Biological diversification"],"related":[{"label":"Biology","uri":"http://id.worldcat.org/fast/832383"},{"label":"Ecological heterogeneity","uri":"http://id.worldcat.org/fast/901453"}]}]}}
LOC_40002001_MOCK_RESPONSE = {"_embedded": {"pois": [{"_embedded": {"files": [{"location": "oxpoints/40002001/depiction/original/primary.jpg","primary": True,"type": "depiction","url": "//mox-static-files.oucs.ox.ac.uk/oxpoints/40002001/depiction/original/primary.jpg"},{"location": "oxpoints/40002001/depiction/original/primary.jpg","type": "depiction","url": "//mox-static-files.oucs.ox.ac.uk/oxpoints/40002001/depiction/original/primary.jpg"}]},"_links": {"child": [{"href": "/places/oxpoints:23233603"},{"href": "/places/oxpoints:23233671","title": "11-13 Banbury Road","type": ["/university/building"],"type_name": ["Building"]},{"href": "/places/oxpoints:23233670","title": "7-9 Banbury Road","type": ["/university/building"],"type_name": ["Building"]},{"href": "/places/oxpoints:23233669","title": "15-19 Banbury Road","type": ["/university/building"],"type_name": ["Building"]}],"parent": {"href": "/places/oxpoints:31337175","title": "IT Services","type": ["/university/department"],"type_name": ["Department"]},"self": {"href": "/places/oxpoints:40002001"}},"address": "7-19 Banbury Road OX2 6NN","alternative_names": ["IT Services, Banbury Road"],"distance": 0,"id": "oxpoints:40002001","identifiers": ["osm:99933769-way","oxpoints:40002001"],"lat": "51.76001","lon": "-1.26035","name": "7-19 Banbury Road","name_sort": "7-19 Banbury Road","shape": "POLYGON ((-1.2604547 51.7597247,-1.2604524 51.759703600000002,-1.2606225 51.759693400000003,-1.2606263 51.759717899999998,-1.2606718 51.759715200000002,-1.2606742 51.759729900000004,-1.260875 51.759717299999998,-1.2609002 51.759870900000003,-1.2609514 51.7598677,-1.2609628 51.759937299999997,-1.2609819 51.759936099999997,-1.2610376 51.760275399999998,-1.2606854 51.760297899999998,-1.260475 51.760310799999999,-1.2604334 51.7600865,-1.2605216 51.760081800000002,-1.2605182 51.760061299999997,-1.2605157 51.760043799999998,-1.2604056 51.760051799999999,-1.2603867 51.759929399999997,-1.2604979 51.759923100000002,-1.2604921 51.7598805,-1.2604867 51.759852799999997,-1.2603628 51.759858199999996,-1.2603454 51.759729800000002,-1.2604547 51.7597247))","type": ["/university/site"],"type_name": ["Site"]}]},"_links": {"self": {"href": "/places/oxpoints:40002001%2C"}},"count": 1}
DEP_23232503_MOCK_RESPONSE = {"_embedded": {"pois": [{"_links": {"child": [{"href": "/places/oxpoints:23232548"}],"parent": {"href": "/places/oxpoints:23232546","title": "Department of Chemistry","type": ["/university/department"],"type_name": ["Department"]},"primary_place": {"href": "/places/oxpoints:23232548"},"self": {"href": "/places/oxpoints:23232503"}},"address": "off South Parks Road OX1 3TA","alternative_names": ["Bayley Group"],"distance": 0,"id": "oxpoints:23232503","identifiers": ["oxpoints:23232503","finance:DQ"],"lat": "51.757797","lon": "-1.253332","name": "Chemical Biology","name_sort": "Chemical Biology","shape": "POLYGON ((-1.252908 51.758493199999997 0,-1.2525447 51.758077800000002 0,-1.252753 51.758007999999997 0,-1.2527069 51.757955299999999 0,-1.2532898 51.757759999999998 0,-1.2533847 51.7578685 0,-1.2532655 51.757908399999998 0,-1.2532846 51.757930299999998 0,-1.2533138 51.757963699999998 0,-1.2533718 51.757944299999998 0,-1.253638 51.758248700000003 0,-1.252908 51.758493199999997 0))","social_twitter": ["https://www.twitter.com/bayley_lab"],"type": ["/university/department"],"type_name": ["Department"],"website": "http://bayley.chem.ox.ac.uk/"}]},"_links": {"self": {"href": "/places/oxpoints:23232503%2C"}},"count": 1}
DEP_23232503_SUBORGS_MOCK_RESPONSE = {"descendants": [],"id": "oxpoints:23232503"}


MOCK_URL_RESPONSES = {
settings.API_OX_PLACES_URL + 'oxpoints:40002001,': LOC_40002001_MOCK_RESPONSE,
settings.API_OX_PLACES_URL + 'oxpoints:23232503,': DEP_23232503_MOCK_RESPONSE,
settings.API_OX_PLACES_URL + 'oxpoints:23232503/organisation-descendants,': DEP_23232503_SUBORGS_MOCK_RESPONSE,
settings.TOPICS_URL + 'get?uri=http%3A%2F%2Fid.worldcat.org%2Ffast%2F1429860': TOPIC_1429860_MOCK_RESPONSE,
}

def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code

def json(self):
return self.json_data

def raise_for_status(self):
return

url = args[0]
response_content = MOCK_URL_RESPONSES.get(url, "Couldn't get mock response for url %s"%url)
return MockResponse(response_content, 200)


def mock_get(url):
return MOCK_URL_RESPONSES[url]


class TestAPI(TestCase):
Expand Down Expand Up @@ -86,9 +116,8 @@ def setUp(self):
self.req_factory = APIRequestFactory()
self.client = APIClient()

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_retrieve_event_happy(self, requests_get):
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/talks/' + self.event1_slug)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
Expand All @@ -99,9 +128,8 @@ def test_retrieve_event_404(self):
response = self.client.get('/api/talks/foo')
self.assertEquals(response.status_code, 404)

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_retrieve_series_happy(self, requests_get):
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/series/' + self.group1_slug)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "talks conference")
Expand All @@ -121,11 +149,10 @@ def test_search_no_results(self):
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_from_to(self, requests_get):
#test the from and to search fields
#expect only the future search
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/talks/search?from=01/01/15&to=01/01/20')
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
Expand All @@ -136,9 +163,8 @@ def test_search_from_to(self, requests_get):
# self.assertContains(response, "title", 2)
# No longer a valid test, as there are further titles within embedded data

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_speaker(self, requests_get):
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/talks/search?from=01/01/01&speaker=' + self.speaker1_slug )
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
Expand All @@ -147,27 +173,24 @@ def test_search_speaker(self, requests_get):
self.assertContains(response, "James Bond")
self.assertContains(response, "A future event")

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_venue(self, requests_get):
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/talks/search?from=01/01/01&venue=' + self.location1)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")
self.assertContains(response, "Banbury Road")

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_organising_department(self, requests_get):
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, DEP_23232503_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/talks/search?from=01/01/01&organising_department=' + self.department1)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")
self.assertContains(response, "Chemical Biology")

@mock.patch('requests.get', autospec=True)
@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_topic(self, requests_get):
requests_get.return_value.json.side_effect = [LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE, LOC_40002001_MOCK_RESPONSE, TOPIC_1429860_MOCK_RESPONSE]
response = self.client.get('/api/talks/search?from=01/01/01&topic=' + self.topic1_uri)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
Expand Down

0 comments on commit f4326b9

Please sign in to comment.