File tree Expand file tree Collapse file tree 3 files changed +14
-26
lines changed
tests/pytest/pems/districts Expand file tree Collapse file tree 3 files changed +14
-26
lines changed Original file line number Diff line number Diff line change 1
1
{% extends "districts/index.html" %}
2
2
3
3
{% block headline %}
4
- District {{ district_number }} - {{ district.name }}
4
+ District {{ district.number }} - {{ district.name }}
5
5
{% endblock headline %}
6
6
7
7
{% block districts-content %}
Original file line number Diff line number Diff line change 1
- from django .views .generic import TemplateView
1
+ from django .views .generic import TemplateView , DetailView
2
2
3
3
from .models import District
4
4
@@ -17,12 +17,10 @@ class IndexView(DistrictContextMixin, TemplateView):
17
17
template_name = "districts/index.html"
18
18
19
19
20
- class DistrictView (DistrictContextMixin , TemplateView ):
20
+ class DistrictView (DistrictContextMixin , DetailView ):
21
+ model = District
22
+ context_object_name = "district"
21
23
template_name = "districts/district.html"
22
24
23
- def get_context_data (self , ** kwargs ):
24
- context = super ().get_context_data (** kwargs )
25
- district_number = self .kwargs .get ("district" )
26
- context ["district_number" ] = district_number
27
- context ["district" ] = context .get ("districts" ).get ("all" ).get (number = district_number )
28
- return context
25
+ def get_object (self ):
26
+ return District .objects .get (number__iexact = self .kwargs ["district" ])
Original file line number Diff line number Diff line change 1
1
import pytest
2
2
3
+ from django .urls import reverse
3
4
from pems .districts import views
4
5
from pems .districts .models import District
5
6
@@ -23,21 +24,10 @@ def test_template_name(self, view):
23
24
assert view .template_name == "districts/index.html"
24
25
25
26
26
- class TestDistrictView :
27
- @pytest .fixture
28
- def view (app_request ):
29
- v = views .DistrictView ()
30
- v .setup (app_request , district = 1 )
31
-
32
- return v
27
+ @pytest .mark .django_db
28
+ def test_district_view (client , model_District ):
29
+ url = reverse ("districts:district" , kwargs = {"district" : 1 })
30
+ response = client .get (url )
33
31
34
- @pytest .mark .django_db
35
- @pytest .mark .usefixtures ("model_District" )
36
- def test_get_context_data (self , view ):
37
-
38
- context = view .get_context_data ()
39
-
40
- assert context ["district_number" ] == 1
41
-
42
- def test_template_name (self , view ):
43
- assert view .template_name == "districts/district.html"
32
+ assert response .status_code == 200
33
+ assert response .context ["district" ] == model_District
You can’t perform that action at this time.
0 commit comments