def test_retrieve_with_sorting_flag(self, order_courses_by_start_date): """ Verify the number of queries is the same with sorting flag set to true. """ course_list = CourseFactory.create_batch(3) for course in course_list: CourseRunFactory(course=course) program = ProgramFactory(courses=course_list, order_courses_by_start_date=order_courses_by_start_date) with self.assertNumQueries(90): self.assert_retrieve_success(program) self.assertEqual(course_list, list(program.courses.all())) # pylint: disable=no-member
def test_list_query(self): """ Verify the endpoint returns a filtered list of courses """ title = 'Some random title' courses = CourseFactory.create_batch(3, title=title) courses = sorted(courses, key=lambda course: course.key.lower()) query = 'title:' + title url = '{root}?q={query}'.format(root=reverse('api:v1:course-list'), query=query) response = self.client.get(url) self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True))
def test_validate(self): """ Validate that there are no integration issues. """ courses = CourseFactory.create_batch(3) course_metadata_validator = CourseMetadataProviderValidator( [str(course.uuid) for course in courses]) # Run all the validations, note that an assertion error will be raised if any of the validation fail. course_metadata_validator.validate()
def test_list_uuid_filter(self): """ Verify the endpoint returns a list of courses filtered by the specified uuid. """ courses = CourseFactory.create_batch(3, partner=self.partner) courses = sorted(courses, key=lambda course: course.key.lower()) uuids = ','.join([str(course.uuid) for course in courses]) url = '{root}?uuids={uuids}'.format(root=reverse('api:v1:course-list'), uuids=uuids) with self.assertNumQueries(46): response = self.client.get(url) self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True))
def test_list_query(self): """ Verify the endpoint returns a filtered list of courses """ title = 'Some random title' courses = CourseFactory.create_batch(3, title=title) courses = sorted(courses, key=lambda course: course.key.lower()) query = 'title:' + title url = '{root}?q={query}'.format(root=reverse('api:v1:course-list'), query=query) with self.assertNumQueries(47): response = self.client.get(url) self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True))
def setUp(self): super(AutocompleteTests, self).setUp() self.user = UserFactory(is_staff=True) self.client.login(username=self.user.username, password=USER_PASSWORD) self.courses = CourseFactory.create_batch(3, title='Some random course title') for course in self.courses: CourseRunFactory(course=course) self.organizations = OrganizationFactory.create_batch(3) first_instructor = PersonFactory(given_name="First Instructor") second_instructor = PersonFactory(given_name="Second Instructor") self.instructors = [first_instructor, second_instructor]
def test_retrieve_with_sorting_flag(self, order_courses_by_start_date): """ Verify the number of queries is the same with sorting flag set to true. """ course_list = CourseFactory.create_batch(3) for course in course_list: CourseRunFactory(course=course) program = ProgramFactory( courses=course_list, order_courses_by_start_date=order_courses_by_start_date) with self.assertNumQueries(26): response = self.assert_retrieve_success(program) assert response.data == self.serialize_program(program) self.assertEqual(course_list, list(program.courses.all())) # pylint: disable=no-member
def test_list_key_filter(self): """ Verify the endpoint returns a list of courses filtered by the specified keys. """ courses = CourseFactory.create_batch(3) courses = sorted(courses, key=lambda course: course.key.lower()) keys = ','.join([course.key for course in courses]) url = '{root}?keys={keys}'.format(root=reverse('api:v1:course-list'), keys=keys) with self.assertNumQueries(41): response = self.client.get(url) self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True))
def test_course_ordering_with_exclusions(self): """ Verify that excluded course runs aren't used when ordering courses. """ request = make_request() course_list = CourseFactory.create_batch(3) # Create a course run with arbitrary start and empty enrollment_start. # This run will be excluded from the program. If it wasn't excluded, # the expected course ordering, by index, would be: 0, 2, 1. excluded_run = CourseRunFactory( course=course_list[0], enrollment_start=None, start=datetime(2014, 1, 1), ) # Create a run with later start and empty enrollment_start. CourseRunFactory( course=course_list[2], enrollment_start=None, start=datetime(2014, 2, 1), ) # Create a run with matching start, but later enrollment_start. CourseRunFactory( course=course_list[1], enrollment_start=datetime(2014, 1, 2), start=datetime(2014, 2, 1), ) # Create a run with later start and enrollment_start. CourseRunFactory( course=course_list[0], enrollment_start=datetime(2014, 2, 1), start=datetime(2014, 3, 1), ) program = ProgramFactory(courses=course_list, excluded_course_runs=[excluded_run]) serializer = self.serializer_class(program, context={'request': request}) expected = MinimalProgramCourseSerializer( # The expected ordering is the reverse of course_list. course_list[::-1], many=True, context={ 'request': request, 'program': program, 'course_runs': list(program.course_runs) }).data self.assertEqual(serializer.data['courses'], expected)
def test_course_autocomplete(self, admin_client): """ Verify course autocomplete returns the data. """ courses = CourseFactory.create_batch(3) path = reverse('admin_metadata:course-autocomplete') response = admin_client.get(path) data = json.loads(response.content.decode('utf-8')) assert response.status_code == 200 assert len(data['results']) == 3 # Search for substrings of course keys and titles course = courses[0] self.assert_valid_query_result(admin_client, path, course.key[12:], course) self.assert_valid_query_result(admin_client, path, course.title[12:], course)
def test_data(self): user = UserFactory() catalog = CatalogFactory(query='*:*', viewers=[user]) # We intentionally use a query for all Courses. courses = CourseFactory.create_batch(10) serializer = CatalogSerializer(catalog) expected = { 'id': catalog.id, 'name': catalog.name, 'query': catalog.query, 'courses_count': len(courses), 'viewers': [user.username] } self.assertDictEqual(serializer.data, expected)
def test_retrieve_with_sorting_flag(self, order_courses_by_start_date, django_assert_num_queries): """ Verify the number of queries is the same with sorting flag set to true. """ course_list = CourseFactory.create_batch(3, partner=self.partner) for course in course_list: CourseRunFactory(course=course) program = ProgramFactory( courses=course_list, order_courses_by_start_date=order_courses_by_start_date, partner=self.partner) # property does not have the right values while being indexed del program._course_run_weeks_to_complete with django_assert_num_queries(29): response = self.assert_retrieve_success(program) assert response.data == self.serialize_program(program) assert course_list == list(program.courses.all()) # pylint: disable=no-member
def test_course_ordering_with_exclusions(self): """ Verify that excluded course runs aren't used when ordering courses. """ request = make_request() course_list = CourseFactory.create_batch(3) # Create a course run with arbitrary start and empty enrollment_start. # This run will be excluded from the program. If it wasn't excluded, # the expected course ordering, by index, would be: 0, 2, 1. excluded_run = CourseRunFactory( course=course_list[0], enrollment_start=None, start=datetime(2014, 1, 1), ) # Create a run with later start and empty enrollment_start. CourseRunFactory( course=course_list[2], enrollment_start=None, start=datetime(2014, 2, 1), ) # Create a run with matching start, but later enrollment_start. CourseRunFactory( course=course_list[1], enrollment_start=datetime(2014, 1, 2), start=datetime(2014, 2, 1), ) # Create a run with later start and enrollment_start. CourseRunFactory( course=course_list[0], enrollment_start=datetime(2014, 2, 1), start=datetime(2014, 3, 1), ) program = ProgramFactory(courses=course_list, excluded_course_runs=[excluded_run]) serializer = self.serializer_class(program, context={'request': request}) expected = MinimalProgramCourseSerializer( # The expected ordering is the reverse of course_list. course_list[::-1], many=True, context={'request': request, 'program': program, 'course_runs': list(program.course_runs)} ).data self.assertEqual(serializer.data['courses'], expected)
def test_data(self): user = UserFactory() catalog = CatalogFactory( query='*:*', viewers=[user]) # We intentionally use a query for all Courses. courses = CourseFactory.create_batch(10) serializer = CatalogSerializer(catalog) expected = { 'id': catalog.id, 'name': catalog.name, 'query': catalog.query, 'courses_count': len(courses), 'viewers': [user.username] } self.assertDictEqual(serializer.data, expected)
def test_course_ordering(self): """ Verify that courses in a program are ordered by ascending run start date, with ties broken by earliest run enrollment start date. """ request = make_request() course_list = CourseFactory.create_batch(3) # Create a course run with arbitrary start and empty enrollment_start. CourseRunFactory( course=course_list[2], enrollment_start=None, start=datetime(2014, 2, 1), ) # Create a second run with matching start, but later enrollment_start. CourseRunFactory( course=course_list[1], enrollment_start=datetime(2014, 1, 2), start=datetime(2014, 2, 1), ) # Create a third run with later start and enrollment_start. CourseRunFactory( course=course_list[0], enrollment_start=datetime(2014, 2, 1), start=datetime(2014, 3, 1), ) program = ProgramFactory(courses=course_list) serializer = self.serializer_class(program, context={'request': request}) expected = MinimalProgramCourseSerializer( # The expected ordering is the reverse of course_list. course_list[::-1], many=True, context={ 'request': request, 'program': program, 'course_runs': list(program.course_runs) }).data self.assertEqual(serializer.data['courses'], expected)
def create_program(self): organizations = OrganizationFactory.create_batch(2) person = PersonFactory() courses = CourseFactory.create_batch(3) for course in courses: CourseRunFactory.create_batch(2, course=course, staff=[person], start=datetime.now()) return ProgramFactory( courses=courses, authoring_organizations=organizations, credit_backing_organizations=organizations, corporate_endorsements=CorporateEndorsementFactory.create_batch(1), individual_endorsements=EndorsementFactory.create_batch(1), expected_learning_items=ExpectedLearningItemFactory.create_batch(1), job_outlook_items=JobOutlookItemFactory.create_batch(1), banner_image=make_image_file('test_banner.jpg'), video=VideoFactory(), order_courses_by_start_date=False, )
def test_course_ordering(self): """ Verify that courses in a program are ordered by ascending run start date, with ties broken by earliest run enrollment start date. """ request = make_request() course_list = CourseFactory.create_batch(3) # Create a course run with arbitrary start and empty enrollment_start. CourseRunFactory( course=course_list[2], enrollment_start=None, start=datetime(2014, 2, 1), ) # Create a second run with matching start, but later enrollment_start. CourseRunFactory( course=course_list[1], enrollment_start=datetime(2014, 1, 2), start=datetime(2014, 2, 1), ) # Create a third run with later start and enrollment_start. CourseRunFactory( course=course_list[0], enrollment_start=datetime(2014, 2, 1), start=datetime(2014, 3, 1), ) program = ProgramFactory(courses=course_list) serializer = self.serializer_class(program, context={'request': request}) expected = MinimalProgramCourseSerializer( # The expected ordering is the reverse of course_list. course_list[::-1], many=True, context={'request': request, 'program': program, 'course_runs': list(program.course_runs)} ).data self.assertEqual(serializer.data['courses'], expected)
def create_program(self): organizations = OrganizationFactory.create_batch(2) person = PersonFactory() courses = CourseFactory.create_batch(3) for course in courses: CourseRunFactory.create_batch(2, course=course, staff=[person], start=datetime.now()) return ProgramFactory( courses=courses, authoring_organizations=organizations, credit_backing_organizations=organizations, corporate_endorsements=CorporateEndorsementFactory.create_batch(1), individual_endorsements=EndorsementFactory.create_batch(1), expected_learning_items=ExpectedLearningItemFactory.create_batch( 1), job_outlook_items=JobOutlookItemFactory.create_batch(1), banner_image=make_image_file('test_banner.jpg'), video=VideoFactory(), order_courses_by_start_date=False, )