예제 #1
0
def test_unsupported_api_resource_instance(mock_swimlane):
    """Test that APIResource instances not returning all cache details are ignored but don't fail"""
    cache = ResourcesCache(5)

    cache.cache(APIResource(mock_swimlane, {}))

    assert len(cache) == 0
예제 #2
0
def test_item_in_cache(mock_record):
    """Test checking if item exists in cache"""

    cache = ResourcesCache(5)

    assert mock_record not in cache

    cache.cache(mock_record)

    assert mock_record in cache
def test_caching(mock_user, mock_group):
    """Test support for User/Group caching"""

    cache = ResourcesCache(5)

    cache.cache(mock_user)
    cache.cache(mock_group)

    assert len(cache) == 2
    assert mock_user in cache
    assert mock_group in cache
예제 #4
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
예제 #5
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]
예제 #6
0
def test_len(mock_app, mock_record):
    """Test cache length includes all cached resources"""

    cache = ResourcesCache(10)

    assert len(cache) == 0

    cache.cache(mock_app)
    assert len(cache) == 1

    # Ignore duplicates
    cache.cache(mock_app)
    assert len(cache) == 1

    cache.cache(mock_record)
    assert len(cache) == 2
예제 #7
0
def test_separate_resource_caches(mock_app, mock_record):
    """Test each resource class has a separate queue of max size instead of a single global queue"""

    # Max cache size of 1 per resource type
    cache = ResourcesCache(1)

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

    assert len(cache) == 2

    # Create another record instance
    other_record = copy.copy(mock_record)
    other_record.id = mock_record.id + '123'

    # Check that only one record is still in the cache
    cache.cache(other_record)
    assert len(cache) == 2
예제 #8
0
def test_cache_unsupported_type(item):
    """Test attempting to cache a non-APIResource instance fails"""
    cache = ResourcesCache(5)

    with pytest.raises(TypeError):
        cache.cache(item)