From 0d885b9b2bf51d561acfd5a1b1b2d1eee407c798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Wed, 11 Sep 2024 08:55:10 +0200 Subject: [PATCH] Fix flakiness of Announcement test_start_date_conflict_constraint The first existing_campaign is unused in the test and looks like an oversight. Example failure: ```python def test_start_date_conflict_constraint(self): existing_campaign = AnnouncementCampaignFactory() # can modify existing value without triggering constraint > existing_campaign = AnnouncementCampaignFactory(start_date=date(2024, 1, 1)) tests/communications/test_models.py:126: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.11/site-packages/factory/base.py:43: in __call__ return cls.create(**kwargs) .venv/lib/python3.11/site-packages/factory/base.py:539: in create return cls._generate(enums.CREATE_STRATEGY, kwargs) .venv/lib/python3.11/site-packages/factory/django.py:122: in _generate return super()._generate(strategy, params) .venv/lib/python3.11/site-packages/factory/base.py:468: in _generate return step.build() .venv/lib/python3.11/site-packages/factory/builder.py:274: in build instance = self.factory_meta.instantiate( .venv/lib/python3.11/site-packages/factory/base.py:320: in instantiate return self.factory._create(model, *args, **kwargs) .venv/lib/python3.11/site-packages/factory/django.py:175: in _create return manager.create(*args, **kwargs) .venv/lib/python3.11/site-packages/django/db/models/manager.py:87: in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) .venv/lib/python3.11/site-packages/django/db/models/query.py:679: in create obj.save(force_insert=True, using=self.db) itou/communications/models.py:178: in save super().save(*args, **kwargs) .venv/lib/python3.11/site-packages/django/db/models/base.py:822: in save self.save_base( .venv/lib/python3.11/site-packages/django/db/models/base.py:909: in save_base updated = self._save_table( .venv/lib/python3.11/site-packages/django/db/models/base.py:1071: in _save_table results = self._do_insert( .venv/lib/python3.11/site-packages/django/db/models/base.py:1112: in _do_insert return manager._insert( .venv/lib/python3.11/site-packages/django/db/models/manager.py:87: in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) .venv/lib/python3.11/site-packages/django/db/models/query.py:1847: in _insert return query.get_compiler(using=using).execute_sql(returning_fields) .venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py:1823: in execute_sql cursor.execute(sql, params) .venv/lib/python3.11/site-packages/sentry_sdk/utils.py:1730: in runner return sentry_patched_function(*args, **kwargs) .venv/lib/python3.11/site-packages/sentry_sdk/integrations/django/__init__.py:651: in execute result = real_execute(self, sql, params) .venv/lib/python3.11/site-packages/django/db/backends/utils.py:79: in execute return self._execute_with_wrappers( .venv/lib/python3.11/site-packages/django/db/backends/utils.py:92: in _execute_with_wrappers return executor(sql, params, many, context) .venv/lib/python3.11/site-packages/django/db/backends/utils.py:100: in _execute with self.db.wrap_database_errors: .venv/lib/python3.11/site-packages/django/db/utils.py:91: in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value .venv/lib/python3.11/site-packages/django/db/backends/utils.py:105: in _execute return self.cursor.execute(sql, params) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = query = 'INSERT INTO "communications_announcementcampaign" ("max_items", "start_date", "live") VALUES (%s, %s, %s) RETURNING "communications_announcementcampaign"."id"' params = (Int4(3), datetime.date(2024, 1, 1), True) def execute( self, query: Query, params: Params | None = None, *, prepare: bool | None = None, binary: bool | None = None, ) -> Self: """ Execute a query or command to the database. """ try: with self._conn.lock: self._conn.wait( self._execute_gen(query, params, prepare=prepare, binary=binary) ) except e._NO_TRACEBACK as ex: > raise ex.with_traceback(None) E django.db.utils.IntegrityError: duplicate key value violates unique constraint "communications_announcementcampaign_start_date_key" E DETAIL: Key (start_date)=(2024-01-01) already exists. ``` --- tests/communications/test_models.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/communications/test_models.py b/tests/communications/test_models.py index 79950535f31..31d8fcf5454 100644 --- a/tests/communications/test_models.py +++ b/tests/communications/test_models.py @@ -119,8 +119,6 @@ def test_start_date_day_constraint(self): AnnouncementCampaignFactory(start_date=date(2024, 1, 2)) def test_start_date_conflict_constraint(self): - existing_campaign = AnnouncementCampaignFactory() - # can modify existing value without triggering constraint existing_campaign = AnnouncementCampaignFactory(start_date=date(2024, 1, 1)) existing_campaign.start_date = date(2024, 2, 1)