Skip to content

Commit 2063613

Browse files
committed
renommage/déplacement row to data
1 parent c47fcea commit 2063613

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from enrich.config import SUGGEST_PREFIX
2+
3+
4+
def dbt_model_row_to_suggest_data(row: dict) -> dict:
5+
"""Construct the pydantic model_params.data dict from a
6+
dbt model's row based on fields prefixed with SUGGEST_PREFIX"""
7+
pre = SUGGEST_PREFIX
8+
keys_ok = [k for k in row.keys() if k.startswith(f"{pre}_")]
9+
keys_ok.remove(f"{pre}_cohort")
10+
11+
# Validation
12+
keys_fail = [
13+
k
14+
for k in row.keys()
15+
if pre in k and k not in keys_ok and not k.startswith(f"{pre}_")
16+
]
17+
if keys_fail:
18+
msg = f"Colonnes invalides avec {pre} mais sans {pre}_: {keys_fail}"
19+
raise KeyError(msg)
20+
21+
# Construct the data dict
22+
return {k.replace(pre + "_", ""): row[k] for k in keys_ok}

dags/enrich/tasks/business_logic/enrich_dbt_model_to_suggestions.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,15 @@
55
from cluster.tasks.business_logic.cluster_acteurs_parents_choose_new import (
66
parent_id_generate,
77
)
8-
from enrich.config import COHORTS, COLS, SUGGEST_PREFIX
8+
from enrich.config import COHORTS, COLS
9+
from enrich.tasks.business_logic.enrich_dbt_model_row_to_suggest_data import (
10+
dbt_model_row_to_suggest_data,
11+
)
912
from utils import logging_utils as log
1013

1114
logger = logging.getLogger(__name__)
1215

1316

14-
def row_to_suggest_data(row: dict) -> dict:
15-
"""Construct the data dict from all row props starting with SUGGEST_PREFIX"""
16-
pre = SUGGEST_PREFIX
17-
keys_ok = [k for k in row.keys() if k.startswith(f"{pre}_")]
18-
keys_ok.remove(f"{pre}_cohort")
19-
20-
# Validation
21-
keys_fail = [
22-
k
23-
for k in row.keys()
24-
if pre in k and k not in keys_ok and not k.startswith(f"{pre}_")
25-
]
26-
if keys_fail:
27-
msg = f"Colonnes invalides avec {pre} mais sans {pre}_: {keys_fail}"
28-
raise KeyError(msg)
29-
30-
# Construct the data dict
31-
return {k.replace(pre + "_", ""): row[k] for k in keys_ok}
32-
33-
3417
def changes_prepare(
3518
model,
3619
model_params: dict,
@@ -100,7 +83,7 @@ def changes_prepare_closed_replaced(
10083
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
10184
# Parent
10285
parent_id = parent_id_generate([str(row[COLS.SUGGEST_SIRET])])
103-
parent_data = row_to_suggest_data(row)
86+
parent_data = dbt_model_row_to_suggest_data(row)
10487
parent_data["identifiant_unique"] = parent_id
10588
parent_data["source"] = None
10689
parent_data["statut"] = ActeurStatus.ACTIF
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import pytest
2-
from enrich.tasks.business_logic.enrich_dbt_model_to_suggestions import (
3-
row_to_suggest_data,
2+
from enrich.tasks.business_logic.enrich_dbt_model_row_to_suggest_data import (
3+
dbt_model_row_to_suggest_data,
44
)
55

66

7-
class TestEnrichSuggestionsRowToSuggestData:
7+
class TestEnrichDbtModelRowToData:
88

99
def test_row_to_suggest_data(self):
1010
row = {
1111
"suggest_cohort": "cohort",
1212
"suggest_siret": "12345678901234",
1313
"foo": "bar",
1414
}
15-
data = row_to_suggest_data(row)
15+
data = dbt_model_row_to_suggest_data(row)
1616
assert data == {"siret": "12345678901234"}
1717

1818
@pytest.mark.parametrize(
@@ -23,9 +23,9 @@ def test_raise_if_inconsistent_suggest_keys(self, key):
2323
row = {"suggest_cohort": "cohort"} # must always be present
2424
row[key] = "12345678901234"
2525
with pytest.raises(KeyError, match="Colonnes invalides"):
26-
row_to_suggest_data(row)
26+
dbt_model_row_to_suggest_data(row)
2727

2828
def test_raise_if_missing_cohort(self):
2929
row = {"suggest_siret": "12345678901234"}
3030
with pytest.raises(ValueError, match="not in list"):
31-
row_to_suggest_data(row)
31+
dbt_model_row_to_suggest_data(row)

0 commit comments

Comments
 (0)