From 6927cf9ba4f92442c09b4cbb6fe6226344ee07f2 Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Mon, 4 Nov 2024 13:36:21 +0100 Subject: [PATCH] Store group sets on their own table alongside Grouping.extra Duplicate the information for now until we are ready to move the group set reads to the new table. --- lms/services/group_set.py | 31 ++++++++++++++----- .../{models => services}/group_set_test.py | 31 +++++++++---------- 2 files changed, 39 insertions(+), 23 deletions(-) rename tests/unit/lms/{models => services}/group_set_test.py (58%) diff --git a/lms/services/group_set.py b/lms/services/group_set.py index 8f15fc7f85..5d791661b4 100644 --- a/lms/services/group_set.py +++ b/lms/services/group_set.py @@ -1,5 +1,8 @@ from typing import TypedDict +from lms.models.group_set import LMSGroupSet +from lms.services.upsert import bulk_upsert + class GroupSetDict(TypedDict): """ @@ -13,6 +16,9 @@ class GroupSetDict(TypedDict): class GroupSetService: + def __init__(self, db): + self._db = db + def store_group_sets(self, course, group_sets: list[dict]): """ Store this course's available group sets. @@ -25,10 +31,21 @@ def store_group_sets(self, course, group_sets: list[dict]): group_sets = [{"id": str(g["id"]), "name": g["name"]} for g in group_sets] course.extra["group_sets"] = group_sets - def get_group_sets(self, course) -> list[GroupSetDict]: - """Get this course's available group sets.""" - return course.extra.get("group_sets", []) - - -def factory(_context, _request): - return GroupSetService() + bulk_upsert( + self._db, + model_class=LMSGroupSet, + values=[ + { + "lms_id": g["id"], + "name": g["name"], + "lms_course_id": course.lms_course.id, + } + for g in group_sets + ], + index_elements=["lms_course_id", "lms_id"], + update_columns=["name", "updated"], + ) + + +def factory(_context, request): + return GroupSetService(db=request.db) diff --git a/tests/unit/lms/models/group_set_test.py b/tests/unit/lms/services/group_set_test.py similarity index 58% rename from tests/unit/lms/models/group_set_test.py rename to tests/unit/lms/services/group_set_test.py index 5bd2bb359b..9b7448d05d 100644 --- a/tests/unit/lms/models/group_set_test.py +++ b/tests/unit/lms/services/group_set_test.py @@ -2,6 +2,7 @@ import pytest +from lms.models import LMSGroupSet from lms.services.group_set import GroupSetService, factory from tests import factories @@ -19,33 +20,31 @@ class TestGroupSetService: ({"id": 1111, "name": "name"}, {"id": "1111", "name": "name"}), ], ) - def test_set_group_sets(self, group_set, expected, svc): - course = factories.Course(extra={}) + def test_set_group_sets(self, group_set, expected, svc, db_session): + course = factories.Course(extra={}, lms_course=factories.LMSCourse()) + db_session.flush() svc.store_group_sets(course, [group_set]) assert course.extra["group_sets"] == [expected] - - def test_get_group_sets(self, svc): - course = factories.Course(extra={"group_sets": sentinel.group_sets}) - - assert svc.get_group_sets(course) == sentinel.group_sets - - def test_get_group_set_empty(self, svc): - course = factories.Course(extra={}) - - assert not svc.get_group_sets(course) + assert ( + db_session.query(LMSGroupSet) + .filter_by(lms_course_id=course.lms_course.id, lms_id=str(group_set["id"])) + .one() + .name + == group_set["name"] + ) @pytest.fixture - def svc(self): - return GroupSetService() + def svc(self, db_session): + return GroupSetService(db=db_session) class TestFactory: - def test_it(self, pyramid_request, GroupSetService): + def test_it(self, pyramid_request, GroupSetService, db_session): service = factory(sentinel.context, pyramid_request) - GroupSetService.assert_called_once_with() + GroupSetService.assert_called_once_with(db=db_session) assert service == GroupSetService.return_value @pytest.fixture