-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7f0577
commit 4a28e97
Showing
5 changed files
with
102 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 17 additions & 27 deletions
44
server/ntb/tests/io/feeding_services/event_http_service_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 54 additions & 94 deletions
148
server/ntb/tests/io/feeding_services/ntb_event_api_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,91 @@ | ||
import os | ||
from unittest import mock | ||
import ntb | ||
import responses | ||
|
||
from responses import matchers | ||
from superdesk.tests import TestCase | ||
from apps.prepopulate.app_populate import AppPopulateCommand | ||
import ntb | ||
from superdesk.io.feeding_services import http_base_service | ||
from ntb.io.feeding_services import ntb_event_api | ||
from planning.events import init_app as init_events_app | ||
from apps.prepopulate.app_populate import AppPopulateCommand | ||
|
||
|
||
PROVIDER = { | ||
'_id': 'test_ntb_events_api_xml', | ||
'feed_parser': 'ntb_events_api_xml', | ||
'config': { | ||
'url': 'https://nyheter.ntb.no/ntbWeb/api/x1/search/full?' | ||
'search.service=newscalendar&' | ||
'search.calendarStart=2018-10-01&' | ||
'search.calendarStop=2018-10-31', | ||
'username': 'fake', | ||
'password': 'fake' | ||
"_id": "test_ntb_events_api_xml", | ||
"feed_parser": "ntb_events_api_xml", | ||
"config": { | ||
"url": "https://nyheter.ntb.no/ntbWeb/api/x1/search/full?" | ||
"search.service=newscalendar&" | ||
"search.calendarStart=2018-10-01&" | ||
"search.calendarStop=2018-10-31", | ||
"username": "fake", | ||
"password": "fake", | ||
}, | ||
} | ||
|
||
|
||
class NTBEventsApiFeedingServiceTestCase(TestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
with self.app.app_context(): | ||
# prepopulate vocabularies | ||
voc_file = os.path.join( | ||
os.path.abspath(os.path.dirname(os.path.dirname(ntb.__file__))), 'data', 'vocabularies.json' | ||
os.path.abspath(os.path.dirname(os.path.dirname(ntb.__file__))), | ||
"data", | ||
"vocabularies.json", | ||
) | ||
AppPopulateCommand().run(voc_file) | ||
# by default events resource is not available | ||
init_events_app(self.app) | ||
|
||
# NTBEventsApiFeedingService does 4 request during 1 update, | ||
# to mock returning of different results (requests.get) self._side_effect is used | ||
self.feeds = [] | ||
for i in range(4): | ||
dirname = os.path.dirname(os.path.realpath(__file__)) | ||
fixture = os.path.normpath( | ||
os.path.join(dirname, '../fixtures', 'ntb_events_api', '{}.xml'.format(i)) | ||
os.path.join( | ||
dirname, "../fixtures", "ntb_events_api", "{}.xml".format(i) | ||
) | ||
) | ||
|
||
with open(fixture, 'rb') as f: | ||
with open(fixture, "rb") as f: | ||
self.feeds.append(f.read()) | ||
|
||
def _side_effect(self, *args, **kwargs): | ||
m = mock.MagicMock() | ||
m.ok = True | ||
|
||
if kwargs['params']['search.offset'] == 0: | ||
m.content = self.feeds[0] | ||
elif kwargs['params']['search.offset'] == 2: | ||
m.content = self.feeds[1] | ||
elif kwargs['params']['search.offset'] == 4: | ||
m.content = self.feeds[2] | ||
elif kwargs['params']['search.offset'] == 6: | ||
m.content = self.feeds[3] | ||
|
||
return m | ||
|
||
@mock.patch.object(http_base_service.requests, 'get') | ||
def test_items_count(self, get): | ||
get.side_effect = self._side_effect | ||
feeding_service = ntb_event_api.NTBEventsApiFeedingService() | ||
feeding_service.provider = PROVIDER | ||
items = feeding_service._update(PROVIDER, {})[0] | ||
|
||
self.assertEqual(len(items), 8) | ||
|
||
@mock.patch.object(http_base_service.requests, 'get') | ||
def test_items_count_ignore_duplicates(self, get): | ||
feeding_service = ntb_event_api.NTBEventsApiFeedingService() | ||
|
||
get.return_value.ok = True | ||
get.return_value.content = self.feeds[0] | ||
feeding_service.provider = PROVIDER | ||
items = feeding_service._update(PROVIDER, {})[0] | ||
self.assertEqual(len(items), 2) | ||
|
||
@mock.patch.object(http_base_service.requests, 'get') | ||
def test_requests_offset(self, get): | ||
get.side_effect = self._side_effect | ||
feeding_service = ntb_event_api.NTBEventsApiFeedingService() | ||
feeding_service.provider = PROVIDER | ||
feeding_service._update(PROVIDER, {}) | ||
|
||
self.assertEqual( | ||
get.mock_calls[0], | ||
mock.call( | ||
PROVIDER['config']['url'], | ||
auth=('fake', 'fake'), | ||
params={'search.offset': 0, 'search.showNumResults': 25}, | ||
timeout=20 | ||
) | ||
@responses.activate(assert_all_requests_are_fired=True) | ||
def test_requests_offset(self): | ||
responses.get( | ||
"https://nyheter.ntb.no/ntbWeb/api/x1/search/full", | ||
match=( | ||
matchers.query_param_matcher({"search.offset": 0}, strict_match=False), | ||
), | ||
body=self.feeds[0], | ||
status=200, | ||
) | ||
|
||
self.assertEqual( | ||
get.mock_calls[1], | ||
mock.call( | ||
PROVIDER['config']['url'], | ||
auth=('fake', 'fake'), | ||
params={'search.offset': 2, 'search.showNumResults': 25}, | ||
timeout=20 | ||
) | ||
responses.get( | ||
"https://nyheter.ntb.no/ntbWeb/api/x1/search/full", | ||
match=( | ||
matchers.query_param_matcher({"search.offset": 2}, strict_match=False), | ||
), | ||
body=self.feeds[1], | ||
status=200, | ||
) | ||
|
||
self.assertEqual( | ||
get.mock_calls[2], | ||
mock.call( | ||
PROVIDER['config']['url'], | ||
auth=('fake', 'fake'), | ||
params={'search.offset': 4, 'search.showNumResults': 25}, | ||
timeout=20 | ||
) | ||
responses.get( | ||
"https://nyheter.ntb.no/ntbWeb/api/x1/search/full", | ||
match=( | ||
matchers.query_param_matcher({"search.offset": 4}, strict_match=False), | ||
), | ||
body=self.feeds[2], | ||
status=200, | ||
) | ||
|
||
self.assertEqual( | ||
get.mock_calls[3], | ||
mock.call( | ||
PROVIDER['config']['url'], | ||
auth=('fake', 'fake'), | ||
params={'search.offset': 6, 'search.showNumResults': 25}, | ||
timeout=20 | ||
) | ||
responses.get( | ||
"https://nyheter.ntb.no/ntbWeb/api/x1/search/full", | ||
match=( | ||
matchers.query_param_matcher({"search.offset": 6}, strict_match=False), | ||
), | ||
body=self.feeds[3], | ||
status=200, | ||
) | ||
feeding_service = ntb_event_api.NTBEventsApiFeedingService() | ||
feeding_service.provider = PROVIDER | ||
items = feeding_service._update(PROVIDER, {})[0] | ||
assert 8 == len(items) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters