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

34 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-24 10:40 -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 1d

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

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="johndoe@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 

52class PostListViewTest(TestCase): 

53 def setUp(self): 

54 url = reverse("post_list") 

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

64 title="A good title", 

65 body="Nice body content", 

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("/posts/") 

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("post_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"""