当前位置: 首页>>代码示例>>Python>>正文


Python CourseFactory.save方法代码示例

本文整理汇总了Python中courses.tests.factories.CourseFactory.save方法的典型用法代码示例。如果您正苦于以下问题:Python CourseFactory.save方法的具体用法?Python CourseFactory.save怎么用?Python CourseFactory.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在courses.tests.factories.CourseFactory的用法示例。


在下文中一共展示了CourseFactory.save方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CourseAPITest

# 需要导入模块: from courses.tests.factories import CourseFactory [as 别名]
# 或者: from courses.tests.factories.CourseFactory import save [as 别名]
class CourseAPITest(TestCase):
    def setUp(self):
        self.api_url = reverse("fun-courses:api-list")
        self.active_1 = CourseFactory(title="active course 1", show_in_catalog=True, is_active=True)
        self.active_2 = CourseFactory(title="active course 2", show_in_catalog=True, is_active=True)
        self.not_active = CourseFactory(title="course not active", show_in_catalog=True, is_active=False)
        self.not_in_catalog = CourseFactory(title="course not in catalog", show_in_catalog=False, is_active=True)

    @property
    def soon(self):
        return now() + timedelta(days=1)

    @property
    def too_late(self):
        return now() + timedelta(days=1000)

    def test_course_list_api_response_loads(self):
        response = self.client.get(self.api_url)
        data = json.loads(response.content)
        self.assertIn("results", data)

    def test_response_contains_on_active_courses(self):
        response = self.client.get(self.api_url)
        self.assertContains(response, self.active_1.title)
        self.assertContains(response, self.active_2.title)
        self.assertNotContains(response, self.not_active.title)
        self.assertNotContains(response, self.not_in_catalog.title)

    def test_only_display_courses_for_a_specific_university(self):
        university = UniversityFactory(code="test-university")
        UniversityFactory(code="another-university")
        CourseUniversityRelation.objects.create(course=self.active_1, university=university)
        filter_data = {"university": "test-university"}
        response = self.client.get(self.api_url, filter_data)
        self.assertContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)
        filter_data = {"university": "another-university"}
        response = self.client.get(self.api_url, filter_data)
        self.assertNotContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)

    def test_only_display_courses_for_a_specific_subject(self):
        subject = CourseSubjectFactory(slug="test-subject")
        UniversityFactory(slug="another-subject")
        self.active_1.subjects.add(subject)
        filter_data = {"subject": "test-subject"}
        response = self.client.get(self.api_url, filter_data)
        self.assertContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)
        filter_data = {"subject": "another-subject"}
        response = self.client.get(self.api_url, filter_data)
        self.assertNotContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)

    def test_only_display_courses_for_a_specific_level(self):
        self.active_1.level = courses_choices.COURSE_LEVEL_INTRODUCTORY
        self.active_1.save()
        filter_data = {"level": courses_choices.COURSE_LEVEL_INTRODUCTORY}
        response = self.client.get(self.api_url, filter_data)
        self.assertContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)
        filter_data = {"level": courses_choices.COURSE_LEVEL_ADVANCED}
        response = self.client.get(self.api_url, filter_data)
        self.assertNotContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)

    def test_only_display_courses_starting_soon(self):
        self.active_1.start_date = self.soon
        self.active_1.save()
        self.active_2.start_date = self.too_late
        self.active_2.save()
        filter_data = {"availability": "start-soon"}
        response = self.client.get(self.api_url, filter_data)
        self.assertContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)

    def test_only_display_courses_ending_soon(self):
        self.active_1.end_date = self.soon
        self.active_1.save()
        self.active_2.end_date = self.too_late
        self.active_2.save()
        filter_data = {"availability": "end-soon"}
        response = self.client.get(self.api_url, filter_data)
        self.assertContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)

    def test_only_display_new_courses(self):
        self.active_1.session_number = 1
        self.active_1.save()
        self.active_2.session_number = 2
        self.active_2.save()
        filter_data = {"availability": "new"}
        response = self.client.get(self.api_url, filter_data)
        self.assertContains(response, self.active_1.title)
        self.assertNotContains(response, self.active_2.title)

    def test_new_courses_exclude_courses_with_finished_enrollent(self):
        self.active_1.session_number = 1
        self.active_1.enrollment_end_date = now() - timedelta(days=1)
        self.active_1.save()
