Coverage for books/tests/test_views.py: 100.00%
75 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-10-23 15:55 -0700
« prev ^ index » next coverage.py v7.17.0, created at 2026-10-23 15:55 -0700
1import pytest 1a
2from django.urls import reverse 1a
3from pytest_django.asserts import ( 1a
4 assertContains,
5)
7from ..models import Book 1a
8from ..views import ( 1a
9 BookCreateView,
10 BookDetailView,
11 BookListView,
12 BookUpdateView,
13)
15# from .factories import BookFactory
17pytestmark = pytest.mark.django_db 1a
20def test_book_list_view(rf): 1a
21 # Get the request
22 request = rf.get(reverse("book_list")) 1a
23 # Use the request to get the response
24 response = BookListView.as_view()(request) 1a
25 # Test that the response is valid
26 assertContains(response, "Our Book list") 1a
29def test_book_detail_view(rf, book): 1a
30 # Get the request
31 url = reverse("book_detail", kwargs={"pk": book.pk}) 1a
32 request = rf.get(url) 1a
33 # Use the request to get the response
34 callable_obj = BookDetailView.as_view() 1a
35 response = callable_obj(request, pk=book.pk) 1a
36 # Test that the response is valid
37 assertContains(response, book.title) 1a
40def test_book_create_view(rf, book, admin_user): 1a
41 form_data = { 1a
42 "title": book.title,
43 "author": book.author,
44 "price": book.price,
45 "creator": book.creator,
46 }
47 # Make a request for our new book
48 request = rf.get(reverse("book_add"), form_data) 1a
49 # Add an authenticated user
50 request.user = admin_user 1a
51 # Use the request to get the response
52 response = BookCreateView.as_view()(request) 1a
53 book_sample = Book.objects.last() 1a
54 # Test that the response is valid
55 assert response.status_code == 200 1a
56 assert book_sample.creator == book.creator 1a
57 assert book_sample.title == book.title 1a
60""" 1a
61def test_book_list_contains_2_books(rf, book):
62 # Create a couple of books
63 book1 = BookFactory()
64 book2 = BookFactory()
65 # Create a request and then a response
66 # for the list of books
67 request = rf.get(reverse("book_list"))
68 response = BookListView.as_view()(request)
69 # Assert that the response contains both book titles
70 # in the template
71 assertContains(response, book1.title)
72 assertContains(response, book2.title)
73"""
76def test_detail_contains_book_data(rf, book): 1a
77 # Make a request for our new book
78 url = reverse("book_detail", kwargs={"pk": book.pk}) 1a
79 request = rf.get(url) 1a
80 # Use the request to get the response
81 callable_obj = BookDetailView.as_view() 1a
82 response = callable_obj(request, pk=book.pk) 1a
83 # Test our Book details:
84 assertContains(response, book.title) 1a
85 assertContains(response, book.author) 1a
86 assertContains(response, book.price) 1a
89def test_book_create_form_valid(rf, admin_user): 1a
90 # Submit the book add form
91 form_data = { 1a
92 "title": "Programming Python",
93 "author": "Mark Lutz",
94 "price": "69.00",
95 }
96 request = rf.post(reverse("book_add"), form_data) 1a
97 request.user = admin_user 1a
98 response = BookCreateView.as_view()(request) 1a
99 # Get the book based on the name
100 book = Book.objects.get(title="Programming Python") 1a
101 # Test that the book matches our form
102 assert response.status_code == 302 1a
103 assert book.author == "Mark Lutz" 1a
104 assert book.price == 69.00 # ruff: ignore[float-equality-comparison] 1a
105 assert book.creator == admin_user 1a
108def test_book_create_correct_title(rf, admin_user): 1a
109 """Page title for BookCreateView should be Add Book."""
110 request = rf.get(reverse("book_add")) 1a
111 request.user = admin_user 1a
112 response = BookCreateView.as_view()(request) 1a
113 assertContains(response, "Add a Book") 1a
116def test_book_update_view(rf, admin_user, book): 1a
117 url = reverse("book_update", kwargs={"pk": book.id}) 1a
118 # Make a request for our new cheese
119 request = rf.get(url) 1a
120 # Add an authenticated user
121 request.user = admin_user 1a
122 # Use the request to get the response
123 callable_obj = BookUpdateView.as_view() 1a
124 response = callable_obj(request, pk=book.id) 1a
125 # Test that the response is valid
126 assertContains(response, "Update") 1a
129def test_book_update(rf, admin_user, book): 1a
130 """POST request to BookUpdateView updates a book
131 and redirects.
132 """
133 # Make a request for our new book
134 form_data = { 1a
135 "title": book.title,
136 "author": "John Q. Public",
137 "price": book.price,
138 }
139 url = reverse("book_update", kwargs={"pk": book.id}) 1a
140 request = rf.post(url, form_data) 1a
141 request.user = admin_user 1a
142 callable_obj = BookUpdateView.as_view() 1a
143 response = callable_obj(request, pk=book.id) 1a
145 # Check that the book has been changed
146 book.refresh_from_db() 1a
147 assert book.author == "John Q. Public" 1a
148 assert response.status_code == 302 1a
151@pytest.mark.django_db 1a
152def test_sitemap(client, ten_books): 1a
153 response = client.get("/sitemap.xml") 1a
154 xml = response.content.decode("utf-8") 1a
155 expected_books = [p for p in ten_books] 1a
156 assert response.status_code == 200 1a
157 assert len(expected_books) == 10 1a
158 assert "<loc>" in xml 1a
159 # assert "<lastmod>" in xml
162""" 1a
163def test_review_create_form_valid(rf, admin_user, book, review):
164 form_data = {
165 "review": "This is a great book",
166 }
167 url = reverse("review_create", kwargs={"book_id": str(book.id)})
168 request = rf.post(url, form_data)
169 request.user = admin_user
170 callable_obj = ReviewCreateView.as_view()
171 response = callable_obj(request) # noqa:F841
172 # response = callable_obj(request, book.id) # noqa:F841
173 # Get the book based on the name
174 review = Review.objects.get(review="This is a great book")
175 # Test that the book matches our form
176 book.refresh_from_db()
177 assert response.status_code == 302
178 assert review.book == book.title
179 assert review.review == "This is a great book"
180 assert review.creator == admin_user
183def test_book_delete(rf, book):
184 request = rf.post(
185 reverse("book_delete", kwargs={"pk": book.id}),
186 )
187 request.user = book.creator
188 callable_obj = BookDeleteView.as_view()
189 response = callable_obj(request, pk=book.id)
190 assert response.status_code == 302
191"""