|
| 1 | +import pandas as pd |
| 2 | +from sqlmodel import Session |
| 3 | + |
| 4 | +from policyengine_us_data.db.create_database_tables import ( |
| 5 | + Stratum, |
| 6 | + StratumConstraint, |
| 7 | + Target, |
| 8 | + create_database, |
| 9 | +) |
| 10 | +from policyengine_us_data.db.etl_national_targets import ( |
| 11 | + TAX_EXPENDITURE_REFORM_ID, |
| 12 | + load_national_targets, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def _make_stratum(session, parent_id=None, notes=None, constraints=None): |
| 17 | + stratum = Stratum(parent_stratum_id=parent_id, notes=notes) |
| 18 | + stratum.constraints_rel = constraints or [] |
| 19 | + session.add(stratum) |
| 20 | + session.commit() |
| 21 | + session.refresh(stratum) |
| 22 | + return stratum |
| 23 | + |
| 24 | + |
| 25 | +def test_load_national_targets_deactivates_stale_baseline_rows(tmp_path, monkeypatch): |
| 26 | + calibration_dir = tmp_path / "calibration" |
| 27 | + calibration_dir.mkdir() |
| 28 | + db_uri = f"sqlite:///{calibration_dir / 'policy_data.db'}" |
| 29 | + engine = create_database(db_uri) |
| 30 | + |
| 31 | + with Session(engine) as session: |
| 32 | + national = _make_stratum(session, notes="United States") |
| 33 | + filer = _make_stratum( |
| 34 | + session, |
| 35 | + parent_id=national.stratum_id, |
| 36 | + notes="United States - Tax Filers", |
| 37 | + constraints=[ |
| 38 | + StratumConstraint( |
| 39 | + constraint_variable="tax_unit_is_filer", |
| 40 | + operation="==", |
| 41 | + value="1", |
| 42 | + ) |
| 43 | + ], |
| 44 | + ) |
| 45 | + itemizer = _make_stratum( |
| 46 | + session, |
| 47 | + parent_id=national.stratum_id, |
| 48 | + notes="United States - Itemizing Tax Filers", |
| 49 | + constraints=[ |
| 50 | + StratumConstraint( |
| 51 | + constraint_variable="tax_unit_is_filer", |
| 52 | + operation="==", |
| 53 | + value="1", |
| 54 | + ), |
| 55 | + StratumConstraint( |
| 56 | + constraint_variable="tax_unit_itemizes", |
| 57 | + operation="==", |
| 58 | + value="1", |
| 59 | + ), |
| 60 | + ], |
| 61 | + ) |
| 62 | + |
| 63 | + session.add( |
| 64 | + Target( |
| 65 | + stratum_id=filer.stratum_id, |
| 66 | + variable="qualified_business_income_deduction", |
| 67 | + period=2024, |
| 68 | + value=63.1e9, |
| 69 | + active=True, |
| 70 | + reform_id=0, |
| 71 | + ) |
| 72 | + ) |
| 73 | + session.add( |
| 74 | + Target( |
| 75 | + stratum_id=itemizer.stratum_id, |
| 76 | + variable="salt_deduction", |
| 77 | + period=2024, |
| 78 | + value=21.247e9, |
| 79 | + active=True, |
| 80 | + reform_id=0, |
| 81 | + ) |
| 82 | + ) |
| 83 | + session.commit() |
| 84 | + |
| 85 | + monkeypatch.setattr( |
| 86 | + "policyengine_us_data.db.etl_national_targets.STORAGE_FOLDER", |
| 87 | + tmp_path, |
| 88 | + ) |
| 89 | + |
| 90 | + tax_expenditure_df = pd.DataFrame( |
| 91 | + [ |
| 92 | + { |
| 93 | + "variable": "salt_deduction", |
| 94 | + "value": 21.247e9, |
| 95 | + "source": "Joint Committee on Taxation", |
| 96 | + "notes": "SALT deduction tax expenditure", |
| 97 | + "year": 2024, |
| 98 | + }, |
| 99 | + { |
| 100 | + "variable": "qualified_business_income_deduction", |
| 101 | + "value": 63.1e9, |
| 102 | + "source": "Joint Committee on Taxation", |
| 103 | + "notes": "QBI deduction tax expenditure", |
| 104 | + "year": 2024, |
| 105 | + }, |
| 106 | + ] |
| 107 | + ) |
| 108 | + |
| 109 | + load_national_targets( |
| 110 | + direct_targets_df=pd.DataFrame(), |
| 111 | + tax_filer_df=pd.DataFrame(), |
| 112 | + tax_expenditure_df=tax_expenditure_df, |
| 113 | + conditional_targets=[], |
| 114 | + ) |
| 115 | + load_national_targets( |
| 116 | + direct_targets_df=pd.DataFrame(), |
| 117 | + tax_filer_df=pd.DataFrame(), |
| 118 | + tax_expenditure_df=tax_expenditure_df, |
| 119 | + conditional_targets=[], |
| 120 | + ) |
| 121 | + |
| 122 | + with Session(engine) as session: |
| 123 | + stale_rows = session.query(Target).filter(Target.reform_id == 0).all() |
| 124 | + assert stale_rows |
| 125 | + assert all(not target.active for target in stale_rows) |
| 126 | + |
| 127 | + reform_rows = ( |
| 128 | + session.query(Target) |
| 129 | + .filter(Target.reform_id == TAX_EXPENDITURE_REFORM_ID) |
| 130 | + .all() |
| 131 | + ) |
| 132 | + assert len(reform_rows) == 2 |
| 133 | + assert all(target.active for target in reform_rows) |
| 134 | + assert {target.variable for target in reform_rows} == { |
| 135 | + "salt_deduction", |
| 136 | + "qualified_business_income_deduction", |
| 137 | + } |
| 138 | + assert all( |
| 139 | + "Modeled as repeal-based income tax expenditure target" |
| 140 | + in (target.notes or "") |
| 141 | + for target in reform_rows |
| 142 | + ) |
0 commit comments