File size: 2,479 Bytes
cbd273f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pandas as pd

from isora_hf.crosswalk import build_indicator_history, label_similarity, normalize_label


def test_normalize_label_ignores_case_punctuation_and_spacing():
    assert normalize_label("Annual report - prepared") == normalize_label(
        "annual  report prepared."
    )


def test_similarity_is_one_for_equivalent_labels_and_none_when_missing():
    assert label_similarity("Annual report - prepared", "Annual report prepared") == 1.0
    assert label_similarity(None, "x") is None


def _indicators():
    return pd.DataFrame(
        [
            {
                "questionnaire_generation": "ISORA 2016",
                "indicator_code": "A",
                "label": "Staff total",
                "indicator_type": "count",
            },
            {
                "questionnaire_generation": "ISORA 2018",
                "indicator_code": "A",
                "label": "Staff total",
                "indicator_type": "count",
            },
            {
                "questionnaire_generation": "ISORA 2020+",
                "indicator_code": "A",
                "label": "Total staff (FTE)",
                "indicator_type": "count",
            },
            {
                "questionnaire_generation": "ISORA 2016",
                "indicator_code": "B",
                "label": "Old only",
                "indicator_type": "binary",
            },
        ]
    )


def _observations():
    return pd.DataFrame(
        [
            {"indicator_code": "A", "fiscal_year": 2014, "value_status": "value"},
            {"indicator_code": "A", "fiscal_year": 2024, "value_status": "not_available"},
            {"indicator_code": "B", "fiscal_year": 2015, "value_status": "value"},
        ]
    )


def test_history_flags_label_changes_and_single_generation_codes():
    hist = build_indicator_history(_indicators(), _observations()).set_index("indicator_code")
    a = hist.loc["A"]
    assert a["in_2016"] and a["in_2018"] and a["in_2020plus"]
    assert a["label_changed_2016_to_2018"] is False or a["label_changed_2016_to_2018"] == False
    assert a["label_changed_2018_to_2020plus"] == True
    assert a["comparability_flag"] == "label_changed"
    assert a["fiscal_years_with_data"] == [2014, 2024]
    assert a["n_observations"] == 2 and a["n_observations_with_value"] == 1
    b = hist.loc["B"]
    assert b["comparability_flag"] == "single_generation"
    assert pd.isna(b["label_2018"]) and b["n_generations"] == 1