Skip to content

Commit

Permalink
Store group sets on their own table alongside Grouping.extra
Browse files Browse the repository at this point in the history
Duplicate the information for now until we are ready to move the group
set reads to the new table.
  • Loading branch information
marcospri committed Nov 5, 2024
1 parent dfbb556 commit 6927cf9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
31 changes: 24 additions & 7 deletions lms/services/group_set.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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.
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from lms.models import LMSGroupSet
from lms.services.group_set import GroupSetService, factory
from tests import factories

Expand All @@ -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
Expand Down

0 comments on commit 6927cf9

Please sign in to comment.