test_iqr()   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
c 0
b 0
f 0
dl 0
loc 46
rs 2.6716

How to fix   Complexity   

Complexity

Complex classes like test_iqr() 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
from pytest import mark
2
3
from pytest_benchmark.stats import Stats
4
5
6 View Code Duplication
def test_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
    stats = Stats()
8
    for i in 4., 36., 45., 50., 75.:
9
        stats.update(i)
10
    assert stats.mean == 42.
11
    assert stats.min == 4.
12
    assert stats.max == 75.
13
    assert stats.stddev == 25.700194551792794
14
    assert stats.rounds == 5
15
    assert stats.total == 210.
16
    assert stats.ops == 0.023809523809523808
17
18
19 View Code Duplication
def test_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
    stats = Stats()
21
    stats.update(17.)
22
    stats.update(19.)
23
    stats.update(24.)
24
    assert stats.mean == 20.
25
    assert stats.min == 17.
26
    assert stats.max == 24.
27
    assert stats.stddev == 3.605551275463989
28
    assert stats.rounds == 3
29
    assert stats.total == 60.
30
    assert stats.ops == 0.05
31
32
33
def test_single_item():
34
    stats = Stats()
35
    stats.update(1)
36
    assert stats.mean == 1
37
    assert stats.median == 1
38
    assert stats.iqr_outliers == 0
39
    assert stats.stddev_outliers == 0
40
    assert stats.min == 1
41
    assert stats.max == 1
42
    assert stats.stddev == 0
43
    assert stats.iqr == 0
44
    assert stats.rounds == 1
45
    assert stats.total == 1
46
    assert stats.ld15iqr == 1
47
    assert stats.hd15iqr == 1
48
    assert stats.ops == 1
49
50
51
@mark.parametrize('length', range(1, 10))
52
def test_length(length):
53
    stats = Stats()
54
    for i in range(length):
55
        stats.update(1)
56
57
    assert stats.as_dict()
58
59
60
def test_iqr():
61
    stats = Stats()
62
    for i in 6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49:
63
        stats.update(i)
64
    assert stats.iqr == 22.5  # https://en.wikipedia.org/wiki/Quartile#Example_1
65
66
    stats = Stats()
67
    for i in 7, 15, 36, 39, 40, 41:
68
        stats.update(i)
69
    assert stats.iqr == 25.0  # https://en.wikipedia.org/wiki/Quartile#Example_2
70
71
    stats = Stats()
72
    for i in 1, 2, 3, 4, 5, 6, 7, 8, 9:
73
        stats.update(i)
74
    assert stats.iqr == 4.5  # http://www.phusewiki.org/docs/2012/PRESENTATIONS/SP/SP06%20.pdf - method 1
75
76
    stats = Stats()
77
    for i in 1, 2, 3, 4, 5, 6, 7, 8:
78
        stats.update(i)
79
    assert stats.iqr == 4.0  # http://www.lexjansen.com/nesug/nesug07/po/po08.pdf - method 1
80
81
    stats = Stats()
82
    for i in 1, 2, 1, 123, 4, 1234, 1, 234, 12, 34, 12, 3, 2, 34, 23:
83
        stats.update(i)
84
    assert stats.iqr == 32.0
85
86
    stats = Stats()
87
    for i in 1, 2, 3, 10, 10.1234, 11, 12, 13., 10.1115, 11.1115, 12.1115, 13.5, 10.75, 11.75, 13.12175, 13.1175, 20, \
88
             50, 52:
89
        stats.update(i)
90
    assert stats.stddev == 13.518730097622106
91
    assert stats.iqr == 3.006212500000002  # close enough: http://www.wessa.net/rwasp_variability.wasp
92
93
    stats = Stats()
94
    for i in [
95
        11.2, 11.8, 13.2, 12.9, 12.1, 13.5, 14.8, 14.8, 13.6, 11.9, 10.4, 11.8, 11.5, 12.6, 14.1, 13.5, 12.5, 14.9,
96
        17.0, 17.0, 15.8, 13.3, 11.4, 14.0, 14.5, 15.0, 17.8, 16.3, 17.2, 17.8, 19.9, 19.9, 18.4, 16.2, 14.6, 16.6,
97
        17.1, 18.0, 19.3, 18.1, 18.3, 21.8, 23.0, 24.2, 20.9, 19.1, 17.2, 19.4, 19.6, 19.6, 23.6, 23.5, 22.9, 24.3,
98
        26.4, 27.2, 23.7, 21.1, 18.0, 20.1, 20.4, 18.8, 23.5, 22.7, 23.4, 26.4, 30.2, 29.3, 25.9, 22.9, 20.3, 22.9,
99
        24.2, 23.3, 26.7, 26.9, 27.0, 31.5, 36.4, 34.7, 31.2, 27.4, 23.7, 27.8, 28.4, 27.7, 31.7, 31.3, 31.8, 37.4,
100
        41.3, 40.5, 35.5, 30.6, 27.1, 30.6, 31.5, 30.1, 35.6, 34.8, 35.5, 42.2, 46.5, 46.7, 40.4, 34.7, 30.5, 33.6,
101
        34.0, 31.8, 36.2, 34.8, 36.3, 43.5, 49.1, 50.5, 40.4, 35.9, 31.0, 33.7, 36.0, 34.2, 40.6, 39.6, 42.0, 47.2,
102
        54.8, 55.9, 46.3, 40.7, 36.2, 40.5, 41.7, 39.1, 41.9, 46.1, 47.2, 53.5, 62.2, 60.6, 50.8, 46.1, 39.0, 43.2,
103
    ]:
104
        stats.update(i)
105
    assert stats.iqr == 18.1  # close enough: http://www.wessa.net/rwasp_variability.wasp
106
107
108
def test_ops():
109
    stats = Stats()
110
    stats.update(0)
111
    assert stats.mean == 0
112
    assert stats.ops == 0
113