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

38 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2025-08-03 13:46 -0700

1from django.contrib.auth import get_user_model 

2from django.test import TestCase 

3from django.urls import reverse 

4 

5from ..models import Post 

6 

7 

8class PostTests(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.post = Post.objects.create( 

18 title="A good title", 

19 body="Nice body content", 

20 author=self.user, 

21 ) 

22 

23 self.post2 = Post.objects.create( 

24 title="A good second title", 

25 body="Nice body content for a second post", 

26 author=self.user, 

27 ) 

28 

29 def test___str__(self): 

30 assert self.post.__str__() == self.post.title 1e

31 assert str(self.post) == self.post.title 1e

32 

33 def test_post_content(self): 

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

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

36 self.assertEqual(f"{self.post.body}", "Nice body content") 1c

37 

38 """ 

39 def test_get_absolute_url(self): 

40 self.assertEqual( 

41 self.post.get_absolute_url(), f"/posts/{self.post.id}/" 

42 ) 

43 """ 

44 

45 def test_post_detail_view(self): 

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

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

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

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

50 

51 def test_post_detail_view_failure(self): 

52 self.client.login(email="johndoe@example.com", password="secret") 1d

53 response = self.client.get(f"/api/v1/{self.post.id}/") 1d

54 self.assertEqual(response.status_code, 403) 1d

55 

56 

57class PostListViewTest(TestCase): 

58 def setUp(self): 

59 url = reverse("post_list") 

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

61 

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

63 username="johndoe", 

64 email="johndoe@example.com", 

65 password="secret", 

66 ) 

67 

68 self.post = Post.objects.create( 

69 title="A good title", 

70 body="Nice body content", 

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

72 author=self.user, 

73 ) 

74 

75 def test_view_url_exists_at_desired_location(self): 

76 # response = self.client.get("/posts/") 

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

78 

79 def test_view_url_accessible_by_name(self): 

80 self.assertEqual(self.response.status_code, 200) 1g

81 

82 

83""" 

84class SitemapTests(TestCase): 

85 def setUp(self): 

86 # url = reverse("sitemap") 

87 url = "/sitemap.xml" 

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

89 

90 def test_view_url_exists_at_desired_location(self): 

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

92 

93 

94class RSSFeedTests(TestCase): 

95 def setUp(self): 

96 url = reverse("post_feed") 

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

98 

99 def test_feed_url_exists_at_desired_location(self): 

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

101"""