Skip to content

Commit

Permalink
Merge pull request #45 from laixintao/tests
Browse files Browse the repository at this point in the history
tests! more tests!
  • Loading branch information
laixintao committed Oct 7, 2023
2 parents fdf46c9 + e3c2367 commit 1b5db8d
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 15 deletions.
10 changes: 8 additions & 2 deletions flameshow/render/flamegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,17 @@ def render_line(self, y: int) -> Strip:
line = self.profile.lines[y]

if not self.frame_maps:
# never should happen
# pragma: no cover
raise RenderException("frame_maps is not init yet!")

segments = []
cursor = 0
for frame in line:
frame_maps = self.frame_maps.get(frame._id)
if not frame_maps:
continue
# never should happen
continue # pragma: no cover
frame_map = frame_maps[self.sample_index]
my_width = frame_map.width
if not my_width:
Expand All @@ -185,7 +188,10 @@ def render_line(self, y: int) -> Strip:
segments.append(Segment(" " * pre_pad))
cursor += pre_pad
elif pre_pad < 0:
raise Exception("Prepad is negative! {}".format(pre_pad))
# never should happen
raise Exception(
"Prepad is negative! {}".format(pre_pad)
) # pragma: no cover

if len(text) < my_width:
text += " " * (my_width - len(text))
Expand Down
7 changes: 5 additions & 2 deletions tests/test_cli_click.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
from unittest.mock import MagicMock, patch

from click.testing import CliRunner
from flameshow.main import main, ensure_tty
from unittest.mock import patch, MagicMock

from flameshow.main import ensure_tty, main


def test_print_version():
Expand Down
3 changes: 3 additions & 0 deletions tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def test_linaer_color_platte():

assert isinstance(color2, Color)

for key in range(999):
platte.get_color(key)


def test_flamegraph_random_color_platte():
platte = flamegraph_random_color_platte
Expand Down
19 changes: 19 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging
from flameshow.main import setup_log
import os

from unittest.mock import patch


def test_run_app_with_verbose_logging(data_dir):
# cleanup logfile first
path = data_dir / "._pytest_flameshow.log"
try:
os.remove(path)
except: # noqa
pass

with patch.object(logging, "basicConfig") as mock_config:
setup_log(True, logging.DEBUG, path)

mock_config.assert_called_once()
148 changes: 137 additions & 11 deletions tests/test_render/test_flamegraph.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import MagicMock

import pytest
from textual.events import MouseMove
from textual.events import Click, MouseMove

from flameshow.exceptions import RenderException
from flameshow.models import Frame
Expand Down Expand Up @@ -636,10 +636,34 @@ def test_flamegraph_render_on_mouse_move():
flamegraph_widget.frame_maps = flamegraph_widget.generate_frame_maps(10, 0)
flamegraph_widget.post_message = MagicMock()

mouse_event = MouseMove(
x=2,
y=1,
delta_x=0,
delta_y=0,
button=False,
shift=False,
meta=False,
ctrl=False,
)
flamegraph_widget.on_mouse_move(mouse_event)

flamegraph_widget.post_message.assert_called_once()
args = flamegraph_widget.post_message.call_args[0]
message = args[0]
assert message.by_mouse == True
assert message.frame._id == 2

assert flamegraph_widget.focused_stack_id == 0
flamegraph_widget.handle_click_frame(mouse_event)
assert flamegraph_widget.focused_stack_id == 2

# move to lines that empty
flamegraph_widget.post_message = MagicMock()
flamegraph_widget.on_mouse_move(
MouseMove(
x=2,
y=1,
x=1,
y=2,
delta_x=0,
delta_y=0,
button=False,
Expand All @@ -648,19 +672,35 @@ def test_flamegraph_render_on_mouse_move():
ctrl=False,
)
)
args = flamegraph_widget.post_message.assert_not_called()

# just to move the the exact offset, should still work
# should be hover on next span instead of last
flamegraph_widget.post_message = MagicMock()
flamegraph_widget.on_mouse_move(
MouseMove(
x=3,
y=1,
delta_x=0,
delta_y=0,
button=False,
shift=False,
meta=False,
ctrl=False,
)
)
flamegraph_widget.post_message.assert_called_once()
args = flamegraph_widget.post_message.call_args[0]
message = args[0]
assert message.by_mouse == True
assert message.frame._id == 2
assert message.frame._id == 1

# move to lines that empty
# move down, not hover on any lines
flamegraph_widget.post_message = MagicMock()
flamegraph_widget.on_mouse_move(
MouseMove(
x=1,
y=2,
x=0,
y=3,
delta_x=0,
delta_y=0,
button=False,
Expand All @@ -671,12 +711,97 @@ def test_flamegraph_render_on_mouse_move():
)
args = flamegraph_widget.post_message.assert_not_called()

# just to move the the exact offset, should still work
# should be hover on next span instead of last

def test_flamegraph_render_line_with_some_width_is_0():
id_store = {}
root = create_frame(
{
"id": 0,
"values": [10],
"children": [
{"id": 2, "values": [3], "children": []},
{
"id": 1,
"values": [2],
"children": [
{"id": 3, "values": [1], "children": []},
],
},
{"id": 4, "values": [0.1], "children": []},
],
},
id_store,
)

p = Profile(
filename="abc",
root_stack=root,
highest_lines=1,
total_sample=2,
sample_types=[SampleType("samples", "count")],
id_store=id_store,
)
flamegraph_widget = FlameGraph(p, 0, -1, 0)
flamegraph_widget.frame_maps = flamegraph_widget.generate_frame_maps(
10, focused_stack_id=0
)

strip = flamegraph_widget.render_line(
1,
)

line_strings = [seg.text for seg in strip._segments]

assert line_strings == ["▏", "no", "▏", "n"]


def test_flamegraph_render_line_with_focused_frame():
id_store = {}
root = create_frame(
{
"id": 0,
"values": [10],
"children": [
{"id": 1, "values": [3], "children": []},
{"id": 4, "values": [1], "children": []},
{
"id": 2,
"values": [6],
"children": [
{"id": 3, "values": [1], "children": []},
],
},
],
},
id_store,
)

p = Profile(
filename="abc",
root_stack=root,
highest_lines=1,
total_sample=2,
sample_types=[SampleType("samples", "count")],
id_store=id_store,
)
flamegraph_widget = FlameGraph(p, 2, -1, 0)
flamegraph_widget.frame_maps = flamegraph_widget.generate_frame_maps(
10, focused_stack_id=2
)

strip = flamegraph_widget.render_line(
1,
)

line_strings = [seg.text for seg in strip._segments]

assert line_strings == ["▏", "node-2 "]

flamegraph_widget.post_message = MagicMock()

flamegraph_widget.on_mouse_move(
MouseMove(
x=3,
x=0,
y=1,
delta_x=0,
delta_y=0,
Expand All @@ -686,8 +811,9 @@ def test_flamegraph_render_on_mouse_move():
ctrl=False,
)
)

flamegraph_widget.post_message.assert_called_once()
args = flamegraph_widget.post_message.call_args[0]
message = args[0]
assert message.by_mouse == True
assert message.frame._id == 1
assert message.frame._id == 2

0 comments on commit 1b5db8d

Please sign in to comment.