コード例 #1
0
def test_get_from_cache_retrieves_correctly():
    """
    Test ``get_from_cache``.

    Tests that :py:meth:`~.RallyAccessor.get_from_cache`:
        * returns the data at the specified cache location if present
        * returns False if not present
    """
    MEM_CACHE.clear()
    MEM_CACHE['cache_key']['cache_lookup'] = ('data', time.time())

    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.get_cacheable_info = Mock()
    my_accessor.cache_timeouts = Mock()
    my_accessor.cache_timeouts.get.return_value = 10

    for (key, index), expected_result in [
                                    (('cache_key', 'cache_lookup'), 'data'),
                                    (('another_key', 'another_lookup'), False),
                                    ]:
        my_accessor.get_cacheable_info.reset_mock()
        my_accessor.get_cacheable_info.return_value = (key, index)

        assert_equal(my_accessor.get_from_cache('url'), expected_result)

        assert_equal(my_accessor.get_cacheable_info.call_args[0], ('url',))
        assert_equal(my_accessor.cache_timeouts.get.call_args[0],
                           (key, my_accessor.default_cache_timeout))
コード例 #2
0
def test_get_from_cache_retrieves_correctly():
    """
    Test ``get_from_cache``.

    Tests that :py:meth:`~.RallyAccessor.get_from_cache`:
        * returns the data at the specified cache location if present
        * returns False if not present
    """
    MEM_CACHE.clear()
    MEM_CACHE['cache_key']['cache_lookup'] = ('data', time.time())

    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.get_cacheable_info = Mock()
    my_accessor.cache_timeouts = Mock()
    my_accessor.cache_timeouts.get.return_value = 10

    for (key, index), expected_result in [
        (('cache_key', 'cache_lookup'), 'data'),
        (('another_key', 'another_lookup'), False),
    ]:
        my_accessor.get_cacheable_info.reset_mock()
        my_accessor.get_cacheable_info.return_value = (key, index)

        assert_equal(my_accessor.get_from_cache('url'), expected_result)

        assert_equal(my_accessor.get_cacheable_info.call_args[0], ('url', ))
        assert_equal(my_accessor.cache_timeouts.get.call_args[0],
                     (key, my_accessor.default_cache_timeout))
コード例 #3
0
def test_get_cacheable_info():
    """
    Test ``get_cacheable_info``.

    Tests that :py:meth:`~.RallyAccessor.get_cacheable_info`:
        * returns the right cache key and index
    """
    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.api_url = 'http://dummy_url/'

    for url, expected_tuple in [
                ('obj.js?query=something', ('obj_query', 'query=something')),
                ('obj/303923.js', ('obj', '303923')),
                ]:
        full_url = "{0}{1}".format(my_accessor.api_url, url)
        assert_equal(my_accessor.get_cacheable_info(full_url), expected_tuple)
コード例 #4
0
def test_get_cacheable_info():
    """
    Test ``get_cacheable_info``.

    Tests that :py:meth:`~.RallyAccessor.get_cacheable_info`:
        * returns the right cache key and index
    """
    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.api_url = 'http://dummy_url/'

    for url, expected_tuple in [
        ('obj.js?query=something', ('obj_query', 'query=something')),
        ('obj/303923.js', ('obj', '303923')),
    ]:
        full_url = "{0}{1}".format(my_accessor.api_url, url)
        assert_equal(my_accessor.get_cacheable_info(full_url), expected_tuple)
コード例 #5
0
def test_get_from_cache_returns_false_if_out_of_date():
    """
    Test ``get_from_cache``.

    Tests that :py:meth:`~.RallyAccessor.get_from_cache`:
        * returns False if data is out of date
    """
    MEM_CACHE.clear()
    MEM_CACHE['cache_key']['cache_lookup'] = ('data', 0)

    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.get_cacheable_info = Mock()
    my_accessor.cache_timeouts = Mock()
    my_accessor.cache_timeouts.get.return_value = 0

    my_accessor.get_cacheable_info.return_value = ('cache_key', 'cache_lookup')

    assert_equal(my_accessor.get_from_cache('url'), False)
コード例 #6
0
def test_get_from_cache_returns_false_if_out_of_date():
    """
    Test ``get_from_cache``.

    Tests that :py:meth:`~.RallyAccessor.get_from_cache`:
        * returns False if data is out of date
    """
    MEM_CACHE.clear()
    MEM_CACHE['cache_key']['cache_lookup'] = ('data', 0)

    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.get_cacheable_info = Mock()
    my_accessor.cache_timeouts = Mock()
    my_accessor.cache_timeouts.get.return_value = 0

    my_accessor.get_cacheable_info.return_value = ('cache_key', 'cache_lookup')

    assert_equal(my_accessor.get_from_cache('url'), False)
コード例 #7
0
def test_set_to_cache_adds_correctly(time_import):
    """
    Test ``set_to_cache``.

    Tests that :py:meth:`~.RallyAccessor.set_to_cache`:
        * adds the given data to the cache.
    """
    MEM_CACHE.clear()

    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.get_cacheable_info = Mock()
    my_accessor.get_cacheable_info.return_value = ('cache_key', 'cache_lookup')

    my_accessor.set_to_cache('url', 'set_to_cache_test')

    assert_equal(my_accessor.get_cacheable_info.call_args[0], ('url',))
    assert_equal(MEM_CACHE,
                 {'cache_key':
                    {'cache_lookup':
                        ('set_to_cache_test',
                         time_import.time.return_value)}})
コード例 #8
0
def test_set_to_cache_adds_correctly(time_import):
    """
    Test ``set_to_cache``.

    Tests that :py:meth:`~.RallyAccessor.set_to_cache`:
        * adds the given data to the cache.
    """
    MEM_CACHE.clear()

    my_accessor = RallyAccessor('uname', 'pword', 'base_url')
    my_accessor.get_cacheable_info = Mock()
    my_accessor.get_cacheable_info.return_value = ('cache_key', 'cache_lookup')

    my_accessor.set_to_cache('url', 'set_to_cache_test')

    assert_equal(my_accessor.get_cacheable_info.call_args[0], ('url', ))
    assert_equal(
        MEM_CACHE, {
            'cache_key': {
                'cache_lookup':
                ('set_to_cache_test', time_import.time.return_value)
            }
        })