Skip to content

Commit

Permalink
Merge 2007fc9 into b244608
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonnefoy committed Jan 15, 2020
2 parents b244608 + 2007fc9 commit 056d3ce
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 6 deletions.
12 changes: 10 additions & 2 deletions predicthq/endpoints/v1/events/endpoint.py
@@ -1,6 +1,9 @@
from predicthq.endpoints.base import UserBaseEndpoint
from predicthq.endpoints.decorators import accepts, returns
from .schemas import SearchParams, EventResultSet, Count, CalendarParams, CalendarResultSet
from .schemas import (
SearchParams, EventResultSet, CountResultSet, CalendarParams, CalendarResultSet,
ImpactParams, ImpactResultSet
)


class EventsEndpoint(UserBaseEndpoint):
Expand All @@ -11,11 +14,16 @@ def search(self, **params):
return self.client.get(self.build_url('v1', 'events'), params=params)

@accepts(SearchParams)
@returns(Count)
@returns(CountResultSet)
def count(self, **params):
return self.client.get(self.build_url('v1', 'events/count'), params=params)

@accepts(CalendarParams)
@returns(CalendarResultSet)
def calendar(self, **params):
return self.client.get(self.build_url('v1', 'events/calendar'), params=params)

@accepts(ImpactParams)
@returns(ImpactResultSet)
def impact(self, **params):
return self.client.get(self.build_url('v1', 'events/impact'), params=params)
32 changes: 30 additions & 2 deletions predicthq/endpoints/v1/events/schemas.py
@@ -1,3 +1,5 @@
from schematics.common import NONEMPTY

from predicthq.endpoints.schemas import (
PaginatedMixin, SortableMixin, Model, ResultSet, ListType, StringType, GeoJSONPointType,
StringListType, StringModelType, Area, ModelType, IntRange, IntType, DateTimeRange,
Expand Down Expand Up @@ -95,7 +97,7 @@ class EventResultSet(ResultSet):
results = ResultType(Event)


class Count(Model):
class CountResultSet(Model):

count = IntType()
top_rank = FloatType()
Expand All @@ -112,7 +114,6 @@ class TopEventsSearchParams(SortableMixin, Model):
class CalendarParams(SearchParams):

top_events = ModelType(TopEventsSearchParams)
view = StringType(choices=('active', 'start'))


class CalendarDay(Model):
Expand All @@ -129,3 +130,30 @@ class CalendarDay(Model):
class CalendarResultSet(ResultSet):

results = ResultType(CalendarDay)


class ImpactParams(SearchParams):

top_events = ModelType(TopEventsSearchParams)
impact_rank = StringType(choices=('rank', 'aviation_rank'))


class ImpactDay(Model):

date = DateType()
count = IntType()
impact = IntType()

rank_levels = DictType(IntType, export_level=NONEMPTY)
rank_levels_impact = DictType(IntType, export_level=NONEMPTY)

aviation_rank_levels = DictType(IntType, export_level=NONEMPTY)
aviation_rank_levels_impact = DictType(IntType, export_level=NONEMPTY)

categories = DictType(IntType)
categories_impact = DictType(IntType)


class ImpactResultSet(ResultSet):

results = ResultType(ImpactDay)
15 changes: 13 additions & 2 deletions tests/endpoints/v1/test_events.py
@@ -1,6 +1,8 @@
import unittest

from predicthq.endpoints.v1.events.schemas import EventResultSet, CalendarResultSet, Count
from predicthq.endpoints.v1.events.schemas import (
EventResultSet, CalendarResultSet, CountResultSet, ImpactResultSet
)
from tests import with_mock_client, with_mock_responses, with_client


Expand Down Expand Up @@ -83,7 +85,7 @@ def test_search(self, client, responses):
@with_mock_responses()
def test_count(self, client, responses):
result = client.events.count(active__gte='2015-01-01', active__lte='2015-12-31', within='50km@-27.470784,153.030124')
assert isinstance(result, Count)
assert isinstance(result, CountResultSet)
assert result.count == 2501

@with_client()
Expand All @@ -94,3 +96,12 @@ def test_calendar(self, client, responses):
assert result.count == 60
assert len(list(result.iter_all())) == 3
assert len(responses.calls) == 1

@with_client()
@with_mock_responses()
def test_impact(self, client, responses):
result = client.events.impact(active__gte="2015-12-24", active__lte="2015-12-26", country="NZ", impact_rank="rank", active__tz="Pacific/Auckland")
assert isinstance(result, ImpactResultSet)
assert result.count == 76
assert len(list(result.iter_all())) == 3
assert len(responses.calls) == 1
119 changes: 119 additions & 0 deletions tests/fixtures/requests_responses/events_test/test_impact.json
@@ -0,0 +1,119 @@
[
{
"method": "GET",
"match_querystring": false,
"url": "/v1/events/impact/",
"status": 200,
"content_type": "application/json",
"body": {
"count": 76,
"next": null,
"previous": null,
"results": [
{
"date": "2015-12-24",
"count": 37,
"impact": 1430,
"rank_levels": {
"1": 20,
"2": 15,
"3": 1,
"4": 0,
"5": 1
},
"rank_levels_impact": {
"1": 400,
"2": 1030,
"3": 0,
"4": 0,
"5": 0
},
"categories": {
"concerts": 16,
"conferences": 1,
"festivals": 16,
"observances": 1,
"performing-arts": 2,
"school-holidays": 1
},
"categories_impact": {
"concerts": 730,
"conferences": 20,
"festivals": 600,
"observances": 0,
"performing-arts": 80,
"school-holidays": 0
}
},
{
"date": "2015-12-25",
"count": 17,
"impact": 2540,
"rank_levels": {
"1": 5,
"2": 9,
"3": 1,
"4": 0,
"5": 2
},
"rank_levels_impact": {
"1": 100,
"2": 540,
"3": 1900,
"4": 0,
"5": 0
},
"categories": {
"concerts": 2,
"conferences": 1,
"festivals": 12,
"public-holidays": 1,
"school-holidays": 1
},
"categories_impact": {
"concerts": 80,
"conferences": 60,
"festivals": 2400,
"public-holidays": 0,
"school-holidays": 0
}
},
{
"date": "2015-12-26",
"count": 24,
"impact": 8740,
"rank_levels": {
"1": 9,
"2": 11,
"3": 1,
"4": 1,
"5": 2
},
"rank_levels_impact": {
"1": 180,
"2": 660,
"3": 1900,
"4": 6000,
"5": 0
},
"categories": {
"concerts": 14,
"festivals": 6,
"performing-arts": 1,
"public-holidays": 1,
"school-holidays": 1,
"sports": 1
},
"categories_impact": {
"concerts": 2480,
"festivals": 200,
"performing-arts": 60,
"public-holidays": 0,
"school-holidays": 0,
"sports": 6000
}
}
]
}
}
]

0 comments on commit 056d3ce

Please sign in to comment.