Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/add_forge_now/test_migration.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||||
import pytest | import pytest | ||||
from django.core.exceptions import ValidationError | |||||
from swh.web.add_forge_now.apps import APP_LABEL | from swh.web.add_forge_now.apps import APP_LABEL | ||||
MIGRATION_0001 = "0001_initial" | MIGRATION_0001 = "0001_initial" | ||||
MIGRATION_0002 = "0002_authorized_null_comment" | MIGRATION_0002 = "0002_authorized_null_comment" | ||||
MIGRATION_0003 = "0003_request_submitter_forward_username" | MIGRATION_0003 = "0003_request_submitter_forward_username" | ||||
MIGRATION_0005 = "0005_prepare_inbound_email" | MIGRATION_0005 = "0005_prepare_inbound_email" | ||||
MIGRATION_0006 = "0006_request_add_new_fields" | MIGRATION_0006 = "0006_request_add_new_fields" | ||||
MIGRATION_0007 = "0007_rename_denied_request_status" | |||||
def now() -> datetime: | def now() -> datetime: | ||||
return datetime.now(tz=timezone.utc) | return datetime.now(tz=timezone.utc) | ||||
def test_add_forge_now_initial_migration(migrator): | def test_add_forge_now_initial_migration(migrator): | ||||
"""Basic migration test to check the model is fine""" | """Basic migration test to check the model is fine""" | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | def test_add_forge_now_add_new_fields_to_request(migrator): | ||||
assert hasattr(Request, "last_moderator") | assert hasattr(Request, "last_moderator") | ||||
assert hasattr(Request, "last_modified_date") | assert hasattr(Request, "last_modified_date") | ||||
for request in Request.objects.all(): | for request in Request.objects.all(): | ||||
history = RequestHistory.objects.filter(request=request) | history = RequestHistory.objects.filter(request=request) | ||||
history = history.order_by("id") | history = history.order_by("id") | ||||
assert request.last_modified_date == history.last().date | assert request.last_modified_date == history.last().date | ||||
assert request.last_moderator == history.last().actor | assert request.last_moderator == history.last().actor | ||||
def test_add_forge_now_denied_status_renamed_to_unsuccesful(migrator): | |||||
state = migrator.apply_tested_migration((APP_LABEL, MIGRATION_0006)) | |||||
Request = state.apps.get_model(APP_LABEL, "Request") | |||||
from swh.web.add_forge_now.models import RequestStatus | |||||
req = Request( | |||||
anlambert: Use `RequestStatus.UNSUCCESSFUL.name` here to set the value and fix the test. | |||||
Done Inline Actionsgreat, I had the right feeling :) bchauvet: great, I had the right feeling :) | |||||
status=RequestStatus.UNSUCCESSFUL.name, | |||||
submitter_name="dudess", | |||||
submitter_email="dudess@orga.org", | |||||
forge_type="cgit", | |||||
forge_url="https://example.org/forge", | |||||
forge_contact_email="forge@example.org", | |||||
forge_contact_name="forge", | |||||
forge_contact_comment=( | |||||
"Discovered on the main forge homepag, following contact link." | |||||
), | |||||
last_modified_date=datetime.now(timezone.utc), | |||||
) | |||||
with pytest.raises(ValidationError): | |||||
req.clean_fields() | |||||
state = migrator.apply_tested_migration((APP_LABEL, MIGRATION_0007)) | |||||
Request = state.apps.get_model(APP_LABEL, "Request") | |||||
Done Inline Actionssame here anlambert: same here | |||||
req = Request( | |||||
status=RequestStatus.UNSUCCESSFUL.name, | |||||
submitter_name="dudess", | |||||
submitter_email="dudess@orga.org", | |||||
forge_type="cgit", | |||||
forge_url="https://example.org/forge", | |||||
forge_contact_email="forge@example.org", | |||||
forge_contact_name="forge", | |||||
forge_contact_comment=( | |||||
"Discovered on the main forge homepag, following contact link." | |||||
), | |||||
last_modified_date=datetime.now(timezone.utc), | |||||
) | |||||
req.clean_fields() |
Use RequestStatus.UNSUCCESSFUL.name here to set the value and fix the test.