134 lines
4.1 KiB
Python
134 lines
4.1 KiB
Python
"""Tests for the plotting module."""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
import matplotlib
|
|
matplotlib.use("Agg")
|
|
import matplotlib.pyplot as plt
|
|
|
|
from disk_checker.plotting.plots import draw_histogram, draw_cdf, generate_plot
|
|
import pandas as pd
|
|
|
|
|
|
class TestDrawHistogram:
|
|
"""Tests for draw_histogram function."""
|
|
|
|
@pytest.fixture
|
|
def sample_df(self):
|
|
"""Create a sample DataFrame for testing."""
|
|
return pd.DataFrame({
|
|
"metric": ["read_clat", "read_clat", "write_clat"],
|
|
"latency_ns": [1000, 2000, 1500],
|
|
"count": [1, 2, 1],
|
|
})
|
|
|
|
def test_draws_histogram(self, sample_df, tmp_path):
|
|
"""Test that histogram is drawn without errors."""
|
|
fig, ax = plt.subplots()
|
|
draw_histogram(ax, sample_df, ["read_clat"])
|
|
assert ax.get_title() == "Completion latency distribution"
|
|
plt.close(fig)
|
|
|
|
def test_multiple_metrics(self, sample_df, tmp_path):
|
|
"""Test drawing multiple metrics."""
|
|
fig, ax = plt.subplots()
|
|
draw_histogram(ax, sample_df, ["read_clat", "write_clat"])
|
|
assert len(ax.collections) > 0
|
|
plt.close(fig)
|
|
|
|
def test_log_scale(self, sample_df, tmp_path):
|
|
"""Test logarithmic scale."""
|
|
fig, ax = plt.subplots()
|
|
draw_histogram(ax, sample_df, ["read_clat"], log_x=True, log_y=True)
|
|
assert ax.get_xscale() == "log"
|
|
assert ax.get_yscale() == "log"
|
|
plt.close(fig)
|
|
|
|
|
|
class TestDrawCdf:
|
|
"""Tests for draw_cdf function."""
|
|
|
|
@pytest.fixture
|
|
def sample_df(self):
|
|
"""Create a sample DataFrame for testing."""
|
|
return pd.DataFrame({
|
|
"metric": ["read_clat", "read_clat"],
|
|
"latency_ns": [1000, 2000],
|
|
"count": [1, 3],
|
|
})
|
|
|
|
def test_draws_cdf(self, sample_df, tmp_path):
|
|
"""Test that CDF is drawn without errors."""
|
|
fig, ax = plt.subplots()
|
|
draw_cdf(ax, sample_df, ["read_clat"])
|
|
assert ax.get_title() == "Cumulative latency distribution"
|
|
assert ax.get_ylim() == (0, 100)
|
|
plt.close(fig)
|
|
|
|
def test_log_scale(self, sample_df, tmp_path):
|
|
"""Test logarithmic X scale."""
|
|
fig, ax = plt.subplots()
|
|
draw_cdf(ax, sample_df, ["read_clat"], log_x=True)
|
|
assert ax.get_xscale() == "log"
|
|
plt.close(fig)
|
|
|
|
|
|
class TestGeneratePlot:
|
|
"""Tests for generate_plot function."""
|
|
|
|
@pytest.fixture
|
|
def sample_df(self):
|
|
"""Create a sample DataFrame for testing."""
|
|
return pd.DataFrame({
|
|
"metric": ["read_clat", "read_clat"],
|
|
"latency_ns": [1000, 2000],
|
|
"count": [1, 3],
|
|
})
|
|
|
|
def test_generates_histogram(self, sample_df, tmp_path):
|
|
"""Test generating histogram plot."""
|
|
output = tmp_path / "test.png"
|
|
result = generate_plot(
|
|
df=sample_df,
|
|
metrics=["read_clat"],
|
|
output_path=output,
|
|
)
|
|
assert result == output
|
|
assert output.exists()
|
|
|
|
def test_generates_cdf_only(self, sample_df, tmp_path):
|
|
"""Test generating CDF-only plot."""
|
|
output = tmp_path / "test_cdf.png"
|
|
result = generate_plot(
|
|
df=sample_df,
|
|
metrics=["read_clat"],
|
|
output_path=output,
|
|
cdf_only=True,
|
|
)
|
|
assert result == output
|
|
assert output.exists()
|
|
|
|
def test_generates_histogram_with_cdf(self, sample_df, tmp_path):
|
|
"""Test generating histogram with CDF below."""
|
|
output = tmp_path / "test_both.png"
|
|
result = generate_plot(
|
|
df=sample_df,
|
|
metrics=["read_clat"],
|
|
output_path=output,
|
|
show_cdf=True,
|
|
)
|
|
assert result == output
|
|
assert output.exists()
|
|
|
|
def test_custom_dpi(self, sample_df, tmp_path):
|
|
"""Test custom DPI setting."""
|
|
output = tmp_path / "test_dpi.png"
|
|
result = generate_plot(
|
|
df=sample_df,
|
|
metrics=["read_clat"],
|
|
output_path=output,
|
|
dpi=300,
|
|
)
|
|
assert result == output
|
|
assert output.exists() |