forked from carltongibson/django-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_views.py
81 lines (62 loc) · 2.75 KB
/
test_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import absolute_import
from __future__ import unicode_literals
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test.client import RequestFactory
from django_filters.views import FilterView
from django_filters.filterset import FilterSet, filterset_factory
from .models import Book
class GenericViewTestCase(TestCase):
urls = 'tests.urls'
def setUp(self):
Book.objects.create(
title="Ender's Game", price='1.00', average_rating=3.0)
Book.objects.create(
title="Rainbow Six", price='1.00', average_rating=3.0)
Book.objects.create(
title="Snowcrash", price='1.00', average_rating=3.0)
class GenericClassBasedViewTests(GenericViewTestCase):
base_url = '/books/'
def test_view(self):
response = self.client.get(self.base_url)
for b in ['Ender's Game', 'Rainbow Six', 'Snowcrash']:
self.assertContains(response, b)
def test_view_filtering_on_title(self):
response = self.client.get(self.base_url + '?title=Snowcrash')
for b in ['Ender's Game', 'Rainbow Six']:
self.assertNotContains(response, b)
self.assertContains(response, 'Snowcrash')
def test_view_with_filterset_not_model(self):
factory = RequestFactory()
request = factory.get(self.base_url)
filterset = filterset_factory(Book)
view = FilterView.as_view(filterset_class=filterset)
response = view(request)
self.assertEqual(response.status_code, 200)
for b in ['Ender's Game', 'Rainbow Six', 'Snowcrash']:
self.assertContains(response, b)
def test_view_without_filterset_or_model(self):
factory = RequestFactory()
request = factory.get(self.base_url)
view = FilterView.as_view()
with self.assertRaises(ImproperlyConfigured):
view(request)
def test_view_with_bad_filterset(self):
class MyFilterSet(FilterSet):
pass
factory = RequestFactory()
request = factory.get(self.base_url)
view = FilterView.as_view(filterset_class=MyFilterSet)
with self.assertRaises(ImproperlyConfigured):
view(request)
class GenericFunctionalViewTests(GenericViewTestCase):
base_url = '/books-legacy/'
def test_view(self):
response = self.client.get(self.base_url)
for b in ['Ender's Game', 'Rainbow Six', 'Snowcrash']:
self.assertContains(response, b)
def test_view_filtering_on_price(self):
response = self.client.get(self.base_url + '?title=Snowcrash')
for b in ['Ender's Game', 'Rainbow Six']:
self.assertNotContains(response, b)
self.assertContains(response, 'Snowcrash')