test_django_add_or_set_locked()   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
c 2
b 0
f 0
dl 0
loc 20
rs 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A none_creator() 0 2 1
A assert_false_creator() 0 2 2
A creator_42() 0 2 1

How to fix   Complexity   

Complexity

Complex classes like test_django_add_or_set_locked() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import pytest
2
3
try:
4
    import django
5
except ImportError:
6
    django = None
7
else:
8
    from django.core.cache import cache
9
10
11
@pytest.mark.skipif("not django")
12
def test_django_works(redis_server):
13
    with cache.lock('whateva'):
14
        pass
15
16
17
@pytest.mark.skipif("not django")
18
def test_django_add_or_set_locked(redis_server):
19
    def creator_42():
20
        return 42
21
22
    def none_creator():
23
        return None
24
25
    def assert_false_creator():
26
        assert False
27
28
    assert cache.locked_get_or_set("foobar-aosl", creator_42) == 42
29
    assert cache.locked_get_or_set("foobar-aosl", assert_false_creator) == 42
30
31
    try:
32
        val = cache.locked_get_or_set("foobar-aosl2", none_creator)
33
    except ValueError:
34
        pass
35
    else:
36
        assert False
37
38
39
@pytest.mark.skipif("not django")
40
def test_reset_all(redis_server):
41
    lock1 = cache.lock("foobar1")
42
    lock2 = cache.lock("foobar2")
43
    lock1.acquire(blocking=False)
44
    lock2.acquire(blocking=False)
45
    cache.reset_all()
46
    lock1 = cache.lock("foobar1")
47
    lock2 = cache.lock("foobar2")
48
    lock1.acquire(blocking=False)
49
    lock2.acquire(blocking=False)
50
    lock1.release()
51
    lock2.release()
52