test_helpers.py 5.96 KB
Newer Older
David Foucher's avatar
David Foucher committed
1
import datetime
David Foucher's avatar
David Foucher committed
2 3
import json
from pathlib import Path
4 5

import pytest
6 7
from trefle.context import Context
from trefle.exceptions import DataError
8 9 10 11 12 13 14 15 16 17 18 19
from trefle.helpers import (
    count_indent,
    diff_month,
    diff_week,
    flatten,
    fold_name,
    insee_commune_to_departement,
    insee_departement_to_region,
    isfloat,
    calculate_age,
    json_path,
)
20 21 22


def test_flatten():
23
    data = {"a": {"b": "value"}}
24
    flatten(data)
25 26 27 28 29 30 31 32 33 34 35 36
    assert data == {"a.b": "value"}


@pytest.mark.parametrize(
    "start,end,months",
    [
        ((2018, 1, 1), (2018, 5, 31), 5),
        ((2018, 1, 1), (2018, 5, 1), 4),
        ((2018, 1, 31), (2018, 5, 1), 3),
        ((2018, 1, 1), (2019, 5, 31), 17),
    ],
)
37
def test_diff_month(start, end, months):
David Foucher's avatar
David Foucher committed
38
    assert diff_month(datetime.datetime(*start), datetime.datetime(*end)) == months
39 40


41 42 43 44 45 46 47 48 49 50 51 52 53
@pytest.mark.parametrize(
    "start,end,weeks",
    [
        ((2018, 1, 1), (2018, 1, 3), 1),
        ((2018, 1, 1), (2018, 1, 7), 1),
        ((2018, 1, 1), (2018, 1, 8), 2),
        ((2018, 1, 1), (2018, 5, 31), 22),
        ((2018, 1, 1), (2018, 5, 1), 18),
        ((2018, 1, 31), (2018, 5, 1), 13),
        ((2018, 1, 1), (2019, 5, 31), 74),
        ((2018, 1, 1), (2019, 1, 1), 53),
    ],
)
54
def test_diff_week(start, end, weeks):
David Foucher's avatar
David Foucher committed
55
    assert diff_week(datetime.datetime(*start), datetime.datetime(*end)) == weeks
56 57


58
@pytest.mark.parametrize("input,expected", [("", 0), ("    ", 4), ("    x", 4)])
59 60 61 62
def test_count_indent(input, expected):
    assert count_indent(input) == expected


63 64 65 66
@pytest.mark.parametrize(
    "input,expected",
    [(4.5, True), ("4.5", True), (4, True), (None, False), ("", False)],
)
67 68
def test_isfloat(input, expected):
    assert isfloat(input) == expected
Yohan Boniface's avatar
Yohan Boniface committed
69 70


71 72 73 74 75 76 77 78 79 80
@pytest.mark.parametrize(
    "input,expected",
    [
        ("AGEFOS PME", "AGEFOSPME"),
        ("Agefos PME", "AGEFOSPME"),
        ("AgéfosPME", "AGEFOSPME"),
        ("Agéfos-PME", "AGEFOSPME"),
        ("Agéfos_PME", "AGEFOSPME"),
    ],
)
Yohan Boniface's avatar
Yohan Boniface committed
81 82
def test_fold_name(input, expected):
    assert fold_name(input) == expected
83 84


85 86 87 88 89 90 91
@pytest.mark.parametrize(
    "input,expected",
    [
        ("93", "11"),  # idf
        ("20", "94"),  # When consuming postode
        ("971", "01"),  # Guadeloupe
        ("972", "02"),  # Martinique
David Foucher's avatar
David Foucher committed
92
        ("975", None),  # no region for saint-pierre
93 94 95
        ("blah", False),
    ],
)
96
def test_insee_departement_to_region(input, expected):
97
    context = Context({"departement": input})
98
    if expected:
99 100
        insee_departement_to_region(context, "departement", "region")
        assert context["region"] == expected
101
    else:
David Foucher's avatar
David Foucher committed
102
        if expected is None:
