Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charludo committed Jan 15, 2025
1 parent fb3e119 commit bef3b67
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 138 deletions.
4 changes: 3 additions & 1 deletion integreat_cms/summ_ai_api/summ_ai_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ async def translate_text_field(
# will raise exceptions - so we don't need an else branch.
except (TimeoutError, aiohttp.ClientError, SummAiRuntimeError) as e:
logger.exception(
"SUMM.AI translation of %r failed",
"SUMM.AI translation of %r failed because of %s: %s",
text_field,
type(e), # noqa: TRY401
e, # noqa: TRY401
)
text_field.exception = e
return text_field
Expand Down
18 changes: 9 additions & 9 deletions tests/cms/utils/test_slug_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def test_generate_unique_slug_fallback(
"foreign_model": "region",
"fallback": "name",
}
assert generate_unique_slug(**kwargs) == "unique_slug_fallback", (
"Name is not used as fallback when slug is missing"
)
assert (
generate_unique_slug(**kwargs) == "unique_slug_fallback"
), "Name is not used as fallback when slug is missing"


@pytest.mark.django_db
Expand All @@ -54,9 +54,9 @@ def test_generate_unique_slug_reserved_region_slug(
"foreign_model": "region",
"fallback": "name",
}
assert generate_unique_slug(**kwargs) == "landing-2", (
"Reserved region slug is not prevented"
)
assert (
generate_unique_slug(**kwargs) == "landing-2"
), "Reserved region slug is not prevented"


@pytest.mark.django_db
Expand All @@ -77,9 +77,9 @@ def test_generate_unique_slug_reserved_page_slug(
"object_instance": page,
"foreign_model": "page",
}
assert generate_unique_slug(**kwargs) == "disclaimer-2", (
"Reserved imprint slug is not prevented for pages"
)
assert (
generate_unique_slug(**kwargs) == "disclaimer-2"
), "Reserved imprint slug is not prevented for pages"


def test_generate_unique_slug_no_fallback() -> None:
Expand Down
18 changes: 9 additions & 9 deletions tests/cms/views/dashboard/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ def test_number_of_outdated_pages_is_correct(
response.content.decode("utf-8"),
)
assert match, "Number of outdated pages not displayed"
assert int(match.group(3)) == expected_number_of_pages, (
"Wrong number of pages displayed as outdated"
)
assert (
int(match.group(3)) == expected_number_of_pages
), "Wrong number of pages displayed as outdated"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -206,9 +206,9 @@ def test_most_outdated_page_is_correct(
response.content.decode("utf-8"),
)
assert match, "Not updated message missing"
assert int(match.group(1)) == days_since_page_is_outdated, (
"Not updated message displaying wrong days count"
)
assert (
int(match.group(1)) == days_since_page_is_outdated
), "Not updated message displaying wrong days count"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -278,9 +278,9 @@ def test_number_of_drafted_pages_is_correct(
response.content.decode("utf-8"),
)
assert match, "Number of drafted pages not displayed"
assert int(match.group(3)) == expected_number_of_pages, (
"Wrong number of pages displayed as outdated"
)
assert (
int(match.group(3)) == expected_number_of_pages
), "Wrong number of pages displayed as outdated"


@pytest.mark.parametrize(
Expand Down
30 changes: 15 additions & 15 deletions tests/cms/views/events/external_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ def test_creating_new_external_calendar(

edit_url = response.headers.get("location")
url_params = resolve(edit_url)
assert url_params.url_name == "edit_external_calendar", (
"We should be redirected to the edit view"
)
assert url_params.kwargs["region_slug"] == "augsburg", (
"The region shouldn't be different from the request"
)
assert (
url_params.url_name == "edit_external_calendar"
), "We should be redirected to the edit view"
assert (
url_params.kwargs["region_slug"] == "augsburg"
), "The region shouldn't be different from the request"
id_of_external_calendar = url_params.kwargs["calendar_id"]

external_calendar = ExternalCalendar.objects.get(id=id_of_external_calendar)
assert external_calendar.name == "Test external calendar", (
"Name should be successfully set on the model"
)
assert external_calendar.url == "https://integreat.app/", (
"URL should be successfully set on the model"
)
assert external_calendar.import_filter_category == "integreat", (
"Filter category should be successfully set on the model"
)
assert (
external_calendar.name == "Test external calendar"
), "Name should be successfully set on the model"
assert (
external_calendar.url == "https://integreat.app/"
), "URL should be successfully set on the model"
assert (
external_calendar.import_filter_category == "integreat"
), "Filter category should be successfully set on the model"
elif role == ANONYMOUS:
assert response.status_code == 302, "We should be redirected to the login view"
assert (
Expand Down
3 changes: 1 addition & 2 deletions tests/cms/views/events/test_event_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def test_create_event(
data=data,
)
if role in PRIV_STAFF_ROLES + WRITE_ROLES:
assert response.status_code == 302

assert response.status_code in [200, 302]
if successfully_created:
redirect_url = response.headers.get("location")
assert_message_in_log(
Expand Down
41 changes: 20 additions & 21 deletions tests/cms/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,34 @@ def check_view_status_code(
or post_kwargs.get("content_type") == "application/json"
):
# Json-views should return 200 or 201 CREATED (for feedback)
assert response.status_code in [
200,
201,
], (
f"JSON view {view_name} returned status code {response.status_code} instead of 200 or 201 for role {role}"
)
assert (
response.status_code
in [
200,
201,
]
), f"JSON view {view_name} returned status code {response.status_code} instead of 200 or 201 for role {role}"
else:
# Normal post-views should redirect after a successful operation (200 usually mean form errors)
assert response.status_code == 302, (
f"POST view {view_name} returned status code {response.status_code} instead of 302 for role {role}"
)
assert (
response.status_code == 302
), f"POST view {view_name} returned status code {response.status_code} instead of 302 for role {role}"
else:
# Get-views should return 200
assert response.status_code == 200, (
f"GET view {view_name} returned status code {response.status_code} instead of 200 for role {role}"
)
assert (
response.status_code == 200
), f"GET view {view_name} returned status code {response.status_code} instead of 200 for role {role}"
elif role == ANONYMOUS:
# For anonymous users, we want to redirect to the login form instead of showing an error

assert response.status_code == 302, (
f"View {view_name} did not enforce access control for anonymous users (status code {response.status_code} instead of 302)"
)
assert (
response.status_code == 302
), f"View {view_name} did not enforce access control for anonymous users (status code {response.status_code} instead of 302)"
assert (
response.headers.get("location") == f"{settings.LOGIN_URL}?next={next_url}"
), (
f"View {view_name} did not redirect to login for anonymous users (location header {response.headers.get('location')})"
)
), f"View {view_name} did not redirect to login for anonymous users (location header {response.headers.get('location')})"
else:
# For logged in users, we want to show an error if they get a permission denied
assert response.status_code == 403, (
f"View {view_name} did not enforce access control for role {role} (status code {response.status_code} instead of 403)"
)
assert (
response.status_code == 403
), f"View {view_name} did not enforce access control for role {role} (status code {response.status_code} instead of 403)"
12 changes: 6 additions & 6 deletions tests/core/management/commands/test_duplicate_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def test_duplicate_pages(settings: SettingsWrapper, load_test_data: None) -> Non
translations_count_after = PageTranslation.objects.filter(
page__region__slug=region_slug,
).count()
assert page_count_after == 2 * page_count_before, (
"The count of pages should have doubled after the command"
)
assert translations_count_after == 2 * translations_count_before, (
"The count of page translations should have doubled after the command"
)
assert (
page_count_after == 2 * page_count_before
), "The count of pages should have doubled after the command"
assert (
translations_count_after == 2 * translations_count_before
), "The count of page translations should have doubled after the command"
18 changes: 9 additions & 9 deletions tests/core/management/commands/test_fix_internal_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def test_fix_internal_links_dry_run(
assert Url.objects.filter(
url=old_url,
).exists(), "Old URL should not be removed during dry run"
assert Link.objects.filter(url__url=old_url).count() == link_occurences, (
"Old link should not be modified during dry run"
)
assert (
Link.objects.filter(url__url=old_url).count() == link_occurences
), "Old link should not be modified during dry run"

for new_url in new_urls:
assert not Url.objects.filter(
Expand Down Expand Up @@ -138,14 +138,14 @@ def test_fix_internal_links_commit(load_test_data_transactional: Any | None) ->
old_urls,
strict=False,
):
assert Link.objects.filter(url__url=old_url).count() < link_occurrences, (
"Some old links should not exist after replacement"
)
assert (
Link.objects.filter(url__url=old_url).count() < link_occurrences
), "Some old links should not exist after replacement"

for new_url in new_urls:
assert Url.objects.filter(
url=new_url,
).exists(), "New URL should exist after replacement"
assert Link.objects.filter(url__url=new_url).count() == 1, (
"New link should exist after replacement"
)
assert (
Link.objects.filter(url__url=new_url).count() == 1
), "New link should exist after replacement"
12 changes: 6 additions & 6 deletions tests/core/management/commands/test_import_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ def test_import_successful(
).exists(), "Event should not exist before import"

for rule in RecurrenceRule.objects.all():
assert rule.to_ical_rrule_string() not in recurrence_rules, (
"Recurrence rule should not exist before import"
)
assert (
rule.to_ical_rrule_string() not in recurrence_rules
), "Recurrence rule should not exist before import"

_, err = get_command_output("import_events")
assert not err
Expand Down Expand Up @@ -193,9 +193,9 @@ def test_update_event(httpserver: HTTPServer, load_test_data: None) -> None:
assert not err
assert "Imported event" in out

assert event_translation.latest_version.title == CALENDAR_V2_EVENT_NAME, (
"event should be renamed"
)
assert (
event_translation.latest_version.title == CALENDAR_V2_EVENT_NAME
), "event should be renamed"


@pytest.mark.django_db
Expand Down
24 changes: 12 additions & 12 deletions tests/core/management/commands/test_replace_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def test_replace_links_dry_run(load_test_data_transactional: Any | None) -> None
assert Url.objects.filter(
url=test_url,
).exists(), "Test URL should exist in test data"
assert Link.objects.filter(url__url=test_url).count() == 4, (
"Test link should exist in test data"
)
assert (
Link.objects.filter(url__url=test_url).count() == 4
), "Test link should exist in test data"
assert not Url.objects.filter(
url=replaced_url,
).exists(), "Replaced URL should not exist in test data"
Expand All @@ -108,9 +108,9 @@ def test_replace_links_dry_run(load_test_data_transactional: Any | None) -> None
assert Url.objects.filter(
url=test_url,
).exists(), "Test URL should not be removed during dry run"
assert Link.objects.filter(url__url=test_url).count() == 4, (
"Test link should not be removed during dry run"
)
assert (
Link.objects.filter(url__url=test_url).count() == 4
), "Test link should not be removed during dry run"
assert not Url.objects.filter(
url=replaced_url,
).exists(), "Replaced URL should not be created during dry run"
Expand All @@ -134,9 +134,9 @@ def test_replace_links_commit(load_test_data_transactional: Any | None) -> None:
assert Url.objects.filter(
url=test_url,
).exists(), "Test URL should not be removed during dry run"
assert Link.objects.filter(url__url=test_url).count() == 4, (
"Test link should not be removed during dry run"
)
assert (
Link.objects.filter(url__url=test_url).count() == 4
), "Test link should not be removed during dry run"
assert not Url.objects.filter(
url=replaced_url,
).exists(), "Replaced URL should not be created during dry run"
Expand All @@ -159,6 +159,6 @@ def test_replace_links_commit(load_test_data_transactional: Any | None) -> None:
assert Url.objects.filter(
url=replaced_url,
).exists(), "Replaced URL should exist after replacement"
assert Link.objects.filter(url__url=replaced_url).count() == 4, (
"Replaced link should exist after replacement"
)
assert (
Link.objects.filter(url__url=replaced_url).count() == 4
), "Replaced link should exist after replacement"
24 changes: 12 additions & 12 deletions tests/core/management/commands/test_reset_deepl_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ def test_reset_mt_budget(load_test_data_transactional: Any | None) -> None:
region1.refresh_from_db()
region2.refresh_from_db()
assert "✔ MT budget has been reset." in out
assert not region1.mt_budget_used, (
"The MT budget of region 1 should have been reset to 0."
)
assert region2.mt_budget_used == 42, (
"The MT budget of region 2 should remain unchanged."
)
assert region1.mt_midyear_start_month is None, (
"The midyear start month of region 1 should have been reset to None."
)
assert region2.mt_midyear_start_month == current_month + 1, (
"The midyear start month of region 2 should not have been reset to None."
)
assert (
not region1.mt_budget_used
), "The MT budget of region 1 should have been reset to 0."
assert (
region2.mt_budget_used == 42
), "The MT budget of region 2 should remain unchanged."
assert (
region1.mt_midyear_start_month is None
), "The midyear start month of region 1 should have been reset to None."
assert (
region2.mt_midyear_start_month == current_month + 1
), "The midyear start month of region 2 should not have been reset to None."
24 changes: 12 additions & 12 deletions tests/pdf/test_pdf_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ def test_pdf_export(
)
with open(f"tests/pdf/files/{expected_filename}", "rb") as file:
expected_pdf = PyPDF3.PdfFileReader(file)
# Assert that both documents have same number of pages
assert result_pdf.numPages == expected_pdf.numPages
# Assert that the content is identical
for page_number in range(result_pdf.numPages):
result_page = result_pdf.getPage(page_number)
expected_page = expected_pdf.getPage(page_number)
assert result_page.artBox == expected_page.artBox
assert result_page.bleedBox == expected_page.bleedBox
assert result_page.cropBox == expected_page.cropBox
assert result_page.mediaBox == expected_page.mediaBox
assert result_page.extractText() == expected_page.extractText()
assert result_page.getContents() == expected_page.getContents()
# Assert that both documents have same number of pages
assert result_pdf.numPages == expected_pdf.numPages
# Assert that the content is identical
for page_number in range(result_pdf.numPages):
result_page = result_pdf.getPage(page_number)
expected_page = expected_pdf.getPage(page_number)
assert result_page.artBox == expected_page.artBox
assert result_page.bleedBox == expected_page.bleedBox
assert result_page.cropBox == expected_page.cropBox
assert result_page.mediaBox == expected_page.mediaBox
assert result_page.extractText() == expected_page.extractText()
assert result_page.getContents() == expected_page.getContents()


@pytest.mark.django_db
Expand Down
Loading

0 comments on commit bef3b67

Please sign in to comment.