Skip to content

Commit

Permalink
Reset translation in test tearDown (#2309)
Browse files Browse the repository at this point in the history
* Reset translation in test tearDown to fix flakiness

The TestCase tearDown implementation doesn't call super, so we need to
inherit form the Mixin first (since MRO is not followed further after
first match)

* Call super.tearDown() in derived classes
  • Loading branch information
richardebeling authored Nov 4, 2024
1 parent 853102f commit 2b88848
Show file tree
Hide file tree
Showing 23 changed files with 49 additions and 48 deletions.
3 changes: 1 addition & 2 deletions evap/contributor/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.forms.models import inlineformset_factory
from django.test import TestCase
from model_bakery import baker

from evap.contributor.forms import EditorContributionForm, EvaluationForm
from evap.evaluation.models import Contribution, Evaluation, Program, Questionnaire, UserProfile
from evap.evaluation.tests.tools import WebTest, get_form_data_from_instance
from evap.evaluation.tests.tools import TestCase, WebTest, get_form_data_from_instance
from evap.staff.forms import ContributionFormset


Expand Down
2 changes: 1 addition & 1 deletion evap/contributor/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import xlrd
from django.core import mail
from django.urls import reverse
from django_webtest import WebTest
from model_bakery import baker

from evap.evaluation.models import Contribution, Course, Evaluation, Questionnaire, UserProfile
from evap.evaluation.tests.tools import (
WebTest,
WebTestWith200Check,
create_evaluation_with_responsible_and_editor,
render_pages,
Expand Down
3 changes: 2 additions & 1 deletion evap/development/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from django.conf import settings
from django.core import management
from django.test import TestCase

from evap.evaluation.tests.tools import TestCase


class TestDumpTestDataCommand(TestCase):
Expand Down
3 changes: 1 addition & 2 deletions evap/evaluation/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from django.core import mail, management
from django.core.management import CommandError
from django.db.models import Sum
from django.test import TestCase
from django.test.utils import override_settings
from model_bakery import baker

Expand All @@ -29,7 +28,7 @@
TextAnswer,
UserProfile,
)
from evap.evaluation.tests.tools import make_manager, make_rating_answer_counters
from evap.evaluation.tests.tools import TestCase, make_manager, make_rating_answer_counters
from evap.tools import MonthAndDay


Expand Down
3 changes: 1 addition & 2 deletions evap/evaluation/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.test import TestCase
from model_bakery import baker

from evap.evaluation.forms import NewKeyForm, ProfileForm
from evap.evaluation.models import UserProfile
from evap.evaluation.tests.tools import get_form_data_from_instance
from evap.evaluation.tests.tools import TestCase, get_form_data_from_instance


class TestNewKeyForm(TestCase):
Expand Down
3 changes: 1 addition & 2 deletions evap/evaluation/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

from django.conf import settings
from django.core.management import call_command
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse
from model_bakery import baker

from evap.evaluation.models import CourseType, Program, Semester, UserProfile
from evap.evaluation.tests.tools import make_manager, submit_with_modal
from evap.evaluation.tests.tools import TestCase, make_manager, submit_with_modal
from evap.staff.tests.utils import WebTestStaffMode


Expand Down
5 changes: 3 additions & 2 deletions evap/evaluation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from django.core import mail
from django.core.cache import caches
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
from django_webtest import WebTest
from django.test import override_settings
from model_bakery import baker

from evap.evaluation.models import (
Expand All @@ -25,6 +24,8 @@
UserProfile,
)
from evap.evaluation.tests.tools import (
TestCase,
WebTest,
let_user_vote_for_evaluation,
make_contributor,
make_editor,
Expand Down
2 changes: 1 addition & 1 deletion evap/evaluation/tests/test_models_logging.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import date, datetime, timedelta

from django.test import TestCase
from django.utils.formats import localize
from model_bakery import baker

from evap.evaluation.models import Contribution, Course, Evaluation, Questionnaire, UserProfile
from evap.evaluation.models_logging import FieldAction, InstanceActionType
from evap.evaluation.tests.tools import TestCase


class TestLoggedModel(TestCase):
Expand Down
5 changes: 1 addition & 4 deletions evap/evaluation/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from django.core.exceptions import SuspiciousOperation
from django.db.models import Model, prefetch_related_objects
from django.http import Http404
from django.test.testcases import TestCase
from django.utils import translation
from model_bakery import baker

from evap.evaluation.models import Contribution, Course, Evaluation, TextAnswer, UserProfile
from evap.evaluation.tests.tools import WebTest
from evap.evaluation.tests.tools import TestCase, WebTest
from evap.evaluation.tools import (
discard_cached_related_objects,
get_object_from_dict_pk_entry_or_logged_40x,
Expand All @@ -37,8 +36,6 @@ def test_respects_stored_language(self):
self.assertEqual(user.language, "de")
self.assertEqual(translation.get_language(), "de")

translation.activate("en") # for following tests


class SaboteurError(Exception):
"""An exception class used for making sure that our mock is raising the exception and not some other unrelated code"""
Expand Down
5 changes: 1 addition & 4 deletions evap/evaluation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from django.core import mail
from django.test import override_settings
from django.urls import reverse
from django.utils import translation
from django_webtest import WebTest
from model_bakery import baker

from evap.evaluation.models import Evaluation, Question, QuestionType, Semester, UserProfile
from evap.evaluation.tests.tools import (
WebTest,
WebTestWith200Check,
create_evaluation_with_responsible_and_editor,
make_manager,
Expand Down Expand Up @@ -160,8 +159,6 @@ def test_changes_language(self):
user.refresh_from_db()
self.assertEqual(user.language, "en")

translation.activate("en") # for following tests


class TestProfileView(WebTest):
url = reverse("evaluation:profile_edit")
Expand Down
19 changes: 17 additions & 2 deletions evap/evaluation/tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from contextlib import contextmanager
from datetime import timedelta

import django.test
import django_webtest
import webtest
from django.conf import settings
from django.contrib.auth.models import Group
from django.db import DEFAULT_DB_ALIAS, connections
from django.http.request import QueryDict
from django.test.utils import CaptureQueriesContext
from django.utils import timezone
from django_webtest import WebTest
from django.utils import timezone, translation
from model_bakery import baker

from evap.evaluation.models import (
Expand All @@ -28,6 +29,20 @@
)


class ResetLanguageOnTearDownMixin:
def tearDown(self):
translation.activate("en") # Django by default does not "reset" this, causing test interdependency
super().tearDown()


class TestCase(ResetLanguageOnTearDownMixin, django.test.TestCase):
pass


class WebTest(ResetLanguageOnTearDownMixin, django_webtest.WebTest):
pass


def to_querydict(dictionary):
querydict = QueryDict(mutable=True)
for key, value in dictionary.items():
Expand Down
4 changes: 2 additions & 2 deletions evap/grades/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from django.contrib.auth.models import Group
from django.core import mail
from django.urls import reverse
from django_webtest import WebTest
from model_bakery import baker

from evap.evaluation.models import Contribution, Course, Evaluation, Questionnaire, Semester, UserProfile
from evap.evaluation.tests.tools import WebTestWith200Check
from evap.evaluation.tests.tools import WebTest, WebTestWith200Check
from evap.grades.models import GradeDocument


Expand Down Expand Up @@ -56,6 +55,7 @@ def tearDown(self):
for course in Course.objects.all():
for grade_document in course.grade_documents.all():
grade_document.file.delete()
super().tearDown()

def helper_upload_grades(self, course, final_grades):
upload_files = [("file", "grades.txt", b"Some content")]
Expand Down
3 changes: 1 addition & 2 deletions evap/results/tests/test_exporters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from io import BytesIO

import xlrd
from django.test import TestCase
from django.utils import translation
from model_bakery import baker

Expand All @@ -19,7 +18,7 @@
TextAnswer,
UserProfile,
)
from evap.evaluation.tests.tools import make_rating_answer_counters
from evap.evaluation.tests.tools import TestCase, make_rating_answer_counters
from evap.results.exporters import ResultsExporter, TextAnswerExporter
from evap.results.tools import cache_results, get_results
from evap.results.views import filter_text_answers
Expand Down
3 changes: 1 addition & 2 deletions evap/results/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.conf import settings
from django.core.cache import caches
from django.test import override_settings
from django.test.testcases import TestCase
from model_bakery import baker

from evap.evaluation.models import (
Expand All @@ -17,7 +16,7 @@
TextAnswer,
UserProfile,
)
from evap.evaluation.tests.tools import make_rating_answer_counters
from evap.evaluation.tests.tools import TestCase, make_rating_answer_counters
from evap.results.tools import (
cache_results,
calculate_average_course_distribution,
Expand Down
4 changes: 2 additions & 2 deletions evap/results/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
from django.core.management import call_command
from django.db import connection
from django.test import override_settings
from django.test.testcases import TestCase
from django.test.utils import CaptureQueriesContext
from django_webtest import WebTest
from model_bakery import baker

from evap.evaluation.models import (
Expand All @@ -25,6 +23,8 @@
UserProfile,
)
from evap.evaluation.tests.tools import (
TestCase,
WebTest,
let_user_vote_for_evaluation,
make_manager,
make_rating_answer_counters,
Expand Down
4 changes: 2 additions & 2 deletions evap/rewards/tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.test import TestCase, override_settings
from django.test import override_settings
from django.urls import reverse
from model_bakery import baker

from evap.evaluation.models import NO_ANSWER, Course, Evaluation, Question, Questionnaire, QuestionType, UserProfile
from evap.evaluation.tests.tools import WebTest
from evap.evaluation.tests.tools import TestCase, WebTest
from evap.rewards.models import RewardPointGranting, SemesterActivation
from evap.rewards.tools import reward_points_of_user

Expand Down
3 changes: 1 addition & 2 deletions evap/rewards/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from django.test import override_settings
from django.urls import reverse
from django_webtest import WebTest
from model_bakery import baker

from evap.evaluation.models import Course, Evaluation, Semester, UserProfile
from evap.evaluation.tests.tools import make_manager
from evap.evaluation.tests.tools import WebTest, make_manager
from evap.rewards.models import (
RewardPointGranting,
RewardPointRedemption,
Expand Down
3 changes: 2 additions & 1 deletion evap/staff/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import patch

from django.forms.models import inlineformset_factory
from django.test import TestCase, override_settings
from django.test import override_settings
from model_bakery import baker

from evap.contributor.forms import EvaluationForm as ContributorEvaluationForm
Expand All @@ -21,6 +21,7 @@
UserProfile,
)
from evap.evaluation.tests.tools import (
TestCase,
create_evaluation_with_responsible_and_editor,
get_form_data_from_instance,
to_querydict,
Expand Down
4 changes: 2 additions & 2 deletions evap/staff/tests/test_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.forms.models import model_to_dict
from django.test import TestCase, override_settings
from django.test import override_settings
from model_bakery import baker

import evap.staff.fixtures.excel_files_test_data as excel_data
from evap.evaluation.models import Contribution, Course, CourseType, Evaluation, Program, Semester, UserProfile
from evap.evaluation.tests.tools import assert_no_database_modifications
from evap.evaluation.tests.tools import TestCase, assert_no_database_modifications
from evap.staff.importers import (
ImporterLog,
ImporterLogEntry,
Expand Down
4 changes: 1 addition & 3 deletions evap/staff/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
from unittest.mock import MagicMock, patch

from django.contrib.auth.models import Group
from django.test import TestCase
from django.utils.html import escape
from django_webtest import WebTest
from model_bakery import baker
from openpyxl import load_workbook

from evap.evaluation.models import Contribution, Course, Evaluation, UserProfile
from evap.evaluation.tests.tools import assert_no_database_modifications
from evap.evaluation.tests.tools import TestCase, WebTest, assert_no_database_modifications
from evap.rewards.models import RewardPointGranting, RewardPointRedemption
from evap.staff.fixtures.excel_files_test_data import (
create_memory_csv_file,
Expand Down
5 changes: 3 additions & 2 deletions evap/staff/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
from django.db.models import Model
from django.http import HttpResponse
from django.test import override_settings
from django.test.testcases import TestCase
from django.urls import reverse
from django.utils import translation
from django_webtest import WebTest
from model_bakery import baker

import evap.staff.fixtures.excel_files_test_data as excel_data
Expand All @@ -41,6 +39,8 @@
)
from evap.evaluation.tests.tools import (
FuzzyInt,
TestCase,
WebTest,
assert_no_database_modifications,
create_evaluation_with_responsible_and_editor,
let_user_vote_for_evaluation,
Expand Down Expand Up @@ -2379,6 +2379,7 @@ def setUpTestData(cls):
def tearDown(self):
# delete the uploaded file again so other tests can start with no file guaranteed
helper_delete_all_import_files(self.manager.id)
super().tearDown()

def test_import_valid_participants_file(self):
page = self.app.get(self.url, user=self.manager)
Expand Down
4 changes: 1 addition & 3 deletions evap/staff/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import time
from contextlib import contextmanager

from django_webtest import WebTest

from evap.evaluation.tests.tools import WebTestWith200Check
from evap.evaluation.tests.tools import WebTest, WebTestWith200Check
from evap.staff.tools import ImportType, generate_import_filename


Expand Down
3 changes: 1 addition & 2 deletions evap/student/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from functools import partial

from django.test.utils import override_settings
from django_webtest import WebTest
from model_bakery import baker

from evap.evaluation.models import (
Expand All @@ -19,7 +18,7 @@
UserProfile,
VoteTimestamp,
)
from evap.evaluation.tests.tools import FuzzyInt, WebTestWith200Check, render_pages
from evap.evaluation.tests.tools import FuzzyInt, WebTest, WebTestWith200Check, render_pages
from evap.student.tools import answer_field_id
from evap.student.views import SUCCESS_MAGIC_STRING

Expand Down

0 comments on commit 2b88848

Please sign in to comment.