Skip to content

Commit

Permalink
Merge pull request #464 from ox-it/api-tests
Browse files Browse the repository at this point in the history
Api tests
  • Loading branch information
ahaith committed May 6, 2016
2 parents e5fdf53 + c40ec9b commit 2c32200
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
75 changes: 73 additions & 2 deletions talks/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
from django.test.testcases import TestCase
from rest_framework.test import APIRequestFactory, APIClient
from talks.events import factories, models
from talks.users import models
from talks.events.models import EVENT_PUBLISHED, PersonEvent, ROLES_SPEAKER
from django.conf import settings
import datetime

FUTURE_DATE_STRING = "2018-01-01 19:00"
FUTURE_STRING = datetime.datetime.strptime(FUTURE_DATE_STRING, "%Y-%m-%d %H:%M").strftime("%Y-%m-%d")

PAST_DATE_STRING = "2011-01-01 20:00"
PAST_STRING = datetime.datetime.strptime(PAST_DATE_STRING, "%Y-%m-%d %H:%M").strftime("%Y-%m-%d")

TODAY_DATE_STRING = datetime.date.today().strftime("%Y-%m-%d %I:%M")
TODAY_STRING = datetime.date.today().strftime("%Y-%m-%d")

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}
Expand Down Expand Up @@ -48,12 +56,14 @@ class TestAPI(TestCase):
def setUp(self):
self.event1_slug = "future-event"
self.group1_slug = "talks-conference"
self.collection1_slug = "it-collection"
self.speaker1_slug = "james-bond"
self.location1 = "oxpoints:40002001"
self.location_details1 = "First floor"
self.department1 = "oxpoints:23232503" # Chemical Biology
self.super_department = "oxpoints:23232546" # Department of Chemistry
self.topic1_uri = "http://id.worldcat.org/fast/1429860"
# create some sample events and series
# create some sample events, series and collections
person1 = factories.PersonFactory.create(
name="James Bond",
bio="Secret Agent",
Expand All @@ -75,6 +85,17 @@ def setUp(self):
description="a seminar",
group_type='Seminar Series'
)
collection1 = factories.EventCollectionFactory.create(
title="IT talks collection",
slug=self.collection1_slug,
description="a collection of IT talks",
public=True,
)
collection2 = factories.EventCollectionFactory.create(
title="My second collection",
slug="my-second-collection",
description="second collection",
)
future_event = factories.EventFactory.create(
title="A future event",
slug=self.event1_slug,
Expand All @@ -83,6 +104,7 @@ def setUp(self):
end=FUTURE_DATE_STRING,
status=EVENT_PUBLISHED,
location=self.location1,
location_details=self.location_details1,
department_organiser=self.department1,
group=group1,
)
Expand All @@ -95,6 +117,17 @@ def setUp(self):
status=EVENT_PUBLISHED,
group=group1,
)

today_event = factories.EventFactory.create(
title="A today event",
slug="today-event",
description="a today event event",
start=TODAY_DATE_STRING,
end=TODAY_DATE_STRING,
status=EVENT_PUBLISHED,
group=group1,
)

factories.PersonEventFactory.create(
person=person1,
event=future_event,
Expand Down Expand Up @@ -123,6 +156,8 @@ def test_retrieve_event_happy(self, requests_get):
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")
self.assertContains(response, "A future event")
self.assertContains(response, "James Bond")
self.assertContains(response, "First floor")

def test_retrieve_event_404(self):
response = self.client.get('/api/talks/foo')
Expand All @@ -141,6 +176,18 @@ def test_retrieve_series_invalid(self):
response = self.client.get('/api/series/foo/')
self.assertEquals(response.status_code, 404)

@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_retrieve_collection_happy(self, requests_get):
response = self.client.get('/api/collections/id/' + self.collection1_slug)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "IT talks collection")
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")

def test_retrieve_collection_invalid(self):
response = self.client.get('/api/collections/id/foo')
self.assertEquals(response.status_code, 404)

def test_search_no_results(self):
# ensure _links section still exists
# ensure _embedded section still exists with empty talks field
Expand All @@ -149,6 +196,30 @@ def test_search_no_results(self):
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")

@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_from_today(self, requests_get):
#test the from=today search
#expect only the future search
response = self.client.get('/api/talks/search?from='+ TODAY_STRING)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")
self.assertContains(response, "A future event")
self.assertContains(response, "A today event")
self.assertNotContains(response, "A past event")

@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_edge_dates(self, requests_get):
#test the from past_event_date, to future_event_date search
#expect all the events in the results
response = self.client.get('/api/talks/search?from='+ PAST_STRING +'&to=' + FUTURE_STRING)
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")
self.assertContains(response, "A future event")
self.assertContains(response, "A today event")
self.assertContains(response, "A past event")

@mock.patch('requests.get', side_effect=mocked_requests_get)
def test_search_from_to(self, requests_get):
#test the from and to search fields
Expand Down Expand Up @@ -203,4 +274,4 @@ def test_search_sub_organisations(self, requests_get):
self.assertEquals(response.status_code, 200)
self.assertContains(response, "_links")
self.assertContains(response, "_embedded")
self.assertContains(response, self.event1_slug)
self.assertContains(response, self.event1_slug)
7 changes: 6 additions & 1 deletion talks/events/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import factory

from . import models
from talks.users.models import Collection


class EventGroupFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.EventGroup

class EventCollectionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Collection


class EventFactory(factory.django.DjangoModelFactory):
start = datetime(2015, 10, 23, 12, 18)
Expand Down Expand Up @@ -41,4 +46,4 @@ class Meta:

class TopicItemFactory_noSubFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.TopicItem
model = models.TopicItem

0 comments on commit 2c32200

Please sign in to comment.