Skip to content

Commit f72ba2d

Browse files
committed
add tests for cockroft-gault and mdrd
1 parent bf0d3d5 commit f72ba2d

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

tests/test_cg.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import pytest
2+
3+
from medimetry.constants import Gender
4+
from medimetry.renal import cockcroft_gault
5+
6+
7+
def test_cockcroft_gault_male_normal():
8+
"""Test Cockcroft-Gault formula for normal male patient."""
9+
# 70-year-old male, 70kg, creatinine 1.0 mg/dl
10+
result = cockcroft_gault(age=70, weight=70, creatinine=1.0, gender=Gender.MALE)
11+
expected = round(((140 - 70) * 70) / (72 * 1.0)) # 68 mL/min
12+
assert result == expected
13+
assert result == 68
14+
15+
16+
def test_cockcroft_gault_female_normal():
17+
"""Test Cockcroft-Gault formula for normal female patient."""
18+
# 70-year-old female, 60kg, creatinine 1.0 mg/dl
19+
result = cockcroft_gault(age=70, weight=60, creatinine=1.0, gender=Gender.FEMALE)
20+
expected = round(((140 - 70) * 60) / (72 * 1.0) * 0.85) # 49 mL/min
21+
assert result == expected
22+
assert result == 50
23+
24+
25+
def test_cockcroft_gault_young_patient():
26+
"""Test Cockcroft-Gault formula for young patient."""
27+
# 30-year-old male, 80kg, creatinine 0.8 mg/dl
28+
result = cockcroft_gault(age=30, weight=80, creatinine=0.8, gender=Gender.MALE)
29+
expected = round(((140 - 30) * 80) / (72 * 0.8)) # 153 mL/min
30+
assert result == expected
31+
assert result == 153
32+
33+
34+
def test_cockcroft_gault_elderly_patient():
35+
"""Test Cockcroft-Gault formula for elderly patient."""
36+
# 85-year-old female, 55kg, creatinine 1.5 mg/dl
37+
result = cockcroft_gault(age=85, weight=55, creatinine=1.5, gender=Gender.FEMALE)
38+
expected = round(((140 - 85) * 55) / (72 * 1.5) * 0.85) # 25 mL/min
39+
assert result == expected
40+
assert result == 24
41+
42+
43+
def test_cockcroft_gault_high_creatinine():
44+
"""Test Cockcroft-Gault formula with high creatinine."""
45+
# 60-year-old male, 75kg, creatinine 3.0 mg/dl
46+
result = cockcroft_gault(age=60, weight=75, creatinine=3.0, gender=Gender.MALE)
47+
expected = round(((140 - 60) * 75) / (72 * 3.0)) # 28 mL/min
48+
assert result == expected
49+
assert result == 28
50+
51+
52+
def test_cockcroft_gault_zero_weight():
53+
"""Test Cockcroft-Gault formula with zero weight."""
54+
with pytest.raises(AssertionError, match="Weight must be positive"):
55+
cockcroft_gault(age=50, weight=0, creatinine=1.0, gender=Gender.MALE)
56+
57+
58+
def test_cockcroft_gault_negative_weight():
59+
"""Test Cockcroft-Gault formula with negative weight."""
60+
with pytest.raises(AssertionError, match="Weight must be positive"):
61+
cockcroft_gault(age=50, weight=-10, creatinine=1.0, gender=Gender.MALE)
62+
63+
64+
def test_cockcroft_gault_excessive_weight():
65+
"""Test Cockcroft-Gault formula with excessive weight."""
66+
with pytest.raises(AssertionError, match="Weight must be less than 400 kg"):
67+
cockcroft_gault(age=50, weight=450, creatinine=1.0, gender=Gender.MALE)
68+
69+
70+
def test_cockcroft_gault_zero_age():
71+
"""Test Cockcroft-Gault formula with zero age."""
72+
with pytest.raises(AssertionError, match="Age must be positive"):
73+
cockcroft_gault(age=0, weight=70, creatinine=1.0, gender=Gender.MALE)
74+
75+
76+
def test_cockcroft_gault_negative_age():
77+
"""Test Cockcroft-Gault formula with negative age."""
78+
with pytest.raises(AssertionError, match="Age must be positive"):
79+
cockcroft_gault(age=-5, weight=70, creatinine=1.0, gender=Gender.MALE)
80+
81+
82+
def test_cockcroft_gault_negative_creatinine():
83+
"""Test Cockcroft-Gault formula with negative creatinine."""
84+
with pytest.raises(AssertionError, match="Creatinine must not be negative"):
85+
cockcroft_gault(age=50, weight=70, creatinine=-0.5, gender=Gender.MALE)
86+
87+
88+
def test_cockcroft_gault_zero_creatinine():
89+
"""Test Cockcroft-Gault formula with zero creatinine."""
90+
# Zero creatinine should be allowed (though clinically unrealistic)
91+
with pytest.raises(ValueError, match="Creatinine must be non-zero"):
92+
cockcroft_gault(age=50, weight=70, creatinine=0.0, gender=Gender.MALE)
93+
# This would result in division by zero, so the function should handle this
94+
95+
96+
def test_cockcroft_gault_invalid_gender():
97+
"""Test Cockcroft-Gault formula with invalid gender."""
98+
with pytest.raises(AssertionError, match="gender must be a Gender instance"):
99+
cockcroft_gault(age=50, weight=70, creatinine=1.0, gender="invalid")
100+
with pytest.raises(AssertionError, match="gender must be a Gender instance"):
101+
cockcroft_gault(age=50, weight=70, creatinine=1.0, gender=2)
102+
103+
104+
def test_cockcroft_gault_realistic_scenarios():
105+
"""Test Cockcroft-Gault formula with realistic clinical scenarios."""
106+
# Healthy young adult male
107+
result = cockcroft_gault(age=25, weight=75, creatinine=0.9, gender=Gender.MALE)
108+
assert result > 100 # Should have good kidney function
109+
110+
# Elderly female with mild kidney impairment
111+
result = cockcroft_gault(age=80, weight=60, creatinine=1.3, gender=Gender.FEMALE)
112+
assert 30 < result < 60 # Moderate kidney impairment
113+
114+
# Middle-aged male with normal function
115+
result = cockcroft_gault(age=45, weight=80, creatinine=1.1, gender=Gender.MALE)
116+
assert 60 < result < 120 # Normal to mildly reduced
117+
118+
119+
def test_cockcroft_gault_gender_difference():
120+
"""Test that female patients have lower clearance than male patients."""
121+
male_result = cockcroft_gault(age=60, weight=70, creatinine=1.0, gender=Gender.MALE)
122+
female_result = cockcroft_gault(age=60, weight=70, creatinine=1.0, gender=Gender.FEMALE)
123+
124+
# Female result should be 85% of male result
125+
expected_female = round(male_result * 0.85)
126+
assert female_result < male_result
127+
assert abs(female_result - expected_female) <= 1 # Allow for rounding differences
128+
129+
130+
def test_cockcroft_gault_return_type():
131+
"""Test that Cockcroft-Gault returns an integer."""
132+
result = cockcroft_gault(age=50, weight=70, creatinine=1.0, gender=Gender.MALE)
133+
assert isinstance(result, int)

