def test_listing(self, num_objects, schema): for filters in self.filters_list: self.before_each() fake_domains = list_fakes(self.fake_domain, num_objects) self.mock_query_method().return_value = fake_domains variables = {"filters": filters} if filters else None result = schema.execute(self.listing_query, variables=variables, context=self.mock_context) assert result.data == self.to_graphql_list(fake_domains) self.mock_query_method().assert_called_once_with(filters)
def test_listing(self, num_objects, schema): for variables in self.filters_list: super().before_each() fake_domains = list_fakes(self.fake_domain, num_objects) def assert_called_correctly(**kwargs): for key, val in kwargs.items(): if val: assert variables[to_camel_case(key)] == val else: assert variables.get(to_camel_case(key)) is None return fake_domains self.mock_query_method().side_effect = assert_called_correctly result = schema.execute(self.listing_query, variables=variables, context=self.mock_context) assert not result.errors assert result.data == self.to_graphql_list(fake_domains) self.mock_query_method().assert_called_once()
mock_course_presistence.create_course = MagicMock( return_value=Ok(course)) assert course_api.create_course(course.course_code).unwrap() == course mock_course_presistence.create_course.assert_called_once_with(course) def test_fail(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) course = fake_course() err = Err(fake.pystr()) mock_course_presistence.create_course = MagicMock(return_value=err) assert course_api.create_course(course.course_code) == err mock_course_presistence.create_course.assert_called_once_with(course) @pytest.mark.parametrize("filters,expected", [(None, list_fakes(fake_course, 5))]) def test_query_courses(mock_course_presistence, filters, expected): course_api = CourseApi(mock_course_presistence) mock_course_presistence.query_courses = MagicMock(return_value=expected) 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)
return_value=Ok(course)) assert course_api.create_course(course.course_code).unwrap() == course mock_course_persistence.create_course.assert_called_once_with(course) def test_fail(self, mock_course_persistence, mock_meeting_persistence): course_api = CourseApi(mock_course_persistence, mock_meeting_persistence) course = fake_course() err = Err(fake.pystr()) mock_course_persistence.create_course = MagicMock(return_value=err) assert course_api.create_course(course.course_code) == err mock_course_persistence.create_course.assert_called_once_with(course) @pytest.mark.parametrize("filters,expected", [(None, list_fakes(fake_course, 5))]) def test_query_courses(mock_course_persistence, mock_meeting_persistence, filters, expected): course_api = CourseApi(mock_course_persistence, mock_meeting_persistence) mock_course_persistence.query_courses = MagicMock(return_value=expected) assert course_api.query_courses(filters) == expected mock_course_persistence.query_courses.assert_called_once_with(filters) @pytest.mark.parametrize("expected", [list_fakes(fake_section, 5), []]) @pytest.mark.parametrize( "filters", [ {}, { "course_code": fake.pystr()
officehour(officeHourId: $officeHourId) { officeHourId } } """ result = schema.execute( query, context=context, variables={"officeHourId": str(office_hour_id)} ) assert not result.errors if expected: assert result.data["officehour"] == {"officeHourId": str(office_hour_id)} else: assert result.data["officehour"] is None @pytest.mark.parametrize("expected", [[], list_fakes(fake_officehour, 10)]) 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(
if val: assert variables[to_camel_case(key)] == val else: assert variables.get(to_camel_case(key)) is None return fake_domains self.mock_query_method().side_effect = assert_called_correctly result = schema.execute(self.listing_query, variables=variables, context=self.mock_context) assert not result.errors assert result.data == self.to_graphql_list(fake_domains) self.mock_query_method().assert_called_once() @pytest.mark.parametrize("expected", [[], list_fakes(fake_section, 10)]) 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,