From 8a7d3d93be1a7c4f7f037a7495b7c8068fe8e0c4 Mon Sep 17 00:00:00 2001 From: jefer94 Date: Mon, 8 Jul 2024 22:30:29 -0500 Subject: [PATCH] add two actions to the admin --- breathecode/mentorship/admin.py | 15 ++- .../mentorship/tests/admin/tests_use_daily.py | 99 +++++++++++++++++++ .../tests/admin/tests_use_google_meet.py | 99 +++++++++++++++++++ 3 files changed, 208 insertions(+), 5 deletions(-) create mode 100644 breathecode/mentorship/tests/admin/tests_use_daily.py create mode 100644 breathecode/mentorship/tests/admin/tests_use_google_meet.py diff --git a/breathecode/mentorship/admin.py b/breathecode/mentorship/admin.py index 4de35e654..10ccb4c30 100644 --- a/breathecode/mentorship/admin.py +++ b/breathecode/mentorship/admin.py @@ -30,16 +30,20 @@ logger = logging.getLogger(__name__) +def use_google_meet(modeladmin, request, queryset): + queryset.update(video_provider="GOOGLE_MEET") + + +def use_daily(modeladmin, request, queryset): + queryset.update(video_provider="DAILY") + + @admin.register(MentorshipService) class ServiceAdmin(admin.ModelAdmin): list_display = ["slug", "name", "status", "academy"] search_fields = ["slug", "name"] list_filter = ["academy__slug", "status"] - # raw_id_fields = ['academy', 'github_user'] - # actions = [sync_issues, generate_bill] - - # def full_name(self, obj): - # return obj.user.first_name + ' ' + obj.user.last_name + actions = [use_google_meet, use_daily] class MentorForm(forms.ModelForm): @@ -361,3 +365,4 @@ class AcademyMentorshipSettingsAdmin(admin.ModelAdmin): "video_provider", ) list_filter = ["academy", "language", "allow_mentee_to_extend", "allow_mentors_to_extend", "video_provider"] + actions = [use_google_meet, use_daily] diff --git a/breathecode/mentorship/tests/admin/tests_use_daily.py b/breathecode/mentorship/tests/admin/tests_use_daily.py new file mode 100644 index 000000000..e431a0b97 --- /dev/null +++ b/breathecode/mentorship/tests/admin/tests_use_daily.py @@ -0,0 +1,99 @@ +""" +Test mentorships +""" + +import pytest + +from breathecode.mentorship.admin import use_daily +from capyc.rest_framework import pytest as capy + + +@pytest.mark.fixture(autouse=True) +def setup(db): + yield + + +@pytest.mark.parametrize("model_path", ["mentorship.MentorshipService", "mentorship.AcademyMentorshipSettings"]) +def test_no_items(database: capy.Database, model_path: str): + Model = database.get_model(model_path) + queryset = Model.objects.filter() + + use_daily(None, None, queryset) + + assert database.list_of(model_path) == [] + + +@pytest.mark.django_db(reset_sequences=True) +@pytest.mark.parametrize( + "model_path, model_name, includes_academy", + [ + ("mentorship.MentorshipService", "mentorship_service", False), + ("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True), + ], +) +def test_two_items__changed( + database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool +): + extra = { + "city": 1, + "country": 1, + } + + if includes_academy: + extra["academy"] = (2, {"name": "Test Academy"}) + extra[model_name] = [{"video_provider": "GOOGLE_MEET", "academy_id": n + 1} for n in range(2)] + + else: + extra[model_name] = (2, {"video_provider": "GOOGLE_MEET"}) + + model = database.create(**extra) + Model = database.get_model(model_path) + queryset = Model.objects.filter() + + use_daily(None, None, queryset) + + assert database.list_of(model_path) == [ + { + **format.to_obj_repr(model[model_name][n]), + "video_provider": "DAILY", + } + for n in range(2) + ] + + +@pytest.mark.django_db(reset_sequences=True) +@pytest.mark.parametrize( + "model_path, model_name, includes_academy", + [ + ("mentorship.MentorshipService", "mentorship_service", False), + ("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True), + ], +) +def test_two_items__did_not_changed( + database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool +): + extra = { + "city": 1, + "country": 1, + } + + if includes_academy: + extra["academy"] = (2, {"name": "Test Academy"}) + extra[model_name] = [{"video_provider": "DAILY", "academy_id": n + 1} for n in range(2)] + + else: + extra[model_name] = (2, {"video_provider": "DAILY"}) + + model = database.create(**extra) + Model = database.get_model(model_path) + queryset = Model.objects.filter() + + use_daily(None, None, queryset) + + assert database.list_of(model_path) == [ + { + **format.to_obj_repr(model[model_name][n]), + "video_provider": "DAILY", + } + for n in range(2) + ] diff --git a/breathecode/mentorship/tests/admin/tests_use_google_meet.py b/breathecode/mentorship/tests/admin/tests_use_google_meet.py new file mode 100644 index 000000000..14efe614b --- /dev/null +++ b/breathecode/mentorship/tests/admin/tests_use_google_meet.py @@ -0,0 +1,99 @@ +""" +Test mentorships +""" + +import pytest + +from breathecode.mentorship.admin import use_google_meet +from capyc.rest_framework import pytest as capy + + +@pytest.mark.fixture(autouse=True) +def setup(db): + yield + + +@pytest.mark.parametrize("model_path", ["mentorship.MentorshipService", "mentorship.AcademyMentorshipSettings"]) +def test_no_items(database: capy.Database, model_path: str): + Model = database.get_model(model_path) + queryset = Model.objects.filter() + + use_google_meet(None, None, queryset) + + assert database.list_of(model_path) == [] + + +@pytest.mark.django_db(reset_sequences=True) +@pytest.mark.parametrize( + "model_path, model_name, includes_academy", + [ + ("mentorship.MentorshipService", "mentorship_service", False), + ("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True), + ], +) +def test_two_items__changed( + database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool +): + extra = { + "city": 1, + "country": 1, + } + + if includes_academy: + extra["academy"] = (2, {"name": "Test Academy"}) + extra[model_name] = [{"video_provider": "DAILY", "academy_id": n + 1} for n in range(2)] + + else: + extra[model_name] = (2, {"video_provider": "DAILY"}) + + model = database.create(**extra) + Model = database.get_model(model_path) + queryset = Model.objects.filter() + + use_google_meet(None, None, queryset) + + assert database.list_of(model_path) == [ + { + **format.to_obj_repr(model[model_name][n]), + "video_provider": "GOOGLE_MEET", + } + for n in range(2) + ] + + +@pytest.mark.django_db(reset_sequences=True) +@pytest.mark.parametrize( + "model_path, model_name, includes_academy", + [ + ("mentorship.MentorshipService", "mentorship_service", False), + ("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True), + ], +) +def test_two_items__did_not_changed( + database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool +): + extra = { + "city": 1, + "country": 1, + } + + if includes_academy: + extra["academy"] = (2, {"name": "Test Academy"}) + extra[model_name] = [{"video_provider": "GOOGLE_MEET", "academy_id": n + 1} for n in range(2)] + + else: + extra[model_name] = (2, {"video_provider": "GOOGLE_MEET"}) + + model = database.create(**extra) + Model = database.get_model(model_path) + queryset = Model.objects.filter() + + use_google_meet(None, None, queryset) + + assert database.list_of(model_path) == [ + { + **format.to_obj_repr(model[model_name][n]), + "video_provider": "GOOGLE_MEET", + } + for n in range(2) + ]