def test_success(self, student_persistence): student = fake_student() assert student_persistence.create_student(student, "aaaa").unwrap() == student assert (student_persistence.get_student( student.student_number).unwrap() == student) assert (student_persistence.get_password_hash( student.student_number).unwrap() == "aaaa") student_persistence.delete_student(student)
def test_update_hash(self, student_persistence): student = fake_student() student_persistence.create_student(student, "aaaa") assert (student_persistence.get_password_hash( student.student_number).unwrap() == "aaaa") assert (student_persistence.update_password_hash( student.student_number, "bbbb").unwrap() is None) assert (student_persistence.get_password_hash( student.student_number).unwrap() == "bbbb") student_persistence.delete_student(student)
def fake_meeting() -> Iterator[Meeting]: while True: start_time = fake.date_time() yield Meeting( meeting_id=uuid4(), office_hour_id=uuid4(), index=abs(fake.pyint()), instructor=fake_instructor(), student=fake_student(), notes=[], comments=[], start_time=int(start_time.timestamp()), )
def test_query_one(self, schema): meeting = next(fake_meeting()) meeting.comments.append(next(fake_comment(fake_student()))) meeting.comments.append(next(fake_comment(fake_instructor()))) context = MagicMock() context.api.meeting_api.get_meeting.return_value = Some(meeting) result = schema.execute( self.query_one, context=context, variables={"meetingId": str(meeting.meeting_id)}, ) assert not result.errors context.api.meeting_api.get_meeting.assert_called_once_with( meeting.meeting_id) assert result.data["meeting"]["meetingId"] == str(meeting.meeting_id)
def test_enrolled_in(schema, expected): context = MagicMock() user = fake_student() context.user = user context.api.course_api.get_sections_of_student.return_value = expected query = """ query enroll { enrolledIn { sectionCode } } """ result = schema.execute(query, context=context) assert not result.errors for expected_section, actual_section in zip(expected, result.data["enrolledIn"]): assert expected_section.section_code == actual_section["sectionCode"]
def test_duplicate(self, student_persistence): student = fake_student() student_persistence.get_student = MagicMock(return_value=Some(student)) assert (student_persistence.create_student( student, "aaaa").unwrap_err() == f"Student {student} already exists")
class TestMeetingQuery: meeting_fragment = """ fragment meetingDetails on Meeting { meetingId officeHourId index instructor { userName firstName lastName } student { studentNumber firstName lastName } notes { noteId meetingId timeStamp contentText } comments { commentId meetingId author { ... on Instructor { userName firstName lastName } ... on Student { studentNumber firstName lastName } } timeStamp contentText } startTime } """ query_one = (""" query getMeeting($meetingId: UUID!) { meeting(meetingId: $meetingId) { ... meetingDetails } } """ + meeting_fragment) query_upcoming = (""" query getUpcoming { upcomingMeetings { ... meetingDetails } } """ + meeting_fragment) def test_query_one(self, schema): meeting = next(fake_meeting()) meeting.comments.append(next(fake_comment(fake_student()))) meeting.comments.append(next(fake_comment(fake_instructor()))) context = MagicMock() context.api.meeting_api.get_meeting.return_value = Some(meeting) result = schema.execute( self.query_one, context=context, variables={"meetingId": str(meeting.meeting_id)}, ) assert not result.errors context.api.meeting_api.get_meeting.assert_called_once_with( meeting.meeting_id) assert result.data["meeting"]["meetingId"] == str(meeting.meeting_id) @pytest.mark.parametrize("user", [fake_instructor(), fake_student()]) @pytest.mark.parametrize("amount", [0, 10]) def test_query_upcoming(self, schema, user, amount): meetings = [ meeting for meeting, _ in zip(fake_meeting(), range(amount)) ] context = MagicMock() context.user = user if isinstance(user, Instructor): api_method = context.api.meeting_api.get_meetings_of_instructor user_id = user.user_name else: api_method = context.api.meeting_api.get_meetings_of_student user_id = user.student_number api_method.return_value = meetings result = schema.execute(self.query_upcoming, context=context) assert not result.errors for gql_result, meeting in zip(result.data["upcomingMeetings"], meetings): assert gql_result["meetingId"] == str(meeting.meeting_id) api_method.assert_called_once_with(user_id)
@pytest.fixture() def meeting_api(): return MeetingApi(MagicMock(MeetingPersistence)) @pytest.mark.parametrize("user_type", ["instructor", "student"]) def test_check_meeting_user(meeting_api, user_type): meeting = next(fake_meeting()) user = meeting.instructor if user_type == "instructor" else meeting.student meeting_api.meeting_persistence.get_meeting.return_value = Some(meeting) assert meeting_api._check_meeting_user(meeting.meeting_id, user, "").is_ok meeting_api.meeting_persistence.get_meeting.assert_called_once_with( meeting.meeting_id) @pytest.mark.parametrize("user", [fake_student(), fake_instructor()]) def test_check_meeting_user_not_permitted(meeting_api, user): meeting = next(fake_meeting()) meeting_api.meeting_persistence.get_meeting.return_value = Some(meeting) error = fake.pystr() assert (meeting_api._check_meeting_user(meeting.meeting_id, user, error).unwrap_err() == error) meeting_api.meeting_persistence.get_meeting.assert_called_once_with( meeting.meeting_id) @pytest.mark.parametrize("user", [fake_student(), fake_instructor()]) def test_check_meeting_user_not_found(meeting_api, user): id_ = uuid4() meeting_api.meeting_persistence.get_meeting.return_value = NONE error = fake.pystr()