Coverage for books/tests/test_views.py: 100.00%

34 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-16 12:21 -0700

1from django.contrib.auth import get_user_model 

2from django.test import TestCase 

3from django.urls import reverse 

4 

5from ..models import Book 

6 

7 

8class BookTests(TestCase): 

9 @classmethod 

10 def setUpTestData(self): 

11 self.user = get_user_model().objects.create_user( 

12 username="leopoldbloom", 

13 email="leopoldbloom@example.com", 

14 password="secret", 

15 ) 

16 

17 self.book = Book.objects.create( 

18 title="A good title", 

19 subtitle="A tale of wonderful things", 

20 author=self.user, 

21 ) 

22 

23 self.book2 = Book.objects.create( 

24 title="A good second title", 

25 subtitle="Nice subtitle for a second book", 

26 author=self.user, 

27 ) 

28 

29 def test___str__(self): 

30 assert self.book.__str__() == self.book.title 1d

31 assert str(self.book) == self.book.title 1d

32 

33 def test_book_content(self): 

34 self.assertEqual(f"{self.book.title}", "A good title") 1c

35 self.assertEqual(f"{self.book.author}", "leopoldbloom") 1c

36 self.assertEqual(f"{self.book.subtitle}", "A tale of wonderful things") 1c

37 

38 """ 

39 def test_get_absolute_url(self): 

40 self.assertEqual( 

41 self.book.get_absolute_url(), f"/books/{self.book.id}/" 

42 ) 

43 """ 

44 

45 def test_book_detail_view(self): 

46 self.client.login(email="leopoldbloom@example.com", password="secret") 1b

47 response = self.client.get(f"/api/v1/{self.book.id}/") 1b

48 self.assertEqual(response.status_code, 200) 1b

49 self.assertContains(response, "A good title") 1b

50 

51 

52class BookListViewTest(TestCase): 

53 def setUp(self): 

54 url = reverse("booklist") 

55 self.response = self.client.get(url) 

56 

57 self.user = get_user_model().objects.create_user( 

58 username="johndoe", 

59 email="johndoe@example.com", 

60 password="secret", 

61 ) 

62 

63 self.book = Book.objects.create( 

64 title="A good title", 

65 subtitle="A tale of wonderful things", 

66 # slug="a-good-title", 

67 author=self.user, 

68 ) 

69 

70 def test_view_url_exists_at_desired_location(self): 

71 # response = self.client.get("/books/") 

72 self.assertEqual(self.response.status_code, 200) 1e

73 

74 def test_view_url_accessible_by_name(self): 

75 self.assertEqual(self.response.status_code, 200) 1f

76 

77 

78""" 

79class SitemapTests(TestCase): 

80 def setUp(self): 

81 # url = reverse("sitemap") 

82 url = "/sitemap.xml" 

83 self.response = self.client.get(url) 

84 

85 def test_view_url_exists_at_desired_location(self): 

86 self.assertEqual(self.response.status_code, 200) 

87 

88 

89class RSSFeedTests(TestCase): 

90 def setUp(self): 

91 url = reverse("book_feed") 

92 self.response = self.client.get(url) 

93 

94 def test_feed_url_exists_at_desired_location(self): 

95 self.assertEqual(self.response.status_code, 200) 

96"""