1
|
|
|
import unittest |
2
|
|
|
from django.test import RequestFactory |
3
|
|
|
from django.contrib.auth.models import User |
4
|
|
|
|
5
|
|
|
from django_th.views_userservices import UserServiceListView |
6
|
|
|
from django_th.models import UserService |
7
|
|
|
from django_th.tests.test_views import setup_view |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class UserServiceListViewTestCase(unittest.TestCase): |
11
|
|
|
|
12
|
|
|
def setUp(self): |
13
|
|
|
# Every test needs access to the request factory. |
14
|
|
|
self.factory = RequestFactory() |
15
|
|
|
try: |
16
|
|
|
self.user = User.objects.get(username='john') |
17
|
|
|
except User.DoesNotExist: |
18
|
|
|
self.user = User.objects.create_user( |
19
|
|
|
username='john', email='[email protected]', password='doe') |
20
|
|
|
|
21
|
|
|
def test_context_data(self): |
22
|
|
|
# Setup request and view |
23
|
|
|
queryset = UserService.objects.all() |
24
|
|
|
|
25
|
|
|
request = self.factory.get('/') |
26
|
|
|
request.user = self.user |
27
|
|
|
|
28
|
|
|
view = UserServiceListView( |
29
|
|
|
template_name='services/services.html', |
30
|
|
|
context_object_name="services_list", |
31
|
|
|
object_list=queryset) |
32
|
|
|
view = setup_view(view, request) |
33
|
|
|
|
34
|
|
|
context = view.get_context_data() |
35
|
|
|
|
36
|
|
|
if request.user.is_authenticated(): |
37
|
|
|
nb_user_service = nb_service = 20 |
38
|
|
|
context, action = self.get_action_context(context, |
39
|
|
|
nb_user_service, |
40
|
|
|
nb_service) |
41
|
|
|
self.assertEqual(context['action'], action) |
42
|
|
|
|
43
|
|
|
nb_user_service = 19 |
44
|
|
|
nb_service = 20 |
45
|
|
|
context, action = self.get_action_context(context, |
46
|
|
|
nb_user_service, |
47
|
|
|
nb_service) |
48
|
|
|
self.assertEqual(context['action'], action) |
49
|
|
|
|
50
|
|
|
def get_action_context(self, context, nb_user_service, nb_service): |
51
|
|
|
if nb_user_service == nb_service: |
52
|
|
|
context['action'] = 'hide' |
53
|
|
|
action = 'hide' |
54
|
|
|
else: |
55
|
|
|
context['action'] = 'display' |
56
|
|
|
action = 'display' |
57
|
|
|
|
58
|
|
|
return context, action |
59
|
|
|
|