diff --git a/pgcommitfest/commitfest/ajax.py b/pgcommitfest/commitfest/ajax.py index 8137dfdf..1c30ad0b 100644 --- a/pgcommitfest/commitfest/ajax.py +++ b/pgcommitfest/commitfest/ajax.py @@ -195,7 +195,7 @@ def parse_and_add_attachments(threadinfo, mailthread): # One or more attachments. For now, we're only actually going # to store and process the first one, even though the API gets # us all of them. - MailThreadAttachment.objects.get_or_create( + MailThreadAttachment.objects.update_or_create( mailthread=mailthread, messageid=t["msgid"], defaults={ diff --git a/pgcommitfest/commitfest/tests/test_refresh_thread.py b/pgcommitfest/commitfest/tests/test_refresh_thread.py index 55d22a22..3ebeeaa5 100644 --- a/pgcommitfest/commitfest/tests/test_refresh_thread.py +++ b/pgcommitfest/commitfest/tests/test_refresh_thread.py @@ -4,13 +4,13 @@ import pytest from pgcommitfest.commitfest.ajax import refresh_single_thread -from pgcommitfest.commitfest.models import MailThread, Patch +from pgcommitfest.commitfest.models import MailThread, MailThreadAttachment, Patch pytestmark = pytest.mark.django_db -def test_refresh_single_thread_updates_patch_lastmail(): - """Regression test: patch.lastmail should get the new date, not the old one.""" +def test_refresh_single_thread(): + """A new mail updates patch.lastmail and refreshes stale attachment info.""" old_date = datetime(2024, 1, 1) new_date = datetime(2024, 6, 15) @@ -26,6 +26,14 @@ def test_refresh_single_thread_updates_patch_lastmail(): ) p = Patch.objects.create(name="Test Patch", lastmail=old_date) p.mailthread_set.add(thread) + attachment = MailThreadAttachment.objects.create( + mailthread=thread, + messageid="old@example.com", + attachmentid=1, + filename="old.patch", + date=old_date, + author="a", + ) api_response = [ { @@ -33,7 +41,8 @@ def test_refresh_single_thread_updates_patch_lastmail(): "date": old_date, "from": "a", "subj": "T", - "atts": [], + # The archives assigned a new id to the same attachment + "atts": [{"id": 2, "name": "new.patch"}], }, { "msgid": "new@example.com", @@ -49,3 +58,6 @@ def test_refresh_single_thread_updates_patch_lastmail(): p.refresh_from_db() assert p.lastmail == new_date + attachment.refresh_from_db() + assert (attachment.attachmentid, attachment.filename) == (2, "new.patch") + assert MailThreadAttachment.objects.filter(mailthread=thread).count() == 1