Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get_assignments tests to exercise all parameters independently and combined #6437

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 44 additions & 45 deletions tests/unit/lms/services/assignment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from h_matchers import Any

from lms.models import AssignmentGrouping, AssignmentMembership, RoleScope
from lms.models import AssignmentGrouping, AssignmentMembership, RoleScope, RoleType
from lms.services.assignment import AssignmentService, factory
from tests import factories

Expand Down Expand Up @@ -239,55 +239,52 @@ def test_is_member(self, svc, db_session):
assert svc.is_member(assignment, user.h_userid)
assert not svc.is_member(assignment, other_user.h_userid)

def test_get_assignments(self, svc, db_session):
assert db_session.scalars(svc.get_assignments()).all()

def test_get_assignments_by_course(self, svc, db_session, assignment):
@pytest.mark.parametrize("instructor_h_userid", [True, False])
@pytest.mark.parametrize("course_id", [True, False])
@pytest.mark.parametrize("h_userids", [True, False])
def test_get_assignments(
self,
svc,
db_session,
instructor_h_userid,
assignment,
with_assignment_noise,
course_id,
h_userids,
):
factories.User()
course = factories.Course()
factories.AssignmentGrouping.create(assignment=assignment, grouping=course)
db_session.flush()

assert db_session.scalars(svc.get_assignments(course_id=course.id)).all() == [
assignment
]

def test_get_assignments_with_instructor_h_userid(self, svc, db_session):
factories.User() # User not in assignment
assignment = factories.Assignment()
user = factories.User()
lti_role = factories.LTIRole(scope=RoleScope.COURSE)
lti_role = factories.LTIRole(scope=RoleScope.COURSE, type=RoleType.INSTRUCTOR)
factories.AssignmentMembership.create(
assignment=assignment, user=user, lti_role=lti_role
)
# Other membership record, with a different role
factories.AssignmentMembership.create(
assignment=assignment, user=user, lti_role=factories.LTIRole()
)

factories.AssignmentGrouping.create(assignment=assignment, grouping=course)
db_session.flush()

assert db_session.scalars(svc.get_assignments(user.h_userid)).all() == [
assignment
]
query_parameters = {}

def test_get_assignments_with_h_userids(self, svc, db_session):
factories.User() # User not in assignment
assignment = factories.Assignment()
user = factories.User()
lti_role = factories.LTIRole(scope=RoleScope.COURSE)
factories.AssignmentMembership.create(
assignment=assignment, user=user, lti_role=lti_role
)
# Other membership record, with a different role
factories.AssignmentMembership.create(
assignment=assignment, user=user, lti_role=factories.LTIRole()
)
if instructor_h_userid:
query_parameters["instructor_h_userid"] = user.h_userid

db_session.flush()
if course_id:
query_parameters["course_id"] = course.id

assert db_session.scalars(
svc.get_assignments(h_userids=[user.h_userid])
).all() == [assignment]
if h_userids:
query_parameters["h_userids"] = [user.h_userid]

query = svc.get_assignments(**query_parameters)

if not query_parameters:
Copy link
Member Author

@marcospri marcospri Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve the assert here, check it matches the assignment created in this test (assignment) + the other ones in the DB (with_assignment_noise).

assert set(db_session.scalars(query).all()) == set(
[assignment] + with_assignment_noise
)

else:
assert db_session.scalars(query).all() == [assignment]

def test_get_assignments_by_course_id_with_duplicate(self, db_session, svc):
course = factories.Course()
Expand Down Expand Up @@ -357,14 +354,16 @@ def non_matching_params(self, request, matching_params):

@pytest.fixture(autouse=True)
def with_assignment_noise(self, assignment):
factories.Assignment(
tool_consumer_instance_guid=assignment.tool_consumer_instance_guid,
resource_link_id="noise_resource_link_id",
)
factories.Assignment(
tool_consumer_instance_guid="noise_tool_consumer_instance_guid",
resource_link_id=assignment.resource_link_id,
)
return [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixture now not only creates the assignments but returns them. I reckon all fixtures should return the values they create even if not immediately used.

factories.Assignment(
tool_consumer_instance_guid=assignment.tool_consumer_instance_guid,
resource_link_id="noise_resource_link_id",
),
factories.Assignment(
tool_consumer_instance_guid="noise_tool_consumer_instance_guid",
resource_link_id=assignment.resource_link_id,
),
]

@pytest.fixture
def create_assignment(self, svc):
Expand Down
Loading