Skip to content

Commit

Permalink
Amélioration des tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Nov 27, 2024
1 parent b8d477e commit 8482909
Showing 1 changed file with 91 additions and 38 deletions.
129 changes: 91 additions & 38 deletions lemarche/crm/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,65 @@ def setUpTestData(cls):
"""Siae instances initialization"""
cls.user_siae = UserFactory(kind=User.KIND_SIAE)
cls.siae_with_name = SiaeFactory(name="Test Company 1")
cls.siae_with_tender = SiaeFactory(users=[cls.user_siae])
cls.siae_with_user = SiaeFactory(users=[cls.user_siae])
cls.siae_with_brevo_id = SiaeFactory(
brevo_company_id="123456789",
completion_rate=50,
)

cls.tender_with_siae = TenderFactory(
siaes=[cls.siae_with_tender, cls.siae_with_brevo_id], deadline_date=date_tomorrow
)
cls.tender = TenderFactory(deadline_date=date_tomorrow)

TenderSiae.objects.create(
tender=cls.tender_with_siae,
siae=cls.siae_with_tender,
email_send_date=now,
tender=cls.tender,
siae=cls.siae_with_user,
detail_contact_click_date=now,
)
TenderSiae.objects.create(
tender=cls.tender_with_siae,
tender=cls.tender,
siae=cls.siae_with_brevo_id,
email_send_date=now,
detail_contact_click_date=now,
detail_contact_click_date=old_date,
)
TenderSiae.objects.create(
tender=cls.tender_with_siae,
siae=cls.siae_with_brevo_id,
email_send_date=now, # should be updated with 90 days filter
detail_contact_click_date=old_date, # should not be updated with 90 days filter

def test_annotated_fields_set_up(self):
"""Test annotated fields are correctly set up"""
siae_with_user_stats = Siae.objects.with_tender_stats().filter(id=self.siae_with_user.id).first()

self.assertEqual(
siae_with_user_stats.tender_email_send_count_annotated,
0,
"Le nombre total de besoins reçus devrait être 0",
)
self.assertEqual(
siae_with_user_stats.tender_detail_contact_click_count_annotated,
1,
"Le nombre total de besoins intéressés devrait être 1",
)

siae_with_brevo_id_stats = Siae.objects.with_tender_stats().filter(id=self.siae_with_brevo_id.id).first()
self.assertEqual(
siae_with_brevo_id_stats.tender_email_send_count_annotated,
1,
"Le nombre total de besoins reçus devrait être 1",
)
self.assertEqual(
siae_with_brevo_id_stats.tender_detail_contact_click_count_annotated,
1,
"Le nombre total de besoins intéressés devrait être 1",
)

siae_with_brevo_id_recent_stats = (
Siae.objects.with_tender_stats(since_days=90).filter(id=self.siae_with_brevo_id.id).first()
)
self.assertEqual(
siae_with_brevo_id_recent_stats.tender_email_send_count_annotated,
1,
"Le nombre total de besoins reçus devrait être 1",
)
self.assertEqual(
siae_with_brevo_id_recent_stats.tender_detail_contact_click_count_annotated,
0,
"Le nombre total de besoins intéressés devrait être 0",
)

@patch("lemarche.utils.apis.api_brevo.create_or_update_company")
Expand All @@ -59,25 +92,37 @@ def test_new_siaes_are_synced_in_brevo(self, mock_create_or_update_company):

self.assertEqual(mock_create_or_update_company.call_count, 3)

def test_siae_has_tender_stats(self):
siae_with_user_tender_stats = Siae.objects.with_tender_stats().filter(id=self.siae_with_brevo_id.id).first()
siae_with_brevo_id_tender_stats = (
Siae.objects.with_tender_stats().filter(id=self.siae_with_brevo_id.id).first()
)
self.assertIsNotNone(
siae_with_user_tender_stats,
"Cette SIAE devrait avoir des statistiques sur les besoins.",
)
self.assertIsNotNone(
siae_with_brevo_id_tender_stats,
"Cette SIAE devrait avoir des statistiques sur les besoins.",
)

def test_siae_extra_data_is_set_on_first_sync(self):
"""Test siae is updated if extra_data is changed."""
initial_extra_data = self.siae_with_tender.extra_data.copy()
initial_extra_data = self.siae_with_user.extra_data.copy()
call_command("crm_brevo_sync_companies", recently_updated=True)

siae_with_tender_stats = (
Siae.objects.with_tender_stats(since_days=90).filter(id=self.siae_with_tender.id).first()
)
self.siae_with_tender.refresh_from_db()
siae_with_user_stats = Siae.objects.with_tender_stats(since_days=90).filter(id=self.siae_with_user.id).first()
self.siae_with_user.refresh_from_db()

expected_extra_data = {
"completion_rate": self.siae_with_tender.completion_rate,
"recent_tender_email_send_count": siae_with_tender_stats.tender_email_send_count_annotated,
"recent_tender_detail_click_count": siae_with_tender_stats.tender_detail_contact_click_count_annotated,
"completion_rate": self.siae_with_user.completion_rate,
"recent_tender_email_send_count": siae_with_user_stats.tender_email_send_count_annotated,
"recent_tender_detail_click_count": siae_with_user_stats.tender_detail_contact_click_count_annotated,
}

self.assertNotEqual(initial_extra_data, expected_extra_data, "siae.extra_data aurait dû être mis à jour.")
self.assertEqual(
self.siae_with_tender.extra_data, expected_extra_data, "siae.extra_data n'est pas conforme aux attentes."
self.siae_with_user.extra_data, expected_extra_data, "siae.extra_data n'est pas conforme aux attentes."
)

def test_siae_extra_data_is_not_updated_if_no_changes(self):
Expand Down Expand Up @@ -105,29 +150,43 @@ def test_siae_extra_data_is_not_updated_if_no_changes(self):

def test_fields_update_within_90_days_and_ignore_older_changes(self):
"""Test fields update within 90 days and ignore older changes."""
# Update annotated fields of a TenderSiae
TenderSiae.objects.filter(
tender=self.tender,
siae=self.siae_with_brevo_id,
).update(
email_send_date=now,
detail_contact_click_date=now,
)

call_command("crm_brevo_sync_companies", recently_updated=True)

# Queryset with 90 days filter
siae_with_all_stats = Siae.objects.with_tender_stats().filter(id=self.siae_with_brevo_id.id).first()
siae_with_recent_stats = (
Siae.objects.with_tender_stats(since_days=90).filter(id=self.siae_with_brevo_id.id).first()
)
# Queryset without 90 days filter
siae_with_all_stats = Siae.objects.with_tender_stats().filter(id=self.siae_with_brevo_id.id).first()

# Check that the annotated fields have been updated or not
expected_extra_data = {
"completion_rate": self.siae_with_brevo_id.completion_rate,
"recent_tender_email_send_count": siae_with_recent_stats.tender_email_send_count_annotated,
"recent_tender_detail_click_count": siae_with_recent_stats.tender_detail_contact_click_count_annotated,
}

self.assertEqual(
siae_with_all_stats.tender_email_send_count_annotated, 2, "Le compte total d'envois d'email devrait être 2"
siae_with_all_stats.tender_email_send_count_annotated, 1, "Le compte total d'envois d'email devrait être 1"
)

self.assertEqual(
siae_with_recent_stats.tender_email_send_count_annotated,
2,
"Les envois récents d'email dans les 90 jours devraient être 2",
1,
"Les envois récents d'email dans les 90 jours devraient être 1",
)
self.assertEqual(
siae_with_all_stats.tender_detail_contact_click_count_annotated,
2,
"Le compte total de clics de contact devrait être 2",
1,
"Le compte total de clics de contact devrait être 1",
)

self.assertEqual(
siae_with_recent_stats.tender_detail_contact_click_count_annotated,
1,
Expand All @@ -136,12 +195,6 @@ def test_fields_update_within_90_days_and_ignore_older_changes(self):

self.siae_with_brevo_id.refresh_from_db()

expected_extra_data = {
"completion_rate": self.siae_with_brevo_id.completion_rate,
"recent_tender_email_send_count": siae_with_recent_stats.tender_email_send_count_annotated,
"recent_tender_detail_click_count": siae_with_recent_stats.tender_detail_contact_click_count_annotated,
}

self.assertEqual(
self.siae_with_brevo_id.extra_data,
expected_extra_data,
Expand Down

0 comments on commit 8482909

Please sign in to comment.