Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion predicthq/endpoints/v1/events/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from predicthq.endpoints.base import BaseEndpoint
from predicthq.endpoints.decorators import accepts, returns
from .schemas import SearchParams, EventResultSet
from .schemas import SearchParams, EventResultSet, CalendarParams, CalendarResultSet


class EventsEndpoint(BaseEndpoint):
Expand All @@ -16,3 +16,8 @@ def search(self, **params):
@accepts(SearchParams)
def count(self, **params):
return self.client.get('/v1/events/count/', params=params).get('count')

@accepts(CalendarParams)
@returns(CalendarResultSet)
def calendar(self, **params):
return self.client.get('/v1/events/calendar/', params=params)
29 changes: 28 additions & 1 deletion predicthq/endpoints/v1/events/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from predicthq.endpoints.schemas import PaginatedMixin, SortableMixin, Model, ResultSet, \
ListType, StringType, GeoJSONPointType, StringListType, StringModelType, Area, \
ModelType, IntRange, IntType, DateRange, DateTimeType, FloatType, ResultType
ModelType, IntRange, IntType, DateRange, DateTimeType, FloatType, ResultType, \
DictType, DateType


class SearchParams(PaginatedMixin, SortableMixin, Model):
Expand Down Expand Up @@ -47,3 +48,29 @@ class Options:
class EventResultSet(ResultSet):

results = ResultType(Event)


class TopEventsSearchParams(SortableMixin, Model):

limit = IntType(min_value=0, max_value=10)


class CalendarParams(SearchParams):

top_events = ModelType(TopEventsSearchParams)


class CalendarDay(Model):

date = DateType()
count = IntType()
top_rank = FloatType()
rank_levels = DictType(IntType)
categories = DictType(IntType)
labels = DictType(IntType)
top_events = ModelType(EventResultSet)


class CalendarResultSet(ResultSet):

results = ResultType(CalendarDay)
11 changes: 10 additions & 1 deletion tests/endpoints/v1/events_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from tests import with_mock_client, with_mock_responses, with_client

from predicthq.endpoints.v1.events.schemas import EventResultSet
from predicthq.endpoints.v1.events.schemas import EventResultSet, CalendarResultSet


class EventsTest(unittest.TestCase):
Expand Down Expand Up @@ -49,3 +49,12 @@ def test_count(self, client, responses):
result = client.events.count(q="Foo Fighters", country="AU", limit=10)
self.assertIsInstance(result, int)
self.assertEqual(result, 12)

@with_client()
@with_mock_responses()
def test_calendar(self, client, responses):
result = client.events.calendar(start__gte="2015-12-24", start__lte="2015-12-26", country="NZ", top_events__limit=1, top_events__sort=["rank"])
self.assertIsInstance(result, CalendarResultSet)
self.assertEqual(result.count, 60)
self.assertEqual(3, len(list(result.iter_all())))
self.assertEqual(1, len(responses.calls))
180 changes: 180 additions & 0 deletions tests/fixtures/requests_responses/events_test/test_calendar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
[
{
"method": "GET",
"match_querystring": false,
"url": "/v1/events/calendar/",
"status": 200,
"content_type": "application/json",
"body": {
"count": 60,
"previous": null,
"next": null,
"results": [
{
"date": "2015-12-24",
"count": 40,
"labels": {
"conference": 1,
"holiday": 1,
"observance": 1,
"concert": 12,
"family": 1,
"festival": 20,
"movie": 1,
"performing-arts": 1,
"delay": 5,
"religion": 1,
"airport": 5,
"music": 12,
"education": 1
},
"top_rank": 50.0,
"rank_levels": {
"1": 15,
"3": 6,
"2": 19,
"5": 0,
"4": 0
},
"top_events": {
"count": 40,
"results": [
{
"category": "observances",
"end": "2015-12-24T00:00:00Z",
"description": "Christmas Eve is the day before Christmas Day and falls on December 24 in the Gregorian calendar.",
"title": "Christmas Eve",
"country": "NZ",
"labels": [
"holiday",
"observance"
],
"rank": 50,
"duration": 0,
"location": [
174.885971,
-40.900557
],
"relevance": null,
"timezone": "",
"start": "2015-12-24T00:00:00Z",
"id": "66e454beaf1aacdef8"
}
]
},
"categories": {
"conferences": 1,
"observances": 1,
"airport-delays": 5,
"festivals": 20,
"performing-arts": 1,
"concerts": 12
}
},
{
"date": "2015-12-25",
"count": 19,
"labels": {
"holiday": 1,
"outdoor": 1,
"concert": 5,
"festival": 9,
"delay": 1,
"attraction": 1,
"airport": 1,
"holiday-national": 1,
"music": 5,
"health": 1,
"sport": 4
},
"top_rank": 70.0,
"rank_levels": {
"1": 7,
"3": 1,
"2": 7,
"5": 0,
"4": 1
},
"top_events": {
"count": 19,
"results": [
{
"category": "public-holidays",
"end": "2015-12-25T00:00:00Z",
"description": "Christmas Day is a public holiday in New Zealand on December 25 each year.",
"title": "Christmas Day",
"country": "NZ",
"labels": [
"holiday",
"holiday-national"
],
"rank": 70,
"duration": 0,
"location": [
174.885971,
-40.900557
],
"relevance": null,
"timezone": "",
"start": "2015-12-25T00:00:00Z",
"id": "5469663176e532cfb8"
}
]
},
"categories": {
"public-holidays": 1,
"festivals": 9,
"airport-delays": 1,
"concerts": 5,
"sports": 3
}
},
{
"date": "2015-12-26",
"count": 1,
"labels": {
"holiday": 1,
"holiday-national": 1
},
"top_rank": 70.0,
"rank_levels": {
"1": 0,
"3": 0,
"2": 0,
"5": 0,
"4": 1
},
"top_events": {
"count": 1,
"results": [
{
"category": "public-holidays",
"end": "2015-12-26T00:00:00Z",
"description": "Boxing Day, annually observed on December 26, is a public holiday in New Zealand.",
"title": "Boxing Day",
"country": "NZ",
"labels": [
"holiday",
"holiday-national"
],
"rank": 70,
"duration": 0,
"location": [
174.885971,
-40.900557
],
"relevance": null,
"timezone": "Pacific/Auckland",
"start": "2015-12-26T00:00:00Z",
"id": "788ef6b62cc8b49dc2"
}
]
},
"categories": {
"public-holidays": 1
}
}
]
}
}
]