import pandas as pd from isora_hf.panel import COMPOSITE_COLUMNS, G16, G18, G20, PANEL_COLUMNS, build_panel def _obs(rows): frame = pd.DataFrame( rows, columns=[ "jurisdiction_code", "fiscal_year", "questionnaire_generation", "indicator_code", "value_numeric", ], ) frame["jurisdiction_name"] = "X" frame["survey_round"] = frame["fiscal_year"].map( {2015: "ISORA 2016", 2017: "ISORA 2018", 2023: "ISORA 2024"} ) frame["indicator_label"] = "lbl " + frame["indicator_code"] frame["value_status"] = "value" frame["monetary_unit"] = None frame["value_local_currency_units"] = frame["value_numeric"] * 1000 return frame def _jur(): return pd.DataFrame( { "jurisdiction_code": ["AAA"], "imf_region": ["Europe"], "world_bank_region_current": ["Europe & Central Asia"], "world_bank_income_group_current": ["High income"], "member_oecd": [True], "member_eu": [False], "member_iota": [True], "member_ciat": [False], "member_adb": [False], } ) def test_column_definitions_are_unique_and_reference_generations(): names = [c.name for c in PANEL_COLUMNS] assert len(names) == len(set(names)) for c in PANEL_COLUMNS: assert set(c.codes) <= {G16, G18, G20} assert c.codes or c.name in COMPOSITE_COLUMNS def test_panel_uses_generation_specific_codes_and_drops_implausible_zeros(): obs = _obs( [ ("AAA", 2015, G16, "10790", 0.0), # 2016 revenue-to-GDP published as 0 -> missing ("AAA", 2017, G18, "337_001", 17.5), ( "AAA", 2017, G18, "337_015", 12.0, ), # 2018 meaning differs -> must NOT feed capex_to_opex_pct ("AAA", 2023, G20, "337_015", 3.0), ("AAA", 2023, G20, "337_031", 10.0), ("AAA", 2023, G20, "337_032", 20.0), ("AAA", 2023, G20, "80040_3", 5.0), ] ) income = pd.Series( ["High income", "High income", "High income"], index=pd.MultiIndex.from_tuples( [("AAA", 2015), ("AAA", 2017), ("AAA", 2023)], names=["jurisdiction_code", "fiscal_year"], ), ) panel, dictionary = build_panel(obs, _jur(), income) p = panel.set_index("fiscal_year") assert pd.isna(p.at[2015, "revenue_to_gdp_pct"]) assert p.at[2017, "revenue_to_gdp_pct"] == 17.5 assert pd.isna(p.at[2017, "capex_to_opex_pct"]) and p.at[2023, "capex_to_opex_pct"] == 3.0 assert p.at[2023, "staff_under_35_pct"] == 30.0 assert p.at[2023, "net_revenue_lcu"] == 5000.0 # money columns come from harmonised units assert p.at[2023, "income_group_wb_classification_fy"] == "FY25" assert dictionary.loc[ dictionary["column"] == "revenue_to_gdp_pct", "zeros_treated_as_missing" ].all() assert set( dictionary.loc[dictionary["column"] == "capex_to_opex_pct", "questionnaire_generation"] ) == {G20}