Esempio n. 1
0
    def test_courses_with_include_archived(self):
        """
        Verify the endpoint returns the list of available and archived courses if include archived
        is True in catalog.
        """
        url = reverse('api:v1:catalog-courses', kwargs={'id': self.catalog.id})
        Course.objects.all().delete()

        now = datetime.datetime.now(pytz.UTC)
        future = now + datetime.timedelta(days=30)
        past = now - datetime.timedelta(days=30)

        course_run = CourseRunFactory.create(
            course__title='ABC Test Course With Archived', end=future, enrollment_end=future
        )
        SeatFactory.create(course_run=course_run)
        # Create an archived course run
        CourseRunFactory.create(course=course_run.course, end=past)

        response = self.client.get(url)

        assert response.status_code == 200
        # The course appearing in response should have on 1 course run
        assert len(response.data['results'][0]['course_runs']) == 1

        # Mark include archived True in catalog
        self.catalog.include_archived = True
        self.catalog.save()
        response = self.client.get(url)

        assert response.status_code == 200
        # The course appearing in response should include archived course run
        assert len(response.data['results'][0]['course_runs']) == 2
    def test_courses_with_time_range_query(self):
        catalog = CatalogFactory(query='start:[2015-01-01 TO 2015-12-01]')
        course_run_1 = CourseRunFactory(
            start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
            status=CourseRunStatus.Published,
            type__is_marketable=True,
        )
        course_run_2 = CourseRunFactory(
            start=datetime.datetime(2015, 10, 13, tzinfo=pytz.UTC),
            status=CourseRunStatus.Published,
            type__is_marketable=True,
        )
        SeatFactory.create(course_run=course_run_1)
        SeatFactory.create(course_run=course_run_2)
        call_command('search_index', '--rebuild', '-f')
        url = reverse('api:v1:catalog-courses', kwargs={'id': catalog.id})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.data['results'] == self.serialize_catalog_course(
            [course_run_1.course, course_run_2.course], many=True)
    def test_courses_with_different_catalog_queries_but_the_same_meaning(
            self, query):
        catalog = CatalogFactory(query=query)
        course_run_1 = CourseRunFactory(
            start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
            course__title='Science at the Polls: Biology for Voters, Part 1',
            status=CourseRunStatus.Published,
            type__is_marketable=True,
        )
        course_run_2 = CourseRunFactory(
            start=datetime.datetime(2015, 10, 13, tzinfo=pytz.UTC),
            course__title="DNA: Biology's Genetic Code",
            status=CourseRunStatus.Published,
            type__is_marketable=True,
        )
        course_run_3 = CourseRunFactory(
            status=CourseRunStatus.Published,
            start=datetime.datetime(2015, 1, 1, tzinfo=pytz.UTC),
            course__title="AP Biology",
            type__is_marketable=True,
        )
        SeatFactory.create(course_run=course_run_1)
        SeatFactory.create(course_run=course_run_2)
        SeatFactory.create(course_run=course_run_3)
        url = reverse('api:v1:catalog-courses', kwargs={'id': catalog.id})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.data['results'] == self.serialize_catalog_course(
            [course_run_1.course, course_run_2.course, course_run_3.course],
            many=True)
    def test_courses_with_subjects_and_negative_query(self):
        catalog = CatalogFactory(
            # pylint: disable=line-too-long
            query=
            'subjects:(-"Business & Management" AND -"Economics & Finance" AND -"Data Analysis & Statistics" AND -"Math"  AND -"Engineering") AND org:(-galileox AND -davidsonnext AND -microsoft AND -gtx AND -pekingx AND -asux AND -bux AND -columbiax)'
        )
        Course.objects.all().delete()
        not_included_subject_names = (
            'Business & Management',
            'Economics & Finance',
            'Business & Management',
            'Economics & Finance',
            'Data Analysis & Statistics',
            'math',
            'Engineering',
        )
        for name in not_included_subject_names:
            course_run = CourseRunFactory(
                start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
                status=CourseRunStatus.Published,
                type__is_marketable=True,
                course__subjects=[SubjectFactory(name=name)],
            )
            SeatFactory.create(course_run=course_run)

        included_subject_names = ('Health & Sport', 'Galaxy')
        desired_courses = []
        for name in included_subject_names:
            course_run = CourseRunFactory(
                start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
                status=CourseRunStatus.Published,
                type__is_marketable=True,
                course__subjects=[SubjectFactory(name=name)],
            )
            SeatFactory.create(course_run=course_run)
            desired_courses.append(course_run.course)

        not_included_org_names = ('galileox', 'davidsonnext', 'microsoft',
                                  'gtx', 'pekingx', 'asux', 'bux', 'ColumbiaX')
        for name in not_included_org_names:
            course_run = CourseRunFactory(
                start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
                status=CourseRunStatus.Published,
                type__is_marketable=True,
                key=
                f'{name}/{factory.Faker("word").evaluate(None, None, {"locale":"en"})}/test',
            )
            SeatFactory.create(course_run=course_run)

        included_org_names = ('apple', 'cisco')
        for name in included_org_names:
            course_run = CourseRunFactory(
                start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
                status=CourseRunStatus.Published,
                type__is_marketable=True,
                key=
                f'{name}/{factory.Faker("word").evaluate(None, None, {"locale":"en"})}/test',
            )
            SeatFactory.create(course_run=course_run)
            desired_courses.append(course_run.course)

        call_command('search_index', '--rebuild', '-f')
        url = reverse('api:v1:catalog-courses', kwargs={'id': catalog.id})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.data['results'] == self.serialize_catalog_course(
            desired_courses, many=True)