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

33 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-15 09:40 -0700

1from django.contrib.auth import get_user_model 1a

2from django.test import TestCase 1a

3from django.urls import reverse 1a

4 

5from ..models import Post 1a

6 

7 

8class PostTests(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 ) 

16 

17 self.post = Post.objects.create( 1a

18 title="A good title", 

19 body="Nice body content", 

20 author=self.user, 

21 ) 

22 

23 self.post2 = Post.objects.create( 1a

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): 1a

30 assert str(self.post) == self.post.title 1a

31 

32 def test_post_content(self): 1a

33 self.assertEqual(f"{self.post.title}", "A good title") 1a

34 self.assertEqual(f"{self.post.author}", "leopoldbloom") 1a

35 self.assertEqual(f"{self.post.body}", "Nice body content") 1a

36 

37 """ 1a

38 def test_get_absolute_url(self): 

39 self.assertEqual( 

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

41 ) 

42 """ 

43 

44 def test_post_detail_view(self): 1a

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

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

47 self.assertEqual(response.status_code, 200) 1a

48 self.assertContains(response, "A good title") 1a

49 

50 

51class PostListViewTest(TestCase): 1a

52 def setUp(self): 1a

53 url = reverse("post_list") 1a

54 self.response = self.client.get(url) 1a

55 

56 self.user = get_user_model().objects.create_user( 1a

57 username="johndoe", 

58 email="johndoe@example.com", 

59 password="secret", 

60 ) 

61 

62 self.post = Post.objects.create( 1a

63 title="A good title", 

64 body="Nice body content", 

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

66 author=self.user, 

67 ) 

68 

69 def test_view_url_exists_at_desired_location(self): 1a

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

71 self.assertEqual(self.response.status_code, 200) 1a

72 

73 def test_view_url_accessible_by_name(self): 1a

74 self.assertEqual(self.response.status_code, 200) 1a

75 

76 

77""" 1a

78class SitemapTests(TestCase): 

79 def setUp(self): 

80 # url = reverse("sitemap") 

81 url = "/sitemap.xml" 

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

83 

84 def test_view_url_exists_at_desired_location(self): 

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

86 

87 

88class RSSFeedTests(TestCase): 

89 def setUp(self): 

90 url = reverse("post_feed") 

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

92 

93 def test_feed_url_exists_at_desired_location(self): 

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

95"""