|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +from policyengine_us_data.datasets.cps.census_cps import ( |
| 7 | + PERSON_COLUMNS, |
| 8 | + TAX_UNIT_COLUMNS, |
| 9 | + _resolve_person_usecols, |
| 10 | +) |
| 11 | +from policyengine_us_data.datasets.cps.cps import ( |
| 12 | + ESI_POLICYHOLDER_VARIABLE, |
| 13 | + ESI_SOURCE_COLUMNS, |
| 14 | + _EMPLOYER_PAYS_ALL, |
| 15 | + _EMPLOYER_PAYS_SOME, |
| 16 | + _ESI_PLAN_PRIORS_2024, |
| 17 | + _validate_raw_cps_schema, |
| 18 | + impute_employer_sponsored_insurance_premiums, |
| 19 | +) |
| 20 | +from policyengine_us_data.datasets.cps.extended_cps import ( |
| 21 | + CPS_ONLY_IMPUTED_VARIABLES, |
| 22 | +) |
| 23 | +from policyengine_us_data.storage.calibration_targets.pull_hardcoded_targets import ( |
| 24 | + HARD_CODED_TOTALS, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def test_resolve_person_usecols_requests_optional_esi_columns_when_available(): |
| 29 | + available = ( |
| 30 | + PERSON_COLUMNS |
| 31 | + + TAX_UNIT_COLUMNS |
| 32 | + + [ |
| 33 | + "NOW_OWNGRP", |
| 34 | + "NOW_HIPAID", |
| 35 | + "NOW_GRPFTYP", |
| 36 | + ] |
| 37 | + ) |
| 38 | + usecols = _resolve_person_usecols(available, spm_unit_columns=[]) |
| 39 | + |
| 40 | + for column in ["NOW_OWNGRP", "NOW_HIPAID", "NOW_GRPFTYP"]: |
| 41 | + assert column in usecols |
| 42 | + |
| 43 | + |
| 44 | +def test_impute_employer_sponsored_insurance_premiums(): |
| 45 | + person = pd.DataFrame( |
| 46 | + { |
| 47 | + "NOW_OWNGRP": [1, 1, 1, 0, 1], |
| 48 | + "NOW_HIPAID": [1, 2, 2, 1, 2], |
| 49 | + "NOW_GRPFTYP": [2, 2, 1, 2, 1], |
| 50 | + "PHIP_VAL": [0, 1_200, 0, 0, 50_000], |
| 51 | + } |
| 52 | + ) |
| 53 | + |
| 54 | + result = impute_employer_sponsored_insurance_premiums(person) |
| 55 | + |
| 56 | + np.testing.assert_allclose( |
| 57 | + result[0], |
| 58 | + _ESI_PLAN_PRIORS_2024["self_only"]["total_premium"], |
| 59 | + ) |
| 60 | + np.testing.assert_allclose( |
| 61 | + result[1], |
| 62 | + _ESI_PLAN_PRIORS_2024["self_only"]["total_premium"] - 1_200, |
| 63 | + ) |
| 64 | + np.testing.assert_allclose( |
| 65 | + result[2], |
| 66 | + _ESI_PLAN_PRIORS_2024["family"]["total_premium"] |
| 67 | + - _ESI_PLAN_PRIORS_2024["family"]["employee_contribution"], |
| 68 | + ) |
| 69 | + assert result[3] == 0 |
| 70 | + assert result[4] == 0 |
| 71 | + |
| 72 | + |
| 73 | +def test_impute_employer_sponsored_insurance_premiums_tolerates_missing_esi_columns(): |
| 74 | + person = pd.DataFrame({"PHIP_VAL": [1_000, 2_000]}) |
| 75 | + |
| 76 | + result = impute_employer_sponsored_insurance_premiums(person) |
| 77 | + |
| 78 | + np.testing.assert_array_equal(result, np.zeros(2)) |
| 79 | + |
| 80 | + |
| 81 | +def test_imputation_status_codes_remain_stable(): |
| 82 | + assert _EMPLOYER_PAYS_ALL == 1 |
| 83 | + assert _EMPLOYER_PAYS_SOME == 2 |
| 84 | + |
| 85 | + |
| 86 | +def test_extended_cps_imputes_esi_premiums_for_clone_half(): |
| 87 | + assert "employer_sponsored_insurance_premiums" in CPS_ONLY_IMPUTED_VARIABLES |
| 88 | + |
| 89 | + |
| 90 | +def test_hardcoded_targets_include_total_esi_premiums(): |
| 91 | + assert HARD_CODED_TOTALS["employer_sponsored_insurance_premiums"] == 1_002.9e9 |
| 92 | + |
| 93 | + |
| 94 | +def test_target_config_includes_total_esi_premiums(): |
| 95 | + target_config_path = Path(__file__).parents[2] / ( |
| 96 | + "policyengine_us_data/calibration/target_config.yaml" |
| 97 | + ) |
| 98 | + content = target_config_path.read_text() |
| 99 | + |
| 100 | + assert "employer_sponsored_insurance_premiums" in content |
| 101 | + |
| 102 | + |
| 103 | +def test_policyholder_variable_name_remains_stable(): |
| 104 | + assert ( |
| 105 | + ESI_POLICYHOLDER_VARIABLE |
| 106 | + == "reported_owns_employer_sponsored_health_insurance_at_interview" |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def test_raw_cps_schema_requires_esi_source_columns(): |
| 111 | + person = pd.DataFrame( |
| 112 | + { |
| 113 | + "CENSUS_TAX_ID": [1], |
| 114 | + **{column: [1] for column in ESI_SOURCE_COLUMNS}, |
| 115 | + } |
| 116 | + ) |
| 117 | + tax_unit = pd.DataFrame() |
| 118 | + |
| 119 | + _validate_raw_cps_schema(person, tax_unit, "raw") |
| 120 | + |
| 121 | + stale_person = person.drop(columns=["NOW_OWNGRP"]) |
| 122 | + try: |
| 123 | + _validate_raw_cps_schema(stale_person, tax_unit, "raw") |
| 124 | + except ValueError as error: |
| 125 | + assert "NOW_OWNGRP" in str(error) |
| 126 | + else: |
| 127 | + raise AssertionError("Expected missing ESI source column to fail validation") |
0 commit comments