|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | +from enrich.config import COHORTS, COLS |
| 4 | +from enrich.tasks.business_logic.enrich_dbt_model_to_suggestions import ( |
| 5 | + enrich_dbt_model_to_suggestions, |
| 6 | +) |
| 7 | + |
| 8 | +from data.models.changes.acteur_rgpd_anonymize import ACTEUR_FIELDS_TO_ANONYMIZE |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.django_db |
| 12 | +class TestEnrichSuggestionsRgpd: |
| 13 | + |
| 14 | + @pytest.fixture |
| 15 | + def df_rgpd(self): |
| 16 | + return pd.DataFrame( |
| 17 | + { |
| 18 | + COLS.SUGGEST_COHORT: [COHORTS.RGPD] * 2, |
| 19 | + COLS.ACTEUR_ID: ["rgpd1", "rgpd2"], |
| 20 | + COLS.ACTEUR_STATUT: ["ACTIF"] * 2, |
| 21 | + COLS.ACTEUR_NOMS_ORIGINE: ["nom rgpd1", "nom rgpd2"], |
| 22 | + COLS.ACTEUR_NOM: ["nom rgpd1", "nom rgpd2"], |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def acteurs(self, df_rgpd): |
| 28 | + # The point of the RGPD pipeline is to anonymize the data |
| 29 | + # everywhere in our DB, thus it won't follow the usual pattern |
| 30 | + # of creating/updating a revision only, it overwrites acteurs |
| 31 | + # wherever they are found WITHOUT creating revivisions |
| 32 | + from unit_tests.qfdmo.acteur_factory import ActeurFactory, RevisionActeurFactory |
| 33 | + |
| 34 | + # rgpd1 only in base |
| 35 | + ActeurFactory(identifiant_unique="rgpd1", nom="nom rgpd1") |
| 36 | + # rgpd2 in both base and revision |
| 37 | + ActeurFactory(identifiant_unique="rgpd2", nom="nom rgpd2") |
| 38 | + RevisionActeurFactory(pk="rgpd2", nom="nom rgpd2") |
| 39 | + |
| 40 | + @pytest.fixture |
| 41 | + def suggestions_applied(self, acteurs, df_rgpd): |
| 42 | + """Generating and applying suggestions""" |
| 43 | + from data.models.suggestion import Suggestion, SuggestionCohorte |
| 44 | + |
| 45 | + enrich_dbt_model_to_suggestions( |
| 46 | + df=df_rgpd, |
| 47 | + cohort=COHORTS.RGPD, |
| 48 | + identifiant_action="test_rgpd", |
| 49 | + dry_run=False, |
| 50 | + ) |
| 51 | + cohort = SuggestionCohorte.objects.get(identifiant_action="test_rgpd") |
| 52 | + suggestions = Suggestion.objects.filter(suggestion_cohorte=cohort) |
| 53 | + assert len(suggestions) == 2 |
| 54 | + # Apply suggestions |
| 55 | + for suggestion in suggestions: |
| 56 | + suggestion.apply() |
| 57 | + |
| 58 | + def test_changed_only_in_base(self, suggestions_applied): |
| 59 | + # Test that the acteur in base is changed WITHOUT creating a revision |
| 60 | + from qfdmo.models import Acteur |
| 61 | + |
| 62 | + rgpd1 = Acteur.objects.get(pk="rgpd1") |
| 63 | + assert all( |
| 64 | + getattr(rgpd1, field) == value |
| 65 | + for field, value in ACTEUR_FIELDS_TO_ANONYMIZE.items() |
| 66 | + ) |
| 67 | + |
| 68 | + def test_changed_in_both(self, suggestions_applied): |
| 69 | + # Test that the acteur in both base and revision is changed |
| 70 | + from qfdmo.models import Acteur, RevisionActeur |
| 71 | + |
| 72 | + rgpd2 = Acteur.objects.get(pk="rgpd2") |
| 73 | + assert all( |
| 74 | + getattr(rgpd2, field) == value |
| 75 | + for field, value in ACTEUR_FIELDS_TO_ANONYMIZE.items() |
| 76 | + ) |
| 77 | + rgpd2_rev = RevisionActeur.objects.get(pk="rgpd2") |
| 78 | + assert all( |
| 79 | + getattr(rgpd2_rev, field) == value |
| 80 | + for field, value in ACTEUR_FIELDS_TO_ANONYMIZE.items() |
| 81 | + ) |
0 commit comments