From 3e4747b14aa3154a966698d3e420915350369ad2 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 9 Sep 2024 16:40:02 +0100 Subject: [PATCH] test: add tests for DrilldownPie --- src/endstone_bstats/__init__.py | 2 + tests/test_drilldown_pie.py | 74 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/test_drilldown_pie.py diff --git a/src/endstone_bstats/__init__.py b/src/endstone_bstats/__init__.py index 5d08018..90daf3c 100644 --- a/src/endstone_bstats/__init__.py +++ b/src/endstone_bstats/__init__.py @@ -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 @@ -13,5 +14,6 @@ "CustomChart", "SimplePie", "AdvancedPie", + "DrilldownPie", "ChartDataError", ] diff --git a/tests/test_drilldown_pie.py b/tests/test_drilldown_pie.py new file mode 100644 index 0000000..54ea05f --- /dev/null +++ b/tests/test_drilldown_pie.py @@ -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