-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests.py
181 lines (139 loc) · 6.34 KB
/
tests.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.test import RequestFactory, TestCase
from django.utils.encoding import force_text
from feder.main.mixins import PermissionStatusMixin
from feder.teryt.factories import JSTFactory
from feder.users.factories import UserFactory
from .factories import InstitutionFactory, TagFactory
from .models import Institution
from .serializers import InstitutionSerializer
from .views import InstitutionAutocomplete, TagAutocomplete
class InstitutionTestCase(TestCase):
def setUp(self):
self.obj = InstitutionFactory(name="Example institution")
def test_get_absolute_url(self):
self.assertEqual(self.obj.get_absolute_url(),
'/instytucje/example-institution')
def test_get_str(self):
self.assertEqual(force_text(self.obj),
"Example institution")
class InstitutionSerializerTestCase(TestCase):
def setUp(self):
self.data = {
"name": "xxx",
"slug": "xxx",
"tags": [
"blabla", "X", "Z"
],
"jst": JSTFactory().pk,
"email": "example-2@example.com",
}
def test_create_institution(self):
serializer = InstitutionSerializer(data={
'name': 'X',
"email": "example-2@example.com",
'jst': JSTFactory().pk,
})
self.assertTrue(serializer.is_valid(), serializer.errors)
self.assertTrue(serializer.save())
def test_create_institution_with_tags(self):
TagFactory(name="X") # for colission tag name
serializer = InstitutionSerializer(data=self.data)
self.assertTrue(serializer.is_valid(), serializer.errors)
obj = serializer.save()
self.assertQuerysetEqual(qs=obj.tags.all(),
values=["blabla", "X", "Z"],
transform=force_text)
def test_update_institution_with_tags(self):
institution = InstitutionFactory()
self.data['pk'] = institution.pk
serializer = InstitutionSerializer(institution, data=self.data)
self.assertTrue(serializer.is_valid(), serializer.errors)
obj = serializer.save()
self.assertQuerysetEqual(qs=obj.tags.all(),
values=["blabla", "X", "Z"],
transform=force_text)
self.assertEqual(Institution.objects.count(), 1) # updated
class ObjectMixin(object):
def setUp(self):
self.user = UserFactory(username='john')
self.institution = InstitutionFactory()
class InstitutionListViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
url = reverse('institutions:list')
status_anonymous = 200
status_no_permission = 200
permission = []
def test_content(self):
response = self.client.get(self.get_url())
self.assertTemplateUsed(response, 'institutions/institution_filter.html')
self.assertContains(response, self.institution.name)
class InstitutionDetailViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
status_anonymous = 200
status_no_permission = 200
permission = []
def get_url(self):
return self.institution.get_absolute_url()
def test_content(self):
response = self.client.get(self.get_url())
self.assertTemplateUsed(response, 'institutions/institution_detail.html')
self.assertContains(response, self.institution.name)
class InstitutionCreateViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
url = reverse('institutions:create')
permission = ['institutions.add_institution', ]
class InstitutionUpdateViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
permission = ['institutions.change_institution', ]
def get_url(self):
return reverse('institutions:update', kwargs={'slug': self.institution.slug})
class InstitutionDeleteViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
permission = ['institutions.delete_institution', ]
def get_url(self):
return reverse('institutions:delete', kwargs={'slug': self.institution.slug})
class InstitutionAutocompleteTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_filter_by_name(self):
InstitutionFactory(name='123')
InstitutionFactory(name='456')
request = self.factory.get('/customer/details', data={'q': '123'})
response = InstitutionAutocomplete.as_view()(request)
self.assertContains(response, '123')
self.assertNotContains(response, '456')
class TagAutocompleteTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_filter_by_name(self):
TagFactory(name='123')
TagFactory(name='456')
request = self.factory.get('/customer/details', data={'q': '123'})
response = TagAutocomplete.as_view()(request)
self.assertContains(response, '123')
self.assertNotContains(response, '456')
def test_get_result_label_without_institution(self):
TagFactory(name='123')
request = self.factory.get('/customer/details', data={'q': '123'})
response = TagAutocomplete.as_view()(request)
self.assertContains(response, '123 (0)')
def test_get_result_label_with_institution(self):
institution = InstitutionFactory()
institution.tags.add(TagFactory(name='123'))
institution.save()
request = self.factory.get('/customer/details', data={'q': '123'})
response = TagAutocomplete.as_view()(request)
self.assertContains(response, '123 (1)')
class SitemapTestCase(ObjectMixin, TestCase):
def test_institutions(self):
url = reverse('sitemaps', kwargs={'section': 'institutions'})
needle = reverse('institutions:details', kwargs={'slug': self.institution})
response = self.client.get(url)
self.assertContains(response, needle)
def test_tags(self):
tag_used = TagFactory()
institution = InstitutionFactory()
institution.tags.add(tag_used)
institution.save()
tag_free = TagFactory()
url = reverse('sitemaps', kwargs={'section': 'institutions_tags'})
response = self.client.get(url)
self.assertContains(response, tag_used.get_absolute_url())
self.assertNotContains(response, tag_free.get_absolute_url())