tests/test_mdrd.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import pytest
2+
3+
from medimetry import EthnicalRace
4+
from medimetry import Gender
5+
from medimetry.renal import mdrd
6+
7+
8+
def test_mdrd_male_normal_creatinine():
9+
"""Test MDRD formula for male patient with normal creatinine levels."""
10+
# 50-year-old male, creatinine 1.0 mg/dl, other race
11+
result = mdrd(creatinine=1.0, age=50, gender=Gender.MALE, race=EthnicalRace.OTHER)
12+
expected = 175 * (1.0**-1.154) * (50**-0.203) # ~79.7 mL/min/1.73m²
13+
assert abs(result - expected) < 0.1
14+
assert 75 < result < 85 # Should be in normal range
15+
16+
17+
def test_mdrd_female_correction_factor():
18+
"""Test MDRD formula applies 0.742 correction factor for female patients."""
19+
# Test with same parameters but different gender to verify correction factor
20+
male_result = mdrd(creatinine=1.0, age=50, gender=Gender.MALE, race=EthnicalRace.OTHER)
21+
female_result = mdrd(creatinine=1.0, age=50, gender=Gender.FEMALE, race=EthnicalRace.OTHER)
22+
23+
# Female result should be male result * 0.742
24+
expected_female = male_result * 0.742
25+
assert abs(female_result - expected_female) < 0.001 # Allow for floating point precision
26+
assert female_result < male_result # Female result should be lower
27+
28+
29+
def test_mdrd_african_american_correction():
30+
"""Test MDRD formula with African American race correction factor."""
31+
# 50-year-old African American male, creatinine 1.2 mg/dl
32+
result = mdrd(creatinine=1.2, age=50, gender=Gender.MALE, race=EthnicalRace.AFRICAN_AMERICAN)
33+
34+
# Calculate expected value: 175 * (1.2)^-1.154 * (50)^-0.203 * 1.212
35+
base_calculation = 175 * (1.2**-1.154) * (50**-0.203)
36+
expected = base_calculation * 1.212
37+
38+
assert abs(result - expected) < 0.01 # Allow small floating point differences
39+
assert result > base_calculation # African American factor should increase result
40+
41+
42+
def test_mdrd_african_american_female():
43+
"""Test MDRD formula for African American female with both correction factors."""
44+
# 50-year-old African American female, creatinine 1.2 mg/dl
45+
result = mdrd(creatinine=1.2, age=50, gender=Gender.FEMALE, race=EthnicalRace.AFRICAN_AMERICAN)
46+
# Base: 175 * (1.2)^-1.154 * (50)^-0.203 = 175 * 0.8676 * 0.4472 = 67.88
47+
# Female factor: 67.88 * 0.742 = 50.37
48+
# African American factor: 50.37 * 1.212 = 61.05
49+
expected = 57.6
50+
assert abs(result - expected) < 0.1
51+
52+
53+
def test_mdrd_zero_creatinine():
54+
"""Test MDRD formula with zero creatinine."""
55+
with pytest.raises(AssertionError, match="Creatinine must be positive"):
56+
mdrd(creatinine=0.0, age=50, gender=Gender.MALE)
57+
58+
59+
def test_mdrd_negative_creatinine():
60+
"""Test MDRD formula with negative creatinine."""
61+
with pytest.raises(AssertionError, match="Creatinine must be positive"):
62+
mdrd(creatinine=-0.5, age=50, gender=Gender.MALE)
63+
64+
65+
def test_mdrd_zero_age():
66+
"""Test MDRD formula with zero age."""
67+
with pytest.raises(AssertionError, match="Age must be positive"):
68+
mdrd(creatinine=1.0, age=0, gender=Gender.MALE)
69+
70+
71+
def test_mdrd_negative_age():
72+
"""Test MDRD formula with negative age."""
73+
with pytest.raises(AssertionError, match="Age must be positive"):
74+
mdrd(creatinine=1.0, age=-5, gender=Gender.MALE)
75+
76+
77+
def test_mdrd_elderly_over_100():
78+
"""Test MDRD formula for elderly patients over 100 years."""
79+
# 105-year-old male, creatinine 1.2 mg/dl
80+
result = mdrd(creatinine=1.2, age=105, gender=Gender.MALE)
81+
# Manual calculation: 175 * (1.2)^-1.154 * (105)^-0.203
82+
# = 175 * 0.8551 * 0.3147 ≈ 47.1
83+
assert 55.1 < result < 55.2
84+
assert isinstance(result, float)

0 commit comments

Comments
 (0)