Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
some refactoring for clearest code
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Demah committed Apr 26, 2015
1 parent 5a642c4 commit be36b9d
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions django_th/tests/test_views_userservices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import unittest
from django.test import RequestFactory
from django.contrib.auth.models import User

from django_th.views_userservices import UserServiceAddedTemplateView
from django_th.views_userservices import UserServiceDeletedTemplateView
from django_th.views_userservices import UserServiceListView
from django_th.models import UserService
from django_th.tests.test_views import setup_view

class UserServiceAddedTemplateViewTestCase(unittest.TestCase):

def test_get(self):
template_name = 'services/thanks_service.html'
# Setup request and view.
request = RequestFactory().get('/th/service/add/thanks')
view = UserServiceAddedTemplateView.as_view(
template_name=template_name)
sentence = 'Your service has been successfully created'
# Run.
response = view(request)
# Check.
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.template_name[0], 'services/thanks_service.html')
self.assertEqual(response.context_data['sentence'], sentence)


class UserServiceDeletedTemplateViewTestCase(unittest.TestCase):

def test_get(self):
template_name = 'services/thanks_service.html'
# Setup request and view.
request = RequestFactory().get('/th/service/delete/thanks')
view = UserServiceDeletedTemplateView.as_view(
template_name=template_name)
sentence = 'Your service has been successfully deleted'
# Run.
response = view(request)
# Check.
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.template_name[0], 'services/thanks_service.html')
self.assertEqual(response.context_data['sentence'], sentence)


class UserServiceListViewTestCase(unittest.TestCase):

def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
try:
self.user = User.objects.get(username='john')
except User.DoesNotExist:
self.user = User.objects.create_user(
username='john', email='john@doe.info', password='doe')

def test_context_data(self):
# Setup request and view
queryset = UserService.objects.all()

request = self.factory.get('/')
request.user = self.user

view = UserServiceListView(
template_name='services/services.html',
context_object_name="services_list",
object_list=queryset)
view = setup_view(view, request)

context = view.get_context_data()

if request.user.is_authenticated():
nb_user_service = nb_service = 20
context, action = self.get_action_context(context, nb_user_service, nb_service)
self.assertEqual(context['action'], action)

nb_user_service = 19
nb_service = 20
context, action = self.get_action_context(context, nb_user_service, nb_service)
self.assertEqual(context['action'], action)

def get_action_context(self, context, nb_user_service, nb_service):
if nb_user_service == nb_service:
context['action'] = 'hide'
action = 'hide'
else:
context['action'] = 'display'
action = 'display'

return context, action

0 comments on commit be36b9d

Please sign in to comment.