103
            insee_departement_to_region(context, "departement", "region")
David Foucher's avatar
David Foucher committed
104 105 106 107
            assert "region" not in context
        else:
            with pytest.raises(DataError):
                insee_departement_to_region(context, "departement", "region")
108 109


110
def test_insee_departement_to_region_with_alias(patch_schema):
111 112 113 114 115 116 117 118 119
    patch_schema(
        {
            "departement": {"type": "string", "alias": ["departement_habitation"]},
            "region": {"type": "string"},
        }
    )
    context = Context({"departement_habitation": "35"})
    insee_departement_to_region(context, "departement", "region")
    assert context["region"] == "53"
120 121 122


def test_insee_commune_to_departement_with_alias(patch_schema):
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    patch_schema(
        {
            "commune": {"type": "string", "alias": ["commune_habitation"]},
            "departement": {"type": "string"},
        }
    )
    context = Context({"commune_habitation": "35001"})
    insee_commune_to_departement(context, "commune", "departement")
    assert context["departement"] == "35"


@pytest.mark.parametrize(
    "input,expected",
    [
        ("93031", "93"),  # idf
        ("2A000", "2A"),
        ("97131", "971"),  # Guadeloupe
        ("97631", "976"),  # Mayotte
        ("99999", False),
        ("invalide", False),
    ],
)
145
def test_insee_commune_to_departement(input, expected):
146
    context = Context({"commune": input})
147
    if expected:
148 149
        insee_commune_to_departement(context, "commune", "beneficiaire.departement")
        assert context["beneficiaire.departement"] == expected
150
    else:
151
        with pytest.raises(DataError):
152 153 154 155 156 157
            insee_commune_to_departement(context, "commune", "beneficiaire.departement")


@pytest.mark.parametrize(
    "input,expected",
    [
David Foucher's avatar
David Foucher committed
158
        ((1999, 5, 20), 20),
159
        ((1999, 10, 8), 20),
160 161
        ((1998, 1, 1), 21),
        ((1998, 12, 31), 20),
162 163
    ],
)
David Foucher's avatar
David Foucher committed
164 165 166 167 168
def test_calculate_age(input, expected, monkeypatch):
    class mydate:
        @classmethod
        def today(cls):
            return datetime.datetime(2018, 10, 8)
169

170
    #TO FIX patch does not work
171
    monkeypatch.setattr(datetime, "date", mydate)
David Foucher's avatar
David Foucher committed
172
    assert calculate_age(datetime.datetime(*input)) == expected
David Foucher's avatar
David Foucher committed
173 174


175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
@pytest.mark.parametrize(
    "input,expected",
    [
        ("intitule", "Titre professionnel plaquiste"),
        ("sessions.0.localisation.formation.commune-insee", "37266"),
        ("sessions.0.financeurs.code", ["2"]),
        ("sessions.0.missing.and.missing", None),
        ("sessions.0.eligibilite-cpf.region-insee", ["24"]),
        ("sessions.0.eligibilite-cpf.type=SALARIE.region-insee", []),
        ("sessions.0.eligibilite-cpf.type=DE|TOUTPUBLIC.region-insee", ["24"]),
        ("sessions.missing", []),
        (
            "sessions.0.eligibilite-cpf.type=SALARIE.branches.[]",
            [
                "23.61Z",
                "43.99E",
                "01.62Z",
                "96.04Z",
                "96.09Z",
                "78.20Z",
                "23.61Z",
                "43.99E",
                "01.62Z",
                "05.20Z",
                "96.09Z",
                "78.20Z",
            ],
        ),
        ("modalites-enseignement=2", False),
        ("validation=5", True),
        ("certification.[]", None),
        ("certification.missing", None),
    ],
)
David Foucher's avatar
David Foucher committed
209
def test_json_path(input, expected):
210
    path = Path(__file__).parent / "data/formation.json"
David Foucher's avatar
David Foucher committed
211 212
    data = json.loads(path.read_text())
    assert json_path(input, data) == expected