• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vg103 / Global_Healthcare / 13955543958

19 Mar 2025 07:56PM UTC coverage: 96.364% (+2.9%) from 93.485%
13955543958

push

github

web-flow
Merge pull request #74 from vg103/jay-branch-patch

Jay branch patch

143 of 144 new or added lines in 5 files covered. (99.31%)

1 existing line in 1 file now uncovered.

636 of 660 relevant lines covered (96.36%)

1.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.08
/tests/test_data_prep.py
1
"""
2
Unit tests for the data preparation module data_prep.py
3
"""
4
import unittest
2✔
5
from unittest.mock import patch
2✔
6
import pandas as pd
2✔
7
from hcare.data_prep import (
2✔
8
    import_data, pivot_ihme, drop_sex, ag_over_cause, reconcile_locations,
9
    make_medical_data_df, process_healthcare_data
10
)
11

12

13
class TestHealthcare(unittest.TestCase):
2✔
14
    """Test cases for the data preparation functions."""
15

16
    def setUp(self):
2✔
17
        """Set up mock data for testing with matching merge keys."""
18
        # Adjusted IHME mock data so that the merge keys match the WHO data below.
19
        self.mock_ihme_data = pd.DataFrame({
2✔
20
            'measure': ['Deaths', 'Deaths'],
21
            'location': ['Central African Republic', 'Central African Republic'],
22
            'sex': ['Male', 'Female'],
23
            'age': ['All ages', 'All ages'],
24
            'cause': ['Cardiovascular diseases', 'Cardiovascular diseases'],
25
            'metric': ['Rate', 'Rate'],
26
            'year': [2023, 2023],
27
            'val': [456.54, 457.56],
28
            'upper': [460.00, 461.00],
29
            'lower': [450.00, 455.00]
30
        })
31

32
        # Pivoted IHME data (simulate a successful pivot); using matching keys.
33
        self.mock_pivot_ihme_data = pd.DataFrame({
2✔
34
            "location": ["Central African Republic"],
35
            "sex": ["Both"],
36
            "cause": ["Cardiovascular diseases"],
37
            "year": [2023],
38
            "Deaths": [456.54],
39
            "Incidence": [1063.22]
40
        })
41

42
        # WHO data: both keys match the pivoted IHME data ("Central African Republic", 2023)
43
        self.med_df = pd.DataFrame({
2✔
44
            'ParentLocation': ['Africa'],
45
            'Location': ['Central African Republic'],
46
            'Period': [2023],
47
            'Value': [0.74]
48
        })
49
        self.nurse_df = pd.DataFrame({
2✔
50
            'ParentLocation': ['Africa'],
51
            'Location': ['Central African Republic'],
52
            'Period': [2023],
53
            'Value': [10.97]
54
        })
55
        self.pharm_df = pd.DataFrame({
2✔
56
            'ParentLocation': ['Africa'],
57
            'Location': ['Central African Republic'],
58
            'Period': [2023],
59
            'Value': [0.03]
60
        })
61
        self.dent_df = pd.DataFrame({
2✔
62
            'ParentLocation': ['Africa'],
63
            'Location': ['Central African Republic'],
64
            'Period': [2023],
65
            'Value': [0.01]
66
        })
67

68
        self.full_med_df = pd.DataFrame({
2✔
69
            "ParentLocation": ["Africa"],
70
            "Location": ["Central African Republic"],
71
            "Period": [2023],
72
            "Medical Doctors per 10,000": [0.74],
73
            "Nurses and Midwifes per 10,000": [10.97],
74
            "Pharmacists per 10,000": [0.03],
75
            "Dentists per 10,000": [0.01]
76
        })
77

78
    @patch("pandas.read_csv")
2✔
79
    def test_df_exists(self, mock_read_csv):
2✔
80
        """Test that the DataFrame is not empty after importing data."""
81
        mock_read_csv.return_value = pd.DataFrame({'col1': [1, 2, 3]})
2✔
82
        df = import_data(data_path="test_path.csv")
2✔
83
        self.assertGreater(df.shape[0], 0, "data is empty")
2✔
84

85
    @patch("hcare.data_prep.pd.DataFrame.to_csv")
2✔
86
    def test_pivot_ihme(self, mock_to_csv):
2✔
87
        """Test pivot_ihme function"""
88
        pivoted = pivot_ihme(self.mock_ihme_data)
2✔
89
        self.assertIn('Deaths', pivoted.columns)
2✔
90
        self.assertNotIn('measure', pivoted.columns)
2✔
91

92
    def test_drop_sex(self):
2✔
93
        """Test drop_sex function"""
94
        df_no_sex = drop_sex(self.mock_ihme_data)
2✔
95
        # With two rows (Male and Female) dropped, only one row remains if one row had "Both"
96
        # For this test, we can check that 'sex' column is removed.
97
        self.assertNotIn('sex', df_no_sex.columns)
2✔
98

99
    def test_ag_over_cause(self):
2✔
100
        """Test ag_over_cause function"""
101
        df_agg = ag_over_cause(self.mock_ihme_data)
2✔
102
        self.assertIn('location', df_agg.columns)
2✔
103
        self.assertIn('year', df_agg.columns)
2✔
104
        self.assertNotIn('cause', df_agg.columns)
2✔
105

106
    def test_make_medical_data_df(self):
2✔
107
        """Test make_medical_data_df function"""
108
        merged_df = make_medical_data_df(
2✔
109
            self.med_df, self.nurse_df, self.pharm_df, self.dent_df)
110
        self.assertEqual(merged_df.shape[1], 7)
2✔
111
        self.assertIn('Medical Doctors per 10,000', merged_df.columns)
2✔
112

113
    @patch("hcare.data_prep.import_data")
2✔
114
    @patch("hcare.data_prep.make_medical_data_df")
2✔
115
    @patch("hcare.data_prep.pivot_ihme")
2✔
116
    @patch("hcare.data_prep.drop_sex")
2✔
117
    @patch("hcare.data_prep.ag_over_cause")
2✔
118
    def test_process_healthcare_data(self, mock_ag_over_cause, mock_drop_sex,
2✔
119
                                     mock_pivot_ihme, mock_make_medical_data_df, mock_import_data):
120
        """Test that process_healthcare_data runs without errors and produces expected calls."""
121
        # Set up the mocks so that merge keys match:
122
        mock_import_data.return_value = self.mock_ihme_data
2✔
123
        mock_pivot_ihme.return_value = self.mock_pivot_ihme_data
2✔
124
        mock_make_medical_data_df.return_value = self.full_med_df
2✔
125
        mock_drop_sex.return_value = self.mock_pivot_ihme_data
2✔
126
        mock_ag_over_cause.return_value = self.mock_pivot_ihme_data
2✔
127

128
        process_healthcare_data("test_path/")
2✔
129

130
        mock_pivot_ihme.assert_called_once()
2✔
131
        mock_drop_sex.assert_called_once()
2✔
132
        mock_ag_over_cause.assert_called_once()
2✔
133

134

135
if __name__ == '__main__':
2✔
UNCOV
136
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc