Coverage for cheese/cheeses/tests/test_views.py: 100.00%
67 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-23 08:46 -0800
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-23 08:46 -0800
1import pytest 1a
3# from django.contrib.sessions.middleware import SessionMiddleware
4# from django.test import RequestFactory
5from django.urls import reverse 1a
6from pytest_django.asserts import ( 1a
7 assertContains,
8)
10# assertRedirects,
11# from cheese.users.models import User
12from ..models import Cheese 1a
13from ..views import ( 1a
14 CheeseCreateView,
15 CheeseDetailView,
16 CheeseListView,
17 CheeseUpdateView,
18)
19from .factories import CheeseFactory, cheese 1a
21pytestmark = pytest.mark.django_db 1a
24def test_good_cheese_list_view_expanded(rf): 1a
25 # Determine the URL
26 # url = reverse("cheeses:list")
27 # rf is the pytest shortcut to django.test.RequestFactory
28 # we generate a request as if from a user accessing
29 # the cheese list view
30 # request = rf.get(url)
31 request = rf.get(reverse("cheeses:list")) 1a
32 # Call as_view() to make a callable object
33 # callable_obj is analogous to a function-based view
34 # callable_obj = CheeseListView.as_view()
35 # Pass in the request into the callable_obj to get an
36 # HTTP response served up by Django
37 # response = callable_obj(request)
38 response = CheeseListView.as_view()(request) 1a
39 # Test that the HTTP response has 'Cheese List' in the
40 # HTML and has a 200 response code
41 assertContains(response, "Cheese List") 1a
44def test_good_cheese_detail_view(rf, cheese): 1a
45 # Order some cheese from the CheeseFactory
46 # Make a request for our new cheese
47 url = reverse("cheeses:detail", kwargs={"slug": cheese.slug}) 1a
48 request = rf.get(url) 1a
50 # Use the request to get the response
51 callable_obj = CheeseDetailView.as_view() 1a
52 response = callable_obj(request, slug=cheese.slug) 1a
53 # Test that the response is valid
54 assertContains(response, cheese.name) 1a
57def test_good_cheese_create_view(rf, cheese, admin_user): 1a
58 # Order some cheese from the CheeseFactory
59 # Make a request for our new cheese
60 request = rf.get(reverse("cheeses:add")) 1a
61 # Add an authenticated user
62 request.user = admin_user 1a
63 # Use the request to get the response
64 response = CheeseCreateView.as_view()(request) 1a
65 # Test that the response is valid
66 assert response.status_code == 200 1a
69def test_cheese_list_contains_2_cheeses(rf): 1a
70 # Let's create a couple cheeses
71 cheese1 = CheeseFactory() 1a
72 cheese2 = CheeseFactory() 1a
73 # Create a request and then a response
74 # for a list of cheeses
75 request = rf.get(reverse("cheeses:list")) 1a
76 response = CheeseListView.as_view()(request) 1a
77 # Assert that the response contains both cheese names
78 # in the template.
79 assertContains(response, cheese1.name) 1a
80 assertContains(response, cheese2.name) 1a
83def test_detail_contains_cheese_data(rf, cheese): 1a
84 # Make a request for our new cheese
85 url = reverse("cheeses:detail", kwargs={"slug": cheese.slug}) 1a
86 request = rf.get(url) 1a
87 # Use the request to get the response
88 callable_obj = CheeseDetailView.as_view() 1a
89 response = callable_obj(request, slug=cheese.slug) 1a
90 # Let's test our Cheesy details!
91 assertContains(response, cheese.name) 1a
92 assertContains(response, cheese.get_firmness_display()) 1a
93 assertContains(response, cheese.country_of_origin.name) 1a
96def test_cheese_create_form_valid(rf, admin_user): 1a
97 # Submit the cheese add form
98 form_data = { 1a
99 "name": "Paski Sir",
100 "description": "A salty hard cheese",
101 "firmness": Cheese.Firmness.HARD,
102 }
103 request = rf.post(reverse("cheeses:add"), form_data) 1a
104 request.user = admin_user 1a
105 response = CheeseCreateView.as_view()(request) # noqa:F841 1a
106 # Get the cheese based on the name
107 cheese = Cheese.objects.get(name="Paski Sir") 1a
108 # Test that the cheese matches our form
109 assert cheese.description == "A salty hard cheese" 1a
110 assert cheese.firmness == Cheese.Firmness.HARD 1a
111 assert cheese.creator == admin_user 1a
114def test_cheese_create_correct_title(rf, admin_user): 1a
115 """Page title for CheeseCreateView should be Add Cheese."""
116 request = rf.get(reverse("cheeses:add")) 1a
117 request.user = admin_user 1a
118 response = CheeseCreateView.as_view()(request) 1a
119 assertContains(response, "Add Cheese") 1a
122def test_good_cheese_update_view(rf, admin_user, cheese): 1a
123 url = reverse("cheeses:update", kwargs={"slug": cheese.slug}) 1a
124 # Make a request for our new cheese
125 request = rf.get(url) 1a
126 # Add an authenticated user
127 request.user = admin_user 1a
128 # Use the request to get the response
129 callable_obj = CheeseUpdateView.as_view() 1a
130 response = callable_obj(request, slug=cheese.slug) 1a
131 # Test that the response is valid
132 assertContains(response, "Update Cheese") 1a
135def test_cheese_update(rf, admin_user, cheese): 1a
136 """POST request to CheeseUpdateView updates a cheese
137 and redirects.
138 """
139 # Make a request for our new cheese
140 form_data = { 1a
141 "name": cheese.name,
142 "description": "Something new",
143 "firmness": cheese.firmness,
144 }
145 url = reverse("cheeses:update", kwargs={"slug": cheese.slug}) 1a
146 request = rf.post(url, form_data) 1a
147 request.user = admin_user 1a
148 callable_obj = CheeseUpdateView.as_view() 1a
149 response = callable_obj(request, slug=cheese.slug) # ruff: ignore[unused-variable] 1a
151 # Check that the cheese has been changed
152 cheese.refresh_from_db() 1a
153 assert cheese.description == "Something new" 1a