diff --git a/api/tests.py b/api/tests.py index 6b6566fe37728f25ebf9996fddcd1fd7d8db6cbb..483cf28300763493a739c594f28f991616e4f38a 100644 --- a/api/tests.py +++ b/api/tests.py @@ -482,8 +482,8 @@ class DataSourceApiIoTUKTest(TestCase): class DataSourceApiHyperCatTest(TestCase): test_name = 'HyperCat' plugin_name = 'HyperCat' - test_url = 'https://api.cityverve.org.uk/v1/cat/polling-station' - dataset = 'https://api.cityverve.org.uk/v1/entity/polling-station/5' + test_url = 'https://api.cityverve.org.uk/v1/cat' + dataset = 'https://api.cityverve.org.uk/v1/cat/polling-station' @classmethod def setUpTestData(cls): diff --git a/datasources/tests/test_connectors_hypercat.py b/datasources/tests/test_connectors_hypercat.py index 86dfae35ffbe7c194aeff1b93426587df8bae117..8c25b63c3c20f540a2cb1075f30c5d0d81113d7d 100644 --- a/datasources/tests/test_connectors_hypercat.py +++ b/datasources/tests/test_connectors_hypercat.py @@ -2,7 +2,6 @@ import itertools import typing from django.test import TestCase -from requests.auth import HTTPBasicAuth from datasources.connectors.base import AuthMethod, BaseDataConnector, HttpHeaderAuth @@ -27,15 +26,15 @@ def _count_items_by_key_value(collection: typing.Iterable[typing.Mapping], class ConnectorHyperCatTest(TestCase): - url = 'https://portal.bt-hypercat.com/cat' - - # Met Office dataset for weather at Heathrow - dataset = 'http://api.bt-hypercat.com/sensors/feeds/c7f361c6-7cb7-4ef5-aed9-397a0c0c4088' + # TODO find working dataset + url = 'https://api.cityverve.org.uk/v1/cat' + subcatalogue = 'https://api.cityverve.org.uk/v1/cat/polling-station' + dataset = 'https://api.cityverve.org.uk/v1/entity/polling-station/5' def _get_connection(self) -> BaseDataConnector: return self.plugin(self.url, api_key=self.api_key, - auth=HTTPBasicAuth) + auth=HttpHeaderAuth) def setUp(self): from decouple import config @@ -43,7 +42,8 @@ class ConnectorHyperCatTest(TestCase): BaseDataConnector.load_plugins('datasources/connectors') self.plugin = BaseDataConnector.get_plugin('HyperCat') - self.api_key = config('HYPERCAT_BT_API_KEY') + self.api_key = config('HYPERCAT_CISCO_API_KEY') + self.auth = None def test_get_plugin(self): self.assertIsNotNone(self.plugin) @@ -58,7 +58,14 @@ class ConnectorHyperCatTest(TestCase): self.assertTrue(connection.is_catalogue) - def test_plugin_get_metadata(self): + def test_determine_auth(self): + connection = self._get_connection() + + auth_method = connection.determine_auth_method(connection.location, connection.api_key) + + self.assertEqual(AuthMethod.HEADER, auth_method) + + def test_plugin_get_catalogue_metadata(self): connection = self._get_connection() result = connection.get_metadata() @@ -67,15 +74,19 @@ class ConnectorHyperCatTest(TestCase): for property in [ 'urn:X-hypercat:rels:hasDescription:en', 'urn:X-hypercat:rels:isContentType', + 'urn:X-hypercat:rels:hasHomepage', ]: self.assertIn(property, relations) - self.assertEqual('BT Hypercat DataHub Catalog', + self.assertEqual('CityVerve Public API - master catalogue', _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:hasDescription:en')['val']) self.assertEqual('application/vnd.hypercat.catalogue+json', _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:isContentType')['val']) + self.assertEqual('https://developer.cityverve.org.uk', + _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:hasHomepage')['val']) + def test_plugin_get_datasets(self): connection = self._get_connection() @@ -87,8 +98,16 @@ class ConnectorHyperCatTest(TestCase): self.assertLessEqual(1, len(datasets)) - self.assertIn(self.dataset, - datasets) + # Only check a couple of expected results are present - there's too many to list here + expected = { + 'https://api.cityverve.org.uk/v1/cat/accident', + 'https://api.cityverve.org.uk/v1/cat/advertising-board', + 'https://api.cityverve.org.uk/v1/cat/advertising-post', + # And because later tests rely on it... + 'https://api.cityverve.org.uk/v1/cat/polling-station', + } + for exp in expected: + self.assertIn(exp, datasets) def test_plugin_iter_datasets(self): connection = self._get_connection() @@ -127,7 +146,7 @@ class ConnectorHyperCatTest(TestCase): This process is relatively slow so we only do a couple of iterations. """ - with self.plugin(self.url, api_key=self.api_key, auth=HTTPBasicAuth) as connection: + with self._get_connection() as connection: for k, v in itertools.islice(connection.items(), 5): self.assertEqual(str, type(k)) @@ -138,132 +157,6 @@ class ConnectorHyperCatTest(TestCase): self.assertEqual(k, v.location) - def test_plugin_get_dataset_metadata(self): - connection = self._get_connection() - - result = connection[self.dataset].get_metadata() - - relations = [relation['rel'] for relation in result] - for property in [ - 'urn:X-bt:rels:feedTitle', - 'urn:X-hypercat:rels:hasDescription:en', - 'urn:X-bt:rels:feedTag', - 'urn:X-bt:rels:hasSensorStream', - 'urn:X-hypercat:rels:isContentType', - ]: - self.assertIn(property, relations) - - self.assertIn('Met Office', - _get_item_by_key_value(result, 'rel', 'urn:X-bt:rels:feedTitle')['val']) - - self.assertIn('Met Office', - _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:hasDescription:en')['val']) - - self.assertEqual(1, - _count_items_by_key_value(result, 'rel', 'urn:X-bt:rels:feedTag')) - - self.assertGreaterEqual(_count_items_by_key_value(result, 'rel', 'urn:X-bt:rels:hasSensorStream'), - 1) - - def test_plugin_get_dataset_data(self): - """ - Test that we can get data from a single dataset within the catalogue. - """ - connection = self._get_connection() - - dataset = connection[self.dataset] - result = dataset.get_data() - - self.assertIsInstance(result, str) - self.assertGreaterEqual(len(result), 1) - self.assertIn('c7f361c6-7cb7-4ef5-aed9-397a0c0c4088', - result) - - -class ConnectorHyperCatCiscoTest(TestCase): - # TODO find working dataset - url = 'https://api.cityverve.org.uk/v1/cat' - subcatalogue = 'https://api.cityverve.org.uk/v1/cat/polling-station' - dataset = 'https://api.cityverve.org.uk/v1/entity/polling-station/5' - - def _get_connection(self) -> BaseDataConnector: - return self.plugin(self.url, - api_key=self.api_key, - auth=HttpHeaderAuth) - - def setUp(self): - from decouple import config - - BaseDataConnector.load_plugins('datasources/connectors') - self.plugin = BaseDataConnector.get_plugin('HyperCat') - - self.api_key = config('HYPERCAT_CISCO_API_KEY') - self.auth = None - - def test_get_plugin(self): - self.assertIsNotNone(self.plugin) - - def test_plugin_init(self): - connection = self._get_connection() - - self.assertEqual(connection.location, self.url) - - def test_plugin_type(self): - connection = self._get_connection() - - self.assertTrue(connection.is_catalogue) - - def test_determine_auth(self): - connection = self._get_connection() - - auth_method = connection.determine_auth_method(connection.location, connection.api_key) - - self.assertEqual(AuthMethod.HEADER, auth_method) - - def test_plugin_get_catalogue_metadata(self): - connection = self._get_connection() - - result = connection.get_metadata() - - relations = [relation['rel'] for relation in result] - for property in [ - 'urn:X-hypercat:rels:hasDescription:en', - 'urn:X-hypercat:rels:isContentType', - 'urn:X-hypercat:rels:hasHomepage', - ]: - self.assertIn(property, relations) - - self.assertEqual('CityVerve Public API - master catalogue', - _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:hasDescription:en')['val']) - - self.assertEqual('application/vnd.hypercat.catalogue+json', - _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:isContentType')['val']) - - self.assertEqual('https://developer.cityverve.org.uk', - _get_item_by_key_value(result, 'rel', 'urn:X-hypercat:rels:hasHomepage')['val']) - - def test_plugin_get_datasets(self): - connection = self._get_connection() - - datasets = connection.get_datasets() - - self.assertEqual(list, - type(datasets)) - - self.assertLessEqual(1, - len(datasets)) - - # Only check a couple of expected results are present - there's too many to list here - expected = { - 'https://api.cityverve.org.uk/v1/cat/accident', - 'https://api.cityverve.org.uk/v1/cat/advertising-board', - 'https://api.cityverve.org.uk/v1/cat/advertising-post', - # And because later tests rely on it... - 'https://api.cityverve.org.uk/v1/cat/polling-station', - } - for exp in expected: - self.assertIn(exp, datasets) - def test_plugin_get_subcatalogue_metadata(self): connection = self._get_connection()