Skip to content

Commit

Permalink
Fix flakiness of Announcement test_start_date_conflict_constraint
Browse files Browse the repository at this point in the history
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 = <django.db.backends.postgresql.base.Cursor [closed] [INTRANS] (host=localhost user=postgres database=test_itou_gw6) at 0x6e3657fadb50>
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.
```
  • Loading branch information
francoisfreitag committed Sep 11, 2024
1 parent 444d56d commit 0d885b9
Showing 1 changed file with 0 additions and 2 deletions.
2 changes: 0 additions & 2 deletions tests/communications/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0d885b9

Please sign in to comment.