Coverage for cheese/cheeses/tests/test_views.py: 100.00%
67 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 16:36 -0700
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 16:36 -0700
1import pytest
3# from django.contrib.sessions.middleware import SessionMiddleware # noqa:F401
4# from django.test import RequestFactory # noqa:F401
5from django.urls import reverse
6from pytest_django.asserts import ( # noqa:F401
7 assertContains,
8)
10# assertRedirects,
11# from cheese.users.models import User # noqa:F401
12from ..models import Cheese
13from ..views import (
14 CheeseCreateView,
15 CheeseDetailView,
16 CheeseListView,
17 CheeseUpdateView,
18)
19from .factories import CheeseFactory, cheese # noqa:F401
21pytestmark = pytest.mark.django_db
24def test_good_cheese_list_view_expanded(rf):
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")) 1j
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) 1j
39 # Test that the HTTP response has 'Cheese List' in the
40 # HTML and has a 200 response code
41 assertContains(response, "Cheese List") 1j
44def test_good_cheese_detail_view(rf, cheese): # noqa:F811
45 # Order some cheese from the CheeseFactory
46 # Make a request for our new cheese
47 url = reverse("cheeses:detail", kwargs={"slug": cheese.slug}) 1g
48 request = rf.get(url) 1g
50 # Use the request to get the response
51 callable_obj = CheeseDetailView.as_view() 1g
52 response = callable_obj(request, slug=cheese.slug) 1g
53 # Test that the response is valid
54 assertContains(response, cheese.name) 1g
57def test_good_cheese_create_view(rf, cheese, admin_user): # noqa:F811
58 # Order some cheese from the CheeseFactory
59 # Make a request for our new cheese
60 request = rf.get(reverse("cheeses:add")) 1h
61 # Add an authenticated user
62 request.user = admin_user 1h
63 # Use the request to get the response
64 response = CheeseCreateView.as_view()(request) 1h
65 # Test that the response is valid
66 assert response.status_code == 200 1h
69def test_cheese_list_contains_2_cheeses(rf):
70 # Let's create a couple cheeses
71 cheese1 = CheeseFactory() 1e
72 cheese2 = CheeseFactory() 1e
73 # Create a request and then a response
74 # for a list of cheeses
75 request = rf.get(reverse("cheeses:list")) 1e
76 response = CheeseListView.as_view()(request) 1e
77 # Assert that the response contains both cheese names
78 # in the template.
79 assertContains(response, cheese1.name) 1e
80 assertContains(response, cheese2.name) 1e
83def test_detail_contains_cheese_data(rf, cheese): # noqa:F811
84 # Make a request for our new cheese
85 url = reverse("cheeses:detail", kwargs={"slug": cheese.slug}) 1d
86 request = rf.get(url) 1d
87 # Use the request to get the response
88 callable_obj = CheeseDetailView.as_view() 1d
89 response = callable_obj(request, slug=cheese.slug) 1d
90 # Let's test our Cheesy details!
91 assertContains(response, cheese.name) 1d
92 assertContains(response, cheese.get_firmness_display()) 1d
93 assertContains(response, cheese.country_of_origin.name) 1d
96def test_cheese_create_form_valid(rf, admin_user):
97 # Submit the cheese add form
98 form_data = { 1b
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) 1b
104 request.user = admin_user 1b
105 response = CheeseCreateView.as_view()(request) # noqa:F841 1b
106 # Get the cheese based on the name
107 cheese = Cheese.objects.get(name="Paski Sir") # noqa:F811 1b
108 # Test that the cheese matches our form
109 assert cheese.description == "A salty hard cheese" 1b
110 assert cheese.firmness == Cheese.Firmness.HARD 1b
111 assert cheese.creator == admin_user 1b
114def test_cheese_create_correct_title(rf, admin_user):
115 """Page title for CheeseCreateView should be Add Cheese."""
116 request = rf.get(reverse("cheeses:add")) 1i
117 request.user = admin_user 1i
118 response = CheeseCreateView.as_view()(request) 1i
119 assertContains(response, "Add Cheese") 1i
122def test_good_cheese_update_view(rf, admin_user, cheese): # noqa:F811
123 url = reverse("cheeses:update", kwargs={"slug": cheese.slug}) 1f
124 # Make a request for our new cheese
125 request = rf.get(url) 1f
126 # Add an authenticated user
127 request.user = admin_user 1f
128 # Use the request to get the response
129 callable_obj = CheeseUpdateView.as_view() 1f
130 response = callable_obj(request, slug=cheese.slug) 1f
131 # Test that the response is valid
132 assertContains(response, "Update Cheese") 1f
135def test_cheese_update(rf, admin_user, cheese): # noqa:F811
136 """POST request to CheeseUpdateView updates a cheese
137 and redirects.
138 """
139 # Make a request for our new cheese
140 form_data = { 1c
141 "name": cheese.name,
142 "description": "Something new",
143 "firmness": cheese.firmness,
144 }
145 url = reverse("cheeses:update", kwargs={"slug": cheese.slug}) 1c
146 request = rf.post(url, form_data) 1c
147 request.user = admin_user 1c
148 callable_obj = CheeseUpdateView.as_view() 1c
149 response = callable_obj(request, slug=cheese.slug) # noqa:F841 1c
151 # Check that the cheese has been changed
152 cheese.refresh_from_db() 1c
153 assert cheese.description == "Something new" 1c