Fix FiWare parser tests

This commit is contained in:
Aitor Magán 2014-07-14 12:16:41 +02:00
parent 1dcb8098b8
commit bdbc1372d5
1 changed files with 29 additions and 32 deletions

View File

@ -8,44 +8,42 @@ from nose_parameterized import parameterized
TEST_CASES = {
'one_ds': {
'host': 'localhost',
'json': '{"customer_name":"test", "resources": [{"url": "http://localhost/dataset/ds1"}]}',
'result': {'errors': [], 'users_datasets': [{'user': 'test', 'datasets': ['ds1']}]}
'json': {"customer_name": "test", "resources": [{"url": "http://localhost/dataset/ds1"}]},
'result': {'users_datasets': [{'user': 'test', 'datasets': ['ds1']}]}
},
'two_ds': {
'host': 'localhost',
'json': '{"customer_name":"test", "resources": [{"url": "http://localhost/dataset/ds1"},' +
'{"url": "http://localhost/dataset/ds2"}]}',
'result': {'errors': [], 'users_datasets': [{'user': 'test', 'datasets': ['ds1', 'ds2']}]}
'json': {"customer_name": "test", "resources": [{"url": "http://localhost/dataset/ds1"},
{"url": "http://localhost/dataset/ds2"}]},
'result': {'users_datasets': [{'user': 'test', 'datasets': ['ds1', 'ds2']}]}
},
'error': {
'host': 'localhost',
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"}]}',
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta'],
'users_datasets': [{'user': 'test', 'datasets': []}]}
'json': {"customer_name": "test", "resources": [{"url": "http://localhosta/dataset/ds1"}]},
'error': 'Dataset ds1 is associated with the CKAN instance located at localhosta',
'result': {'users_datasets': [{'user': 'test', 'datasets': []}]}
},
'error_one_ds': {
'host': 'localhost',
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"},' +
'{"url": "http://localhost/dataset/ds2"}]}',
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta'],
'users_datasets': [{'user': 'test', 'datasets': ['ds2']}]}
'json': {"customer_name": "test", "resources": [{"url": "http://localhosta/dataset/ds1"},
{"url": "http://localhost/dataset/ds2"}]},
'error': 'Dataset ds1 is associated with the CKAN instance located at localhosta',
'result': {'users_datasets': [{'user': 'test', 'datasets': ['ds2']}]}
},
'two_errors': {
'host': 'localhost',
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"},' +
'{"url": "http://localhostb/dataset/ds2"}]}',
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta',
'Dataset ds2 is associated with the CKAN instance located at localhostb'],
'users_datasets': [{'user': 'test', 'datasets': []}]}
'json': {"customer_name": "test", "resources": [{"url": "http://localhosta/dataset/ds1"},
{"url": "http://localhostb/dataset/ds2"}]},
'error': 'Dataset ds1 is associated with the CKAN instance located at localhosta',
'result': {'users_datasets': [{'user': 'test', 'datasets': []}]}
},
'two_errors_two_ds': {
'host': 'example.com',
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"},' +
'{"url": "http://example.es/dataset/ds2"}, {"url": "http://example.com/dataset/ds3"},' +
'{"url": "http://example.com/dataset/ds4"}]}',
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta',
'Dataset ds2 is associated with the CKAN instance located at example.es'],
'users_datasets': [{'user': 'test', 'datasets': ['ds3', 'ds4']}]}
'json': {"customer_name": "test", "resources": [{"url": "http://localhosta/dataset/ds1"},
{"url": "http://example.es/dataset/ds2"}, {"url": "http://example.com/dataset/ds3"},
{"url": "http://example.com/dataset/ds4"}]},
'error': 'Dataset ds1 is associated with the CKAN instance located at localhosta',
'result': {'users_datasets': [{'user': 'test', 'datasets': ['ds3', 'ds4']}]}
},
}
@ -60,12 +58,8 @@ class FiWareParserTest(unittest.TestCase):
self._request = fiware.request
fiware.request = MagicMock()
#self._json_loads = fiware.helpers.json.loads
#fiware.helpers.json.loads = MagicMock()
def tearDown(self):
# Unmock functions
#fiware.helpers.json.loads = self._json_loads
fiware.request = self._request
@parameterized.expand([
@ -80,10 +74,13 @@ class FiWareParserTest(unittest.TestCase):
# Configure
fiware.request.host = TEST_CASES[case]['host']
fiware.request.body = TEST_CASES[case]['json']
# Call the function
result = self.parser.parse_notification()
# Assert that the result is what we expected to be
self.assertEquals(TEST_CASES[case]['result'], result)
if 'error' in TEST_CASES[case]:
with self.assertRaises(fiware.tk.ValidationError) as cm:
self.parser.parse_notification(TEST_CASES[case]['json'])
self.assertEqual(cm.exception.error_dict['message'], TEST_CASES[case]['error'])
else:
result = self.parser.parse_notification(TEST_CASES[case]['json'])
# Assert that the result is what we expected to be
self.assertEquals(TEST_CASES[case]['result'], result)