def test_success(self, course_persistence, instructor_persistence): section = fake_section() course_persistence.create_course(section.course) instructor_persistence.create_instructor(section.taught_by, "aaaaa") assert course_persistence.create_section(section).unwrap() == section assert course_persistence.get_section( section.identity).unwrap() == section course_persistence.delete_section(section.identity) course_persistence.delete_course(section.course.course_code) instructor_persistence.delete_instructor(section.taught_by)
def test_fail(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) section = fake_section() err = Err(fake.pystr()) mock_course_presistence.create_section = MagicMock(return_value=err) assert (course_api.create_section( section.course, section.year, section.semester, section.section_code, section.taught_by, section.num_students, ) == err) mock_course_presistence.create_section.assert_called_once_with(section)
def test_success(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) section = fake_section() mock_course_presistence.create_section = MagicMock( return_value=Ok(section)) assert (course_api.create_section( section.course, section.year, section.semester, section.section_code, section.taught_by, section.num_students, ).unwrap() == section) mock_course_presistence.create_section.assert_called_once_with(section)
def test_create_section_fail(schema, mock_context, create_section_query): context, course_api, instructor_api = mock_context section = fake_section() context.user = section.taught_by error = fake.pystr() course_api.create_section = MagicMock(return_value=Err(error)) result = schema.execute(create_section_query, variables=section_input(section), context=context) assert error in str(result.errors[0]) course_api.create_section.assert_called_once_with( section.course, section.year, section.semester, section.section_code, section.taught_by, section.num_students, )
def test_enroll_students(schema, mock_context, student_numbers): context, course_api, instructor_api = mock_context section = fake_section() course_api.enroll_students.return_value = student_numbers query = """ mutation enroll($sectionInput: SectionInput!, $students: [String]!) { enrollStudents(sectionInput: $sectionInput, studentNumbers: $students) { studentNumbers } } """ variables = section_input(section) variables["students"] = student_numbers result = schema.execute(query, context=context, variables=variables) assert not result.errors assert result.data["enrollStudents"]["studentNumbers"] == student_numbers course_api.enroll_students.assert_called_once_with(section.identity, student_numbers)
def test_create_section(schema, mock_context, create_section_query, supply_instructor): context, course_api, instructor_api = mock_context section = fake_section() if supply_instructor: instructor_api.get_instructor = MagicMock( return_value=Some(section.taught_by)) else: context.user = section.taught_by course_api.create_section = MagicMock(return_value=Ok(section)) variables = section_input(section) if supply_instructor: variables["sectionInput"]["taughtBy"] = section.taught_by.user_name result = schema.execute(create_section_query, variables=variables, context=context) assert not result.errors assert result.data == { "createSection": { "course": { "courseCode": section.course.course_code }, "year": section.year, "semester": section.semester.name, "taughtBy": { "userName": section.taught_by.user_name, "firstName": section.taught_by.first_name, "lastName": section.taught_by.last_name, }, "numStudents": section.num_students, "sectionCode": section.section_code, } } course_api.create_section.assert_called_once_with( section.course, section.year, section.semester, section.section_code, section.taught_by, section.num_students, )
def test_officehour_listing(schema, expected): context = MagicMock() context.api.course_api.get_officehours_for_section_on_weekday.return_value = ( expected ) weekday = choice(list(Weekday)) section = fake_section() query = """ query getOfficeHours($sectionInput: SectionInput!, $weekday: Weekday!) { officehours(sectionInput: $sectionInput, weekday: $weekday) { officeHourId } } """ result = schema.execute( query, context=context, variables={ "sectionInput": { "course": {"courseCode": section.course.course_code}, "year": section.year, "semester": section.semester.name, "sectionCode": section.section_code, "taughtBy": section.taught_by.user_name, "numStudents": section.num_students, }, "weekday": weekday.name, }, ) assert not result.errors assert result.data["officehours"] == [ {"officeHourId": str(officehour.office_hour_id)} for officehour in expected ] context.api.course_api.get_officehours_for_section_on_weekday.assert_called_once_with( section.identity, weekday )
def fake_domain(self): return fake_section()
assert course_api.query_courses(filters) == expected mock_course_presistence.query_courses.assert_called_once_with(filters) @pytest.mark.parametrize("filters,expected", [(None, list_fakes(fake_section, 5))]) def test_query_sections(mock_course_presistence, filters, expected): course_api = CourseApi(mock_course_presistence) mock_course_presistence.query_sections = MagicMock(return_value=expected) assert course_api.query_sections(filters) == expected mock_course_presistence.query_sections.assert_called_once_with(filters) @pytest.mark.parametrize("expected", [NONE, Some(fake_course())]) def test_get_course(mock_course_presistence, expected): course_api = CourseApi(mock_course_presistence) mock_course_presistence.get_course = MagicMock(return_value=expected) course_code = expected.map_or(lambda c: c.course_code, None) assert course_api.get_course(course_code) == expected mock_course_presistence.get_course.assert_called_once_with(course_code) @pytest.mark.parametrize("expected", [NONE, Some(fake_section())]) def test_get_section(mock_course_presistence, expected): course_api = CourseApi(mock_course_presistence) expected_identity = expected.map_or(lambda section: section.identity, None) mock_course_presistence.get_section = MagicMock(return_value=expected) assert course_api.get_section(expected_identity) == expected mock_course_presistence.get_section.assert_called_once_with( expected_identity)
def test_duplicate(self, course_persistence): section = fake_section() course_persistence.get_section = MagicMock(return_value=Some(section)) assert (course_persistence.create_section(section).unwrap_err() == f"Section {section} already exists")
def test_invalid_course(self, course_persistence): section = fake_section() assert (course_persistence.create_section(section).unwrap_err() == f"Course {section.course} does not exist") assert course_persistence.get_section(section.identity).is_none