Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/test_api_lookup.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import pytest | |||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.api.views import utils | from swh.web.api.views import utils | ||||
from swh.web.tests.testcase import WebTestCase | |||||
class ApiLookupTestCase(WebTestCase): | def test_genericapi_lookup_nothing_is_found(): | ||||
def test_genericapi_lookup_nothing_is_found(self): | |||||
# given | |||||
def test_generic_lookup_fn(sha1, another_unused_arg): | def test_generic_lookup_fn(sha1, another_unused_arg): | ||||
assert another_unused_arg == 'unused_arg' | assert another_unused_arg == 'unused_arg' | ||||
assert sha1 == 'sha1' | assert sha1 == 'sha1' | ||||
return None | return None | ||||
# when | notfound_msg = 'This will be raised because None is returned.' | ||||
with self.assertRaises(NotFoundExc) as cm: | |||||
with pytest.raises(NotFoundExc) as e: | |||||
utils.api_lookup( | utils.api_lookup( | ||||
test_generic_lookup_fn, 'sha1', 'unused_arg', | test_generic_lookup_fn, 'sha1', 'unused_arg', | ||||
notfound_msg='This will be raised because None is returned.') | notfound_msg=notfound_msg) | ||||
assert e.match(notfound_msg) | |||||
self.assertIn('This will be raised because None is returned.', | def test_generic_api_map_are_enriched_and_transformed_to_list(): | ||||
cm.exception.args[0]) | |||||
def test_generic_api_map_are_enriched_and_transformed_to_list(self): | |||||
# given | |||||
def test_generic_lookup_fn_1(criteria0, param0, param1): | def test_generic_lookup_fn_1(criteria0, param0, param1): | ||||
assert criteria0 == 'something' | assert criteria0 == 'something' | ||||
return map(lambda x: x + 1, [1, 2, 3]) | return map(lambda x: x + 1, [1, 2, 3]) | ||||
# when | |||||
actual_result = utils.api_lookup( | actual_result = utils.api_lookup( | ||||
test_generic_lookup_fn_1, 'something', 'some param 0', | test_generic_lookup_fn_1, 'something', 'some param 0', | ||||
'some param 1', | 'some param 1', | ||||
notfound_msg=('This is not the error message you are looking for. ' | notfound_msg=('This is not the error message you are looking for. ' | ||||
'Move along.'), | 'Move along.'), | ||||
enrich_fn=lambda x: x * 2) | enrich_fn=lambda x: x * 2) | ||||
self.assertEqual(actual_result, [4, 6, 8]) | assert actual_result == [4, 6, 8] | ||||
def test_generic_api_list_are_enriched_too(): | |||||
def test_generic_api_list_are_enriched_too(self): | |||||
# given | |||||
def test_generic_lookup_fn_2(crit): | def test_generic_lookup_fn_2(crit): | ||||
assert crit == 'something' | assert crit == 'something' | ||||
return ['a', 'b', 'c'] | return ['a', 'b', 'c'] | ||||
# when | |||||
actual_result = utils.api_lookup( | actual_result = utils.api_lookup( | ||||
test_generic_lookup_fn_2, 'something', | test_generic_lookup_fn_2, 'something', | ||||
notfound_msg=('Not the error message you are looking for, it is. ' | notfound_msg=('Not the error message you are looking for, it is. ' | ||||
'Along, you move!'), | 'Along, you move!'), | ||||
enrich_fn=lambda x: ''. join(['=', x, '='])) | enrich_fn=lambda x: ''. join(['=', x, '='])) | ||||
self.assertEqual(actual_result, ['=a=', '=b=', '=c=']) | assert actual_result == ['=a=', '=b=', '=c='] | ||||
def test_generic_api_generator_are_enriched_and_returned_as_list(): | |||||
def test_generic_api_generator_are_enriched_and_returned_as_list(self): | |||||
# given | |||||
def test_generic_lookup_fn_3(crit): | def test_generic_lookup_fn_3(crit): | ||||
assert crit == 'crit' | assert crit == 'crit' | ||||
return (i for i in [4, 5, 6]) | return (i for i in [4, 5, 6]) | ||||
# when | |||||
actual_result = utils.api_lookup( | actual_result = utils.api_lookup( | ||||
test_generic_lookup_fn_3, 'crit', | test_generic_lookup_fn_3, 'crit', | ||||
notfound_msg='Move!', | notfound_msg='Move!', | ||||
enrich_fn=lambda x: x - 1) | enrich_fn=lambda x: x - 1) | ||||
self.assertEqual(actual_result, [3, 4, 5]) | assert actual_result == [3, 4, 5] | ||||
def test_generic_api_simple_data_are_enriched_and_returned_too(): | |||||
def test_generic_api_simple_data_are_enriched_and_returned_too(self): | |||||
# given | |||||
def test_generic_lookup_fn_4(crit): | def test_generic_lookup_fn_4(crit): | ||||
assert crit == '123' | assert crit == '123' | ||||
return {'a': 10} | return {'a': 10} | ||||
def test_enrich_data(x): | def test_enrich_data(x): | ||||
x['a'] = x['a'] * 10 | x['a'] = x['a'] * 10 | ||||
return x | return x | ||||
# when | |||||
actual_result = utils.api_lookup( | actual_result = utils.api_lookup( | ||||
test_generic_lookup_fn_4, '123', | test_generic_lookup_fn_4, '123', | ||||
notfound_msg='Nothing to do', | notfound_msg='Nothing to do', | ||||
enrich_fn=test_enrich_data) | enrich_fn=test_enrich_data) | ||||
self.assertEqual(actual_result, {'a': 100}) | assert actual_result == {'a': 100} | ||||
def test_api_lookup_not_found(self): | |||||
# when | def test_api_lookup_not_found(): | ||||
with self.assertRaises(NotFoundExc) as e: | notfound_msg = 'this is the error message raised as it is None' | ||||
with pytest.raises(NotFoundExc) as e: | |||||
utils.api_lookup( | utils.api_lookup( | ||||
lambda x: None, 'something', | lambda x: None, 'something', | ||||
notfound_msg='this is the error message raised as it is None') | notfound_msg=notfound_msg) | ||||
assert e.match(notfound_msg) | |||||
self.assertEqual(e.exception.args[0], | |||||
'this is the error message raised as it is None') | |||||
def test_api_lookup_with_result(self): | def test_api_lookup_with_result(): | ||||
# when | |||||
actual_result = utils.api_lookup( | actual_result = utils.api_lookup( | ||||
lambda x: x + '!', 'something', | lambda x: x + '!', 'something', | ||||
notfound_msg='this is the error which won\'t be used here') | notfound_msg='this is the error which won\'t be used here') | ||||
self.assertEqual(actual_result, 'something!') | assert actual_result == 'something!' | ||||
def test_api_lookup_with_result_as_map(self): | def test_api_lookup_with_result_as_map(): | ||||
# when | |||||
actual_result = utils.api_lookup( | actual_result = utils.api_lookup( | ||||
lambda x: map(lambda y: y+1, x), [1, 2, 3], | lambda x: map(lambda y: y+1, x), [1, 2, 3], | ||||
notfound_msg='this is the error which won\'t be used here') | notfound_msg='this is the error which won\'t be used here') | ||||
self.assertEqual(actual_result, [2, 3, 4]) | assert actual_result == [2, 3, 4] |