Completed
Push — develop ( 9c9256...041c3e )
by Jace
9s
created

describe_get()   A

Complexity

Conditions 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
c 2
b 0
f 0
dl 0
loc 13
rs 9.2
1
# pylint: disable=missing-docstring,unused-variable,unused-argument,expression-not-assigned,singleton-comparison
2
3
import os
4
5
import pytest
6
from expecter import expect
7
8
from coveragespace.cache import Cache
9
10
11
def describe_cache():
12
13
    @pytest.fixture
14
    def cache():
15
        return Cache()
16
17
    @pytest.fixture
18
    def cache_empty(cache):
19
        # pylint: disable=protected-access
20
        cache._data.clear()
21
        return cache
22
23
    @pytest.fixture
24
    def cache_missing(cache_empty):
25
        try:
26
            os.remove(Cache.PATH)
27
        except OSError:
28
            pass
29
        return cache_empty
30
31
    @pytest.fixture
32
    def cache_corrupt(cache):
33
        # pylint: disable=protected-access
34
        cache._data = "corrupt"
35
        cache._store()
36
        return cache
37
38
    def describe_init():
39
40
        def it_loads_previous_results(cache_empty):
41
            cache_empty.set(("url", {}), "previous")
42
43
            cache = Cache()
44
            expect(cache.get(("url", {}))) == "previous"
45
46
        def it_handles_missing_cache_files(cache_missing):
47
            expect(Cache().get(("url", {}))) == None
48
49
        def it_handles_corrupt_cache_files(cache_corrupt):
50
            expect(Cache().get(("url", {}))) == None
51
52
    def describe_get():
53
54
        def it_hits_with_existing_data(cache_empty):
55
            cache = cache_empty
56
            cache.set(("url", {}), "existing")
57
58
            expect(cache.get(("url", {}))) == "existing"
59
60
        def it_misses_with_no_data(cache_empty):
61
            expect(cache_empty.get(("url", {}))) == None
62
63
        def it_returns_the_default_on_miss(cache_empty):
64
            expect(cache_empty.get("foo", 42)) == 42
65