class MaterialDetailViewTestCase(TestCase):
    def setUp(self):
        self.course = CourseFactory()
        self.material1 = MaterialFactory(course=self.course, rank=0)
        self.material2 = MaterialFactory(course=self.course, rank=1)
        self.material3 = MaterialFactory(course=self.course, rank=2)

    def test_good_view(self):
        url = self.material2.get_absolute_url()
        response = self.client.get(url)
        assert response.status_code == 200
        self.assertTemplateUsed(response, "courses/material_detail.html")
        self.assertIn("course", response.context_data)
        self.assertIn("num_comments", response.context_data)
        self.assertIn("num_comment_participants", response.context_data)
        self.assertIn("prev", response.context_data)
        self.assertIn("next", response.context_data)

    def test_increase_material_views(self):
        url = self.material2.get_absolute_url()
        self.client.get(url)
        self.material2.refresh_from_db()
        assert self.material2.views == 1

    def test_headline(self):
        url = self.material2.get_absolute_url()
        response = self.client.get(url)
        self.assertContains(
            response,
            "%s_%s" % (self.material2.title, self.material2.course.title))
 def test_next(self, course):
     material = MaterialFactory(course=course)
     assert material.next is None
     next_material = MaterialFactory(course=course)
     assert Material.objects.get(pk=material.pk).next == {
         "course__slug": course.slug,
         "id": next_material.pk,
         "title": next_material.title,
     }
 def test_prev(self, course):
     material = MaterialFactory(course=course, rank=1)
     assert material.prev is None
     prev_material = MaterialFactory(course=course, rank=0)
     assert Material.objects.get(pk=material.pk).prev == {
         "course__slug": course.slug,
         "id": prev_material.pk,
         "title": prev_material.title,
     }
예제 #4
0
def test_get_index_entry_queryset():
    now = timezone.now()

    pinned_post1 = PostFactory(
        status=Post.STATUS_CHOICES.published,
        pinned=True,
        show_on_index=True,
        pub_date=now,
    )
    pinned_post2 = PostFactory(
        status=Post.STATUS_CHOICES.published,
        pinned=True,
        show_on_index=True,
        pub_date=now - timedelta(days=1),
    )
    material = MaterialFactory(status=Material.STATUS.published,
                               show_on_index=True,
                               pub_date=now)
    post = PostFactory(
        status=Post.STATUS_CHOICES.published,
        pinned=False,
        show_on_index=True,
        pub_date=now - timedelta(days=1),
    )

    expected = [pinned_post1.id, pinned_post2.id, material.id, post.id]
    assert [entry.id for entry in get_index_entry_queryset()] == expected
    def setup_method(self):
        after_3_days = timezone.now() + timedelta(days=3)

        self.published_material = MaterialFactory(
            status=Material.STATUS.published,
            show_on_index=True,
            pub_date=timezone.now(),
        )
        self.draft_material = MaterialFactory(
            status=Material.STATUS.draft,
            show_on_index=True,
        )
        self.writing_material = MaterialFactory(
            status=Material.STATUS.writing,
            show_on_index=True,
        )
        self.hidden_material = MaterialFactory(
            status=Material.STATUS.hidden,
            show_on_index=True,
        )
        self.future_publishing_material = MaterialFactory(
            status=Material.STATUS.published,
            show_on_index=True,
            pub_date=after_3_days,
        )
        self.future_draft_material = MaterialFactory(
            status=Material.STATUS.draft,
            show_on_index=True,
            pub_date=after_3_days,
        )
        self.hide_on_index_published_material = MaterialFactory(
            status=Material.STATUS.published,
            show_on_index=False,
            pub_date=timezone.now(),
        )
    def test_entries_display(self):
        course = CourseFactory()
        pinned_post = PostFactory(
            pinned=True,
            show_on_index=True,
            status=Post.STATUS_CHOICES.published,
        )
        material = MaterialFactory(
            course=course,
            show_on_index=True,
            status=Material.STATUS.published,
            pub_date=timezone.now(),
        )

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "[置顶] " + pinned_post.title)
        self.assertContains(response, pinned_post.brief)

        self.assertContains(response, material.title)
        self.assertContains(response, "系列教程")
class CourseDetailViewTestCase(TestCase):
    def setUp(self):
        self.course = CourseFactory()
        self.material1 = MaterialFactory(course=self.course, rank=0)
        self.material2 = MaterialFactory(course=self.course, rank=1)

    def test_good_view(self):
        url = self.course.get_absolute_url()
        response = self.client.get(url)
        assert response.status_code == 200
        self.assertTemplateUsed(response, "courses/course_detail.html")
        self.assertIn("next", response.context_data)
        self.assertEqual(
            response.context_data["next"],
            {
                "title": self.material1.title,
                "url": self.material1.get_absolute_url()
            },
        )

    def test_headline(self):
        course_url = self.course.get_absolute_url()
        response = self.client.get(course_url)
        self.assertContains(response, self.course.title)
 def setUp(self):
     self.course = CourseFactory()
     self.material1 = MaterialFactory(course=self.course, rank=0)
     self.material2 = MaterialFactory(course=self.course, rank=1)
     self.material3 = MaterialFactory(course=self.course, rank=2)
 def test_first_material(self, course):
     material1 = MaterialFactory(course=course)
     MaterialFactory(course=course)
     assert course.first_material == material1
예제 #10
0
def material():
    return MaterialFactory()