Completed
Pull Request — master (#12)
by Jitse
01:00
created

test_profile_and_display_results()   F

Complexity

Conditions 15

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
c 2
b 0
f 0
dl 0
loc 36
rs 2.7451

How to fix   Complexity   

Complexity

Complex classes like test_profile_and_display_results() 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
# -*- coding: utf-8 -*-
2
#
3
# Copyright © 2017 Spyder Project Contributors
4
# Licensed under the terms of the MIT License
5
# (see LICENSE.txt for details)
6
7
"""Tests for memoryprofiler.py."""
8
9
# Standard library imports
10
import os
11
12
# Third party imports
13
from pytestqt import qtbot
14
from qtpy.QtCore import Qt
15
16
from spyder.utils.qthelpers import qapplication
17
MAIN_APP = qapplication() 
18
19
# Local imports
20
from spyder_memory_profiler.widgets.memoryprofiler import MemoryProfilerWidget
21
22
try:
23
    from unittest.mock import Mock
24
except ImportError:
25
    from mock import Mock  # Python 2
26
27
28
TEST_SCRIPT = \
29
"""@profile
30
def foo():
31
    a = [1] * (10 ** 6)
32
    b = [2] * (2 * 10 ** 7)
33
    del b
34
    return a
35
foo()"""
36
        
37
def test_profile_and_display_results(qtbot, tmpdir, monkeypatch):
38
    """Run profiler on simple script and check that results are okay."""
39
    os.chdir(tmpdir.strpath)
40
    testfilename = tmpdir.join('test_foo.py').strpath
41
42
    with open(testfilename, 'w') as f:
43
        f.write(TEST_SCRIPT)
44
45
    MockQMessageBox = Mock()
46
    monkeypatch.setattr('spyder_memory_profiler.widgets.memoryprofiler.QMessageBox',
47
                        MockQMessageBox)
48
49
    widget = MemoryProfilerWidget(None)
50
    qtbot.addWidget(widget)
51
    with qtbot.waitSignal(widget.sig_finished, timeout=10000, raising=True):
52
        widget.analyze(testfilename)    
53
54
    MockQMessageBox.assert_not_called()
55
    dt = widget.datatree
56
    assert dt.topLevelItemCount() == 1  # number of functions profiled
57
    
58
    top = dt.topLevelItem(0)                               
59
    assert top.data(0, Qt.DisplayRole).startswith('foo ')
60
    assert top.childCount() == 6
61
    for i in range(6):
62
        assert top.child(i).data(0, Qt.DisplayRole) == i + 1  # line no
63
64
    # column 2 has increment (in Mib); displayed as 'xxx MiB' so need to strip
65
    # last 4 characters
66
    assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) >= 7  # increment (MiB)
67
    assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) <= 8
68
    assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) >= 150
69
    assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) <= 160
70
    assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) >= -160
71
    assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) <= -150
72
    assert float(top.child(5).data(2, Qt.DisplayRole)[:-4]) == 0
73