Coverage for books/tests/test_views.py: 100.00%
34 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 10:24 -0700
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 10:24 -0700
1from django.contrib.auth import get_user_model 1a
2from django.test import TestCase 1a
3from django.urls import reverse 1a
5from ..models import Book 1a
8class BookTests(TestCase): 1a
9 @classmethod 1a
10 def setUpTestData(self): 1a
11 self.user = get_user_model().objects.create_user( 1a
12 username="leopoldbloom",
13 email="leopoldbloom@example.com",
14 password="secret",
15 )
17 self.book = Book.objects.create( 1a
18 title="A good title",
19 subtitle="A tale of wonderful things",
20 author=self.user,
21 )
23 self.book2 = Book.objects.create( 1a
24 title="A good second title",
25 subtitle="Nice subtitle for a second book",
26 author=self.user,
27 )
29 def test___str__(self): 1a
30 assert self.book.__str__() == self.book.title 1a
31 assert str(self.book) == self.book.title 1a
33 def test_book_content(self): 1a
34 self.assertEqual(f"{self.book.title}", "A good title") 1a
35 self.assertEqual(f"{self.book.author}", "leopoldbloom") 1a
36 self.assertEqual(f"{self.book.subtitle}", "A tale of wonderful things") 1a
38 """ 1a
39 def test_get_absolute_url(self):
40 self.assertEqual(
41 self.book.get_absolute_url(), f"/books/{self.book.id}/"
42 )
43 """
45 def test_book_detail_view(self): 1a
46 self.client.login(email="leopoldbloom@example.com", password="secret") 1a
47 response = self.client.get(f"/api/v1/{self.book.id}/") 1a
48 self.assertEqual(response.status_code, 200) 1a
49 self.assertContains(response, "A good title") 1a
52class BookListViewTest(TestCase): 1a
53 def setUp(self): 1a
54 url = reverse("booklist") 1a
55 self.response = self.client.get(url) 1a
57 self.user = get_user_model().objects.create_user( 1a
58 username="johndoe",
59 email="johndoe@example.com",
60 password="secret",
61 )
63 self.book = Book.objects.create( 1a
64 title="A good title",
65 subtitle="A tale of wonderful things",
66 # slug="a-good-title",
67 author=self.user,
68 )
70 def test_view_url_exists_at_desired_location(self): 1a
71 # response = self.client.get("/books/")
72 self.assertEqual(self.response.status_code, 200) 1a
74 def test_view_url_accessible_by_name(self): 1a
75 self.assertEqual(self.response.status_code, 200) 1a
78""" 1a
79class SitemapTests(TestCase):
80 def setUp(self):
81 # url = reverse("sitemap")
82 url = "/sitemap.xml"
83 self.response = self.client.get(url)
85 def test_view_url_exists_at_desired_location(self):
86 self.assertEqual(self.response.status_code, 200)
89class RSSFeedTests(TestCase):
90 def setUp(self):
91 url = reverse("book_feed")
92 self.response = self.client.get(url)
94 def test_feed_url_exists_at_desired_location(self):
95 self.assertEqual(self.response.status_code, 200)
96"""