Back to Repositories

Testing Ratio Calculation Components in Textualize/rich

A comprehensive test suite for Rich library’s ratio calculation functionality, focusing on ratio reduction and resolution algorithms. The tests verify the accurate distribution of space and handling of ratios with various constraints and edge cases.

Test Coverage Overview

The test suite provides thorough coverage of ratio calculations with both basic and complex scenarios.

  • Ratio reduction with different total values and constraints
  • Resolution of space distribution with fixed sizes and ratios
  • Edge cases including zero ratios and minimum size requirements
  • Boundary testing with various input combinations

Implementation Analysis

The implementation uses pytest’s parametrize feature for comprehensive ratio reduction testing, enabling multiple test cases with a single function definition.

  • Named tuples for structured edge definitions
  • Parametrized tests for ratio_reduce function
  • Direct assertions for ratio_resolve scenarios
  • Type hints for improved code clarity

Technical Details

  • Python’s typing module for type annotations
  • Pytest framework for test organization
  • NamedTuple for data structure definition
  • Parametrized testing for multiple test cases

Best Practices Demonstrated

The test suite exhibits strong testing practices with clear organization and comprehensive coverage.

  • Structured test data using named tuples
  • Comprehensive edge case coverage
  • Clear test case organization
  • Type safety through annotations

textualize/rich

tests/test_ratio.py

            
import pytest
from typing import NamedTuple, Optional

from rich._ratio import ratio_reduce, ratio_resolve


class Edge(NamedTuple):
    size: Optional[int] = None
    ratio: int = 1
    minimum_size: int = 1


@pytest.mark.parametrize(
    "total,ratios,maximums,values,result",
    [
        (20, [2, 4], [20, 20], [5, 5], [-2, -8]),
        (20, [2, 4], [1, 1], [5, 5], [4, 4]),
        (20, [2, 4], [1, 1], [2, 2], [1, 1]),
        (3, [2, 4], [3, 3], [2, 2], [1, 0]),
        (3, [2, 4], [3, 3], [0, 0], [-1, -2]),
        (3, [0, 0], [3, 3], [4, 4], [4, 4]),
    ],
)
def test_ratio_reduce(total, ratios, maximums, values, result):
    assert ratio_reduce(total, ratios, maximums, values) == result


def test_ratio_resolve():
    assert ratio_resolve(100, []) == []
    assert ratio_resolve(100, [Edge(size=100), Edge(ratio=1)]) == [100, 1]
    assert ratio_resolve(100, [Edge(ratio=1)]) == [100]
    assert ratio_resolve(100, [Edge(ratio=1), Edge(ratio=1)]) == [50, 50]
    assert ratio_resolve(100, [Edge(size=20), Edge(ratio=1), Edge(ratio=1)]) == [
        20,
        40,
        40,
    ]
    assert ratio_resolve(100, [Edge(size=40), Edge(ratio=2), Edge(ratio=1)]) == [
        40,
        40,
        20,
    ]
    assert ratio_resolve(
        100, [Edge(size=40), Edge(ratio=2), Edge(ratio=1, minimum_size=25)]
    ) == [40, 35, 25]
    assert ratio_resolve(100, [Edge(ratio=1), Edge(ratio=1), Edge(ratio=1)]) == [
        33,
        33,
        34,
    ]
    assert ratio_resolve(
        50, [Edge(size=30), Edge(ratio=1, minimum_size=10), Edge(size=30)]
    ) == [30, 10, 30]
    assert ratio_resolve(110, [Edge(ratio=1), Edge(ratio=1), Edge(ratio=1)]) == [
        36,
        37,
        37,
    ]