#.........这里部分代码省略.........
开发者ID:regisb,项目名称:fun-apps,代码行数:103,代码来源:test_api.py

示例2: CourseAPITest

# 需要导入模块: from courses.tests.factories import CourseFactory [as 别名]
# 或者: from courses.tests.factories.CourseFactory import save [as 别名]
class CourseAPITest(TestCase):

    def setUp(self):
        next_week = now() + timedelta(days=7)
        self.api_url = reverse('fun-courses-api:courses-list')
        self.active_1 = CourseFactory(
            title='active course 1',
            show_in_catalog=True,
            is_active=True,
            end_date=next_week,
        )
        self.active_2 = CourseFactory(
            title='active course 2',
            show_in_catalog=True,
            is_active=True,
            end_date=next_week,
        )
        self.not_active = CourseFactory(
            title='course not active',
            show_in_catalog=True,
            is_active=False,
            end_date=next_week,
        )
        self.not_in_catalog = CourseFactory(
            title='course not in catalog',
            show_in_catalog=False,
            is_active=True,
            end_date=next_week,
        )
        self.user = UserFactory(username='user', password='password')  # user with profile

    def login_as_admin(self):
        self.user.is_staff = True
        self.user.save()
        self.client.login(username='user', password='password')

    @property
    def soon(self):
        return now() + timedelta(days=1)

    @property
    def too_late(self):
        return now() + timedelta(days=1000)

    def test_course_list_api_response_loads(self):
        response = self.client.get(self.api_url)
        data = json.loads(response.content)
        self.assertIn('results', data)

    def test_response_contains_only_active_courses(self):
        response = self.client.get(self.api_url)
        self.assertContains(response, self.active_1.title)
        self.assertContains(response, self.active_2.title)
        self.assertNotContains(response, self.not_active.title)
        self.assertNotContains(response, self.not_in_catalog.title)

    def test_staff_user_can_see_courses_not_catalog(self):
        self.user.is_staff = True
        self.user.save()
        self.client.login(username='user', password='password')
        data = {'extended_list': True}
        response = self.client.get(self.api_url, data)
        self.assertContains(response, self.active_1.title)
        self.assertContains(response, self.active_2.title)
        self.assertContains(response, self.not_in_catalog.title)

    def test_user_can_only_see_public_courses_if_not_staff(self):
        self.user.is_staff = False
        self.user.save()
        self.client.login(username='user', password='password')
        data = {'extended_list': True}
        response = self.client.get(self.api_url, data)
        self.assertContains(response, self.active_1.title)
        self.assertContains(response, self.active_2.title)
        self.assertNotContains(response, self.not_in_catalog.title)

    def test_user_can_only_see_public_courses_if_not_logged_in(self):
        self.client.logout()
        data = {'extended_list': True}
        response = self.client.get(self.api_url, data)
        self.assertContains(response, self.active_1.title)
        self.assertContains(response, self.active_2.title)
        self.assertNotContains(response, self.not_in_catalog.title)

    def test_can_update_course_score_as_admin(self):
        self.login_as_admin()
        self.active_1.score = 0
        self.active_1.save()
        data = json.dumps({'score': 100})
        url = reverse('fun-courses-api:courses-detail',
            args=[self.active_1.id]
        )
        response = self.client.put(url, data, content_type='application/json')
        response_data = json.loads(response.content)
        self.assertEqual(100, response_data['score'])

    def test_cannot_update_course_score_if_not_logged_in(self):
        self.client.logout()
        data = {'score': 100}
        url = reverse('fun-courses-api:courses-detail',
#.........这里部分代码省略.........
开发者ID:openfun,项目名称:fun-apps,代码行数:103,代码来源:test_course_api.py


注:本文中的courses.tests.factories.CourseFactory.save方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。