예제 #1
0
def test_clear(mock_app, mock_record):
    """Test clearing individual and all resources from cache"""

    cache = ResourcesCache(5)

    cache.cache(mock_app)
    cache.cache(mock_record)

    assert len(cache) == 2

    # Clear by resource class type
    cache.clear(type(mock_app))

    assert len(cache) == 1
    assert mock_app not in cache
    assert mock_record in cache

    cache.cache(mock_app)

    assert len(cache) == 2

    # Clear all caches
    cache.clear()

    assert len(cache) == 0
    assert mock_app not in cache
    assert mock_record not in cache
예제 #2
0
def test_get_item_from_cache(mock_record):
    """Test retrieving item from cache, and that item is a copy instead of reference to same instance"""

    cache = ResourcesCache(5)

    cache_key = (type(mock_record), 'id', mock_record.id)

    # Attempt to get before record is in cache
    with pytest.raises(KeyError):
        cached_record = cache[cache_key]

    cache.cache(mock_record)

    cached_record = cache[cache_key]

    assert cached_record == mock_record
    assert cached_record is not mock_record

    # Prove failure after clearing cache
    cache.clear()
    with pytest.raises(KeyError):
        cached_record = cache[cache_key]