|
|
"""Tests for mathematical utility functions.""" |
|
|
|
|
|
import pytest |
|
|
from src.utils.math import calculate_mean, calculate_median |
|
|
|
|
|
def test_calculate_mean(): |
|
|
"""Test the calculate_mean function with various inputs.""" |
|
|
|
|
|
assert calculate_mean([1, 2, 3, 4, 5]) == 3.0 |
|
|
|
|
|
|
|
|
assert calculate_mean([1.5, 2.5, 3.5]) == 2.5 |
|
|
|
|
|
|
|
|
assert calculate_mean([1, 2.5, 3]) == 2.1666666666666665 |
|
|
|
|
|
def test_calculate_mean_errors(): |
|
|
"""Test error handling in calculate_mean function.""" |
|
|
|
|
|
with pytest.raises(ValueError) as exc: |
|
|
calculate_mean([]) |
|
|
assert str(exc.value) == "Cannot calculate mean of empty list" |
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as exc: |
|
|
calculate_mean([1, "2", 3]) |
|
|
assert str(exc.value) == "All elements must be numbers" |
|
|
|
|
|
def test_calculate_median(): |
|
|
"""Test the calculate_median function with various inputs.""" |
|
|
|
|
|
assert calculate_median([1, 2, 3, 4, 5]) == 3.0 |
|
|
|
|
|
|
|
|
assert calculate_median([1, 2, 3, 4]) == 2.5 |
|
|
|
|
|
|
|
|
assert calculate_median([5, 2, 1, 4, 3]) == 3.0 |
|
|
|
|
|
|
|
|
assert calculate_median([1.5, 2.5, 3.5]) == 2.5 |
|
|
|
|
|
|
|
|
assert calculate_median([1, 2.5, 3]) == 2.5 |
|
|
|
|
|
def test_calculate_median_errors(): |
|
|
"""Test error handling in calculate_median function.""" |
|
|
|
|
|
with pytest.raises(ValueError) as exc: |
|
|
calculate_median([]) |
|
|
assert str(exc.value) == "Cannot calculate median of empty list" |
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as exc: |
|
|
calculate_median([1, "2", 3]) |
|
|
assert str(exc.value) == "All elements must be numbers" |