def test_make_api_call_partial_url_not_cached(urllib2): """ Test ``make_api_call`` with partial url, not cached. Tests that :py:meth:`~.RallyAccessor.make_api_call`: * makes a call to the API via urllib. * prepends the api_url to the partial url. * makes the url given safe. * stores the new data in the cache. """ MEM_CACHE.clear() my_accessor = RallyAccessor('uname', 'pword', 'base_url') my_accessor.api_url = 'http://dummy_url/' my_accessor.get_from_cache = Mock() my_accessor.set_to_cache = Mock() my_accessor.make_url_safe = Mock() my_accessor._get_json_response = Mock() my_accessor.make_url_safe.return_value = 'safe-url' my_accessor.get_from_cache.return_value = False my_accessor._get_json_response.return_value = 'python_dict' response = my_accessor.make_api_call('some-url', full_url=True) assert_equal(response, 'python_dict') assert_equal(my_accessor.make_url_safe.call_args[0], ('some-url',)) assert_equal(my_accessor._get_json_response.call_args[0], (urllib2.Request.return_value,)) assert_equal(my_accessor.get_from_cache.call_args[0], ('safe-url',)) assert_equal(my_accessor.set_to_cache.call_args[0], ('safe-url', 'python_dict'))
def test_make_api_call_partial_url_not_cached(urllib2): """ Test ``make_api_call`` with partial url, not cached. Tests that :py:meth:`~.RallyAccessor.make_api_call`: * makes a call to the API via urllib. * prepends the api_url to the partial url. * makes the url given safe. * stores the new data in the cache. """ MEM_CACHE.clear() my_accessor = RallyAccessor('uname', 'pword', 'base_url') my_accessor.api_url = 'http://dummy_url/' my_accessor.get_from_cache = Mock() my_accessor.set_to_cache = Mock() my_accessor.make_url_safe = Mock() my_accessor._get_json_response = Mock() my_accessor.make_url_safe.return_value = 'safe-url' my_accessor.get_from_cache.return_value = False my_accessor._get_json_response.return_value = 'python_dict' response = my_accessor.make_api_call('some-url', full_url=True) assert_equal(response, 'python_dict') assert_equal(my_accessor.make_url_safe.call_args[0], ('some-url', )) assert_equal(my_accessor._get_json_response.call_args[0], (urllib2.Request.return_value, )) assert_equal(my_accessor.get_from_cache.call_args[0], ('safe-url', )) assert_equal(my_accessor.set_to_cache.call_args[0], ('safe-url', 'python_dict'))
def test_make_url_safe(): """Test that :py:meth:`.RallyAccessor.make_url_safe` works correctly.""" my_accessor = RallyAccessor('uname', 'pword', 'base_url') for url, expected_outcome in [(' ', "%20"), ('(', "%28"), (')', "%29"), ('"', "%22"), ('(Hello = "Fred")', '%28Hello%20=%20%22Fred%22%29')]: assert_equal(my_accessor.make_url_safe(url), expected_outcome)
def test_make_url_safe(): """Test that :py:meth:`.RallyAccessor.make_url_safe` works correctly.""" my_accessor = RallyAccessor('uname', 'pword', 'base_url') for url, expected_outcome in [ (' ', "%20"), ('(', "%28"), (')', "%29"), ('"', "%22"), ('(Hello = "Fred")', '%28Hello%20=%20%22Fred%22%29')]: assert_equal(my_accessor.make_url_safe(url), expected_outcome)
def test_make_api_call_full_url_cached(urllib2): """ Test ``make_api_call`` with full, cached url. Tests that :py:meth:`~.RallyAccessor.make_api_call`: * looks in the cache first and does not make a urllib call. * uses the url given without amendments. * makes the url given safe. """ my_accessor = RallyAccessor('uname', 'pword', 'base_url') my_accessor.get_from_cache = Mock() my_accessor.make_url_safe = Mock() my_accessor.make_url_safe.return_value = 'safe-url' my_accessor.get_from_cache.return_value = 'Data' response = my_accessor.make_api_call('some-url', full_url=True) assert_equal(response, 'Data') assert_equal(my_accessor.make_url_safe.call_args[0], ('some-url',)) assert_equal(my_accessor.get_from_cache.call_args[0], ('safe-url',)) assert_false(urllib2.urlopen.called)
def test_make_api_call_full_url_cached(urllib2): """ Test ``make_api_call`` with full, cached url. Tests that :py:meth:`~.RallyAccessor.make_api_call`: * looks in the cache first and does not make a urllib call. * uses the url given without amendments. * makes the url given safe. """ my_accessor = RallyAccessor('uname', 'pword', 'base_url') my_accessor.get_from_cache = Mock() my_accessor.make_url_safe = Mock() my_accessor.make_url_safe.return_value = 'safe-url' my_accessor.get_from_cache.return_value = 'Data' response = my_accessor.make_api_call('some-url', full_url=True) assert_equal(response, 'Data') assert_equal(my_accessor.make_url_safe.call_args[0], ('some-url', )) assert_equal(my_accessor.get_from_cache.call_args[0], ('safe-url', )) assert_false(urllib2.urlopen.called)