From d62b951dcfa323d4d3fab03163d7ebdc22bc4a90 Mon Sep 17 00:00:00 2001 From: Alejandro MG Date: Fri, 12 Apr 2024 10:46:15 +0200 Subject: [PATCH 1/2] Fixes brevo usage test --- macantine/tests/test_brevo_user_data.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/macantine/tests/test_brevo_user_data.py b/macantine/tests/test_brevo_user_data.py index 793a5057d..aceb72c67 100644 --- a/macantine/tests/test_brevo_user_data.py +++ b/macantine/tests/test_brevo_user_data.py @@ -2,7 +2,6 @@ # import sib_api_v3_sdk from django.test import TestCase -from django.test.utils import tag from macantine import tasks from freezegun import freeze_time from data.factories import UserFactory, CanteenFactory, DiagnosticFactory, TeledeclarationFactory @@ -10,7 +9,6 @@ class TestBrevoUserData(TestCase): - @tag("DEBUG") @freeze_time("2021-01-20") @mock.patch("macantine.tasks.contacts_api_instance.create_contact") @mock.patch("macantine.tasks.contacts_api_instance.update_batch_contacts") @@ -25,7 +23,7 @@ def test_batch_user_updates(self, batch_update_mock, create_contact_mock): batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == new_user.email, batch_update_mock.call_args[0][0].contacts)) self.assertEqual(payload.email, new_user.email) attributes = payload.attributes @@ -72,7 +70,7 @@ def test_user_has_empty_canteen(self, batch_update_mock, create_contact_mock): batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -94,7 +92,7 @@ def test_user_has_published_canteen(self, batch_update_mock, create_contact_mock batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -119,7 +117,7 @@ def test_user_has_canteen_with_diag(self, batch_update_mock, create_contact_mock batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -159,7 +157,7 @@ def test_user_has_canteen_with_td(self, batch_update_mock, create_contact_mock): batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -195,7 +193,7 @@ def test_user_has_sat_canteen_with_cc_diag(self, batch_update_mock, create_conta batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -246,7 +244,7 @@ def test_user_has_sat_canteen_with_cc_td(self, batch_update_mock, create_contact batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = batch_update_mock.call_args[0][0].contacts[0] + payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) From b762b8b12db2f3e5abfc406bcb650318a5170823 Mon Sep 17 00:00:00 2001 From: Helen Root Date: Tue, 16 Apr 2024 00:09:26 +0200 Subject: [PATCH 2/2] Refacto suggestion --- macantine/tests/test_brevo_user_data.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/macantine/tests/test_brevo_user_data.py b/macantine/tests/test_brevo_user_data.py index aceb72c67..9a1f7e76d 100644 --- a/macantine/tests/test_brevo_user_data.py +++ b/macantine/tests/test_brevo_user_data.py @@ -9,6 +9,9 @@ class TestBrevoUserData(TestCase): + def _find_payload_for_user(contacts, user_email): + return next(filter(lambda x: x.email == user_email, contacts)) + @freeze_time("2021-01-20") @mock.patch("macantine.tasks.contacts_api_instance.create_contact") @mock.patch("macantine.tasks.contacts_api_instance.update_batch_contacts") @@ -23,7 +26,7 @@ def test_batch_user_updates(self, batch_update_mock, create_contact_mock): batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == new_user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, new_user.email) self.assertEqual(payload.email, new_user.email) attributes = payload.attributes @@ -70,7 +73,7 @@ def test_user_has_empty_canteen(self, batch_update_mock, create_contact_mock): batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, user.email) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -92,7 +95,7 @@ def test_user_has_published_canteen(self, batch_update_mock, create_contact_mock batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, user.email) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -117,7 +120,7 @@ def test_user_has_canteen_with_diag(self, batch_update_mock, create_contact_mock batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, user.email) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -157,7 +160,7 @@ def test_user_has_canteen_with_td(self, batch_update_mock, create_contact_mock): batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, user.email) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -193,7 +196,7 @@ def test_user_has_sat_canteen_with_cc_diag(self, batch_update_mock, create_conta batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, user.email) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True) @@ -244,7 +247,7 @@ def test_user_has_sat_canteen_with_cc_td(self, batch_update_mock, create_contact batch_update_mock.assert_called_once() create_contact_mock.assert_not_called() - payload = next(filter(lambda x: x.email == user.email, batch_update_mock.call_args[0][0].contacts)) + payload = TestBrevoUserData._find_payload_for_user(batch_update_mock.call_args[0][0].contacts, user.email) attributes = payload.attributes self.assertEqual(attributes.get("MA_CANTINE_GERE_UN_ETABLISSEMENT"), True) self.assertEqual(attributes.get("MA_CANTINE_MANQUE_BILAN_DONNEES_2023"), True)