Skip to content

Commit

Permalink
test: add tests for DrilldownPie
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 9, 2024
1 parent 81d1ea7 commit 3e4747b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/endstone_bstats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from endstone_bstats._base import MetricsBase
from endstone_bstats._charts.advanced_pie import AdvancedPie
from endstone_bstats._charts.custom_chart import CustomChart
from endstone_bstats._charts.drilldown_pie import DrilldownPie
from endstone_bstats._charts.simple_pie import SimplePie
from endstone_bstats._config import MetricsConfig
from endstone_bstats._errors import ChartDataError
Expand All @@ -13,5 +14,6 @@
"CustomChart",
"SimplePie",
"AdvancedPie",
"DrilldownPie",
"ChartDataError",
]
74 changes: 74 additions & 0 deletions tests/test_drilldown_pie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest
from unittest.mock import Mock
from typing import Dict, Optional
from endstone_bstats._charts.drilldown_pie import DrilldownPie


def test_get_chart_data_none():
"""Test that get_chart_data returns None when the callable returns None."""
callable_mock = Mock(return_value=None)
drilldown_pie = DrilldownPie("chart_id", callable_mock)

result = drilldown_pie.get_chart_data()
assert result is None


def test_get_chart_data_empty():
"""Test that get_chart_data returns None when the callable returns an empty dictionary."""
callable_mock = Mock(return_value={})
drilldown_pie = DrilldownPie("chart_id", callable_mock)

result = drilldown_pie.get_chart_data()
assert result is None


def test_get_chart_data_valid():
"""Test that get_chart_data processes and returns valid data correctly."""
data = {
"Category A": {"Sub A1": 10, "Sub A2": 20},
"Category B": {"Sub B1": 15, "Sub B2": 25},
"Category C": {}
}
callable_mock = Mock(return_value=data)
drilldown_pie = DrilldownPie("chart_id", callable_mock)

result = drilldown_pie.get_chart_data()
expected_result = {
"values": {
"Category A": {"Sub A1": 10, "Sub A2": 20},
"Category B": {"Sub B1": 15, "Sub B2": 25}
}
}
assert result == expected_result


def test_get_chart_data_some_empty_values():
"""Test that get_chart_data omits empty sub-dictionaries."""
data = {
"Category A": {"Sub A1": 10, "Sub A2": 20},
"Category B": {},
}
callable_mock = Mock(return_value=data)
drilldown_pie = DrilldownPie("chart_id", callable_mock)

result = drilldown_pie.get_chart_data()
expected_result = {
"values": {
"Category A": {"Sub A1": 10, "Sub A2": 20},
}
}
assert result == expected_result


def test_get_chart_data_all_empty_values():
"""Test that get_chart_data returns None when all sub-dictionaries in the callable result are empty."""
data = {
"Category A": {},
"Category B": {},
"Category C": {}
}
callable_mock = Mock(return_value=data)
drilldown_pie = DrilldownPie("chart_id", callable_mock)

result = drilldown_pie.get_chart_data()
assert result is None

0 comments on commit 3e4747b

Please sign in to comment.