diff --git a/mtd/parsers/request_parser.py b/mtd/parsers/request_parser.py index f2d61fb..ed54972 100644 --- a/mtd/parsers/request_parser.py +++ b/mtd/parsers/request_parser.py @@ -2,7 +2,7 @@ import requests from jsonschema.exceptions import ValidationError from mtd.parsers import json_parser, xml_parser -from mtd.exceptions import RequestException, UnsupportedFiletypeError +from mtd.exceptions import RequestException, MissingResourceError, UnsupportedFiletypeError from mtd.parsers.utils import ResourceManifest from json.decoder import JSONDecodeError from lxml import etree @@ -19,7 +19,10 @@ class Parser(BaseParser): def __init__(self, manifest: ResourceManifest, resource_path: str): self.manifest = manifest self.type = "json" - res = requests.get(resource_path) + try: + res = requests.get(resource_path) + except: + raise MissingResourceError(f"{resource_path}") if res.status_code >= 200 and res.status_code < 300: if "xml" in res.headers['content-type']: self.type = "xml" diff --git a/mtd/tests/__init__.py b/mtd/tests/__init__.py index c41c4f6..6091d13 100644 --- a/mtd/tests/__init__.py +++ b/mtd/tests/__init__.py @@ -65,7 +65,7 @@ def emit(self, record): 'word': 'rød'} ] -SAMPLE_DATA_OBJ = [ +SAMPLE_DATA_OBJ_BLANK = [ { 'audio': [{'speaker': 'AP', 'filename': 'ap_red.mp3'}, {'speaker': 'AR', 'filename': 'ar_red.mp3'}], 'definition': 'red', diff --git a/mtd/tests/run.py b/mtd/tests/run.py index 4364aae..63b2551 100644 --- a/mtd/tests/run.py +++ b/mtd/tests/run.py @@ -32,7 +32,7 @@ loader.loadTestsFromTestCase(test) # for test in [JsonParserTest] # for test in (CsvParserTest, PsvParserTest, TsvParserTest, XlsxParserTest) - for test in [XlsxParserTest] + for test in [RequestsParserTest] # for test in (CsvParserTest, DictParserTest, JsonParserTest, PsvParserTest, RequestsParserTest, TsvParserTest, XlsxParserTest, XmlParserTest) ] diff --git a/mtd/tests/test_request_parser.py b/mtd/tests/test_request_parser.py index 535b29c..9824e64 100644 --- a/mtd/tests/test_request_parser.py +++ b/mtd/tests/test_request_parser.py @@ -4,6 +4,7 @@ from mtd.tests.test_data import json as json_path, xml as xml_path from unittest import TestCase from mtd.parsers import parse +from mtd.exceptions import MissingResourceError, RequestException, UnsupportedFiletypeError class RequestsParserTest(TestCase): def setUp(self): @@ -44,4 +45,22 @@ def test_data_obj_matches_json_sample(self): for data in self.json_data: parsed_data = parse(self.json_manifest, data[0]) parsed_data_obj = parsed_data['data'].to_dict(orient='records') - self.assertEqual(data[2], parsed_data_obj) \ No newline at end of file + self.assertEqual(data[2], parsed_data_obj) + + def test_no_connection(self): + '''Check can't connect to site + ''' + with self.assertRaises(MissingResourceError): + parse(self.json_manifest, 'https://foo.bar') + + def test_404(self): + '''Test returns request exception from 404 + ''' + with self.assertRaises(RequestException): + parse(self.json_manifest, 'https://www.google.com/foobar1') + + def test_bad_format(self): + '''Check non-json/xml returns unsupported format error + ''' + with self.assertRaises(UnsupportedFiletypeError): + parse(self.json_manifest, 'https://google.com') \ No newline at end of file diff --git a/mtd/tests/test_xlsx_parser.py b/mtd/tests/test_xlsx_parser.py index b02c291..dfe5746 100644 --- a/mtd/tests/test_xlsx_parser.py +++ b/mtd/tests/test_xlsx_parser.py @@ -1,5 +1,5 @@ from mtd.tests.test_data import xlsx -from mtd.tests import SAMPLE_DATA_DF, SAMPLE_DATA_OBJ +from mtd.tests import SAMPLE_DATA_DF, SAMPLE_DATA_OBJ_BLANK import os from unittest import TestCase from mtd.parsers import parse @@ -25,7 +25,7 @@ def test_data_obj_matches_sample(self): ''' parsed_data = parse(self.manifest, self.data) parsed_data_obj = parsed_data['data'].to_dict(orient='records') - self.assertEqual(SAMPLE_DATA_OBJ, parsed_data_obj) + self.assertEqual(SAMPLE_DATA_OBJ_BLANK, parsed_data_obj) def test_missing_sheet(self): '''Check error raised for missing sheet in 'location'