Module todo.tests.test_views
Expand source code
from django.urls import reverse
from django.test import TestCase, Client, RequestFactory
from django.contrib.auth.models import User
from todo.views import login_request, template_from_todo, template, delete_todo, index
from django.utils import timezone
from todo.models import List, ListItem, Template, TemplateItem
class TestViews(TestCase):
def setUp(self):
# Every test needs access to the client and request factory.
self.client = Client()
self.factory = RequestFactory()
self.user = User.objects.create_user(
username='jacob', email='jacob@…', password='top_secret')
def testLogin(self):
request = self.factory.get('/login/')
request.user = self.user
post = request.POST.copy() # to make it mutable
post['todo'] = 1
request.POST = post
response = login_request(request)
self.assertEqual(response.status_code, 200)
def testSavingTodoList(self):
response = self.client.get(reverse('todo:createNewTodoList'))
self.assertEqual(response.status_code, 302)
# print(response)
def test_delete_todo_list(self):
request = self.factory.get('/todo/')
request.user = self.user
todo = List.objects.create(
title_text="test list",
created_on=timezone.now(),
updated_on=timezone.now(),
user_id_id=self.user.id
)
ListItem.objects.create(
item_name="test item",
item_text="This is a test item on a test list",
created_on=timezone.now(),
list=todo,
is_done=False,
)
post = request.POST.copy()
post['todo'] = 1
request.POST = post
response = delete_todo(request)
self.assertEqual(response.status_code, 302)
def test_index(self):
request = self.factory.get('/todo/')
request.user = self.user
response = index(request)
self.assertEqual(response.status_code, 200)
def test_template_from_todo_redirect(self):
client = self.client
response = client.get(reverse('todo:template_from_todo'))
self.assertEquals(response.status_code, 302)
def test_template_from_todo_function(self):
request = self.factory.get('/todo/')
request.user = self.user
todo = List.objects.create(
title_text="test list",
created_on=timezone.now(),
updated_on=timezone.now(),
user_id_id=request.user.id
)
item = ListItem.objects.create(
item_name="test item",
item_text="This is a test item on a test list",
created_on=timezone.now(),
list=todo,
is_done=False,
)
post = request.POST.copy() # to make it mutable
post['todo'] = 1
request.POST = post
response = template_from_todo(request)
self.assertEqual(response.status_code, 302)
def test_template_display(self):
request = self.factory.get('/todo/')
request.user = self.user
new_template = Template.objects.create(
title_text="test template",
created_on=timezone.now(),
updated_on=timezone.now(),
user_id_id=request.user.id
)
template_item = TemplateItem.objects.create(
item_text="test item",
created_on=timezone.now(),
template=new_template
)
post = request.POST.copy() # to make it mutable
post['todo'] = 1
request.POST = post
response = template(request, 1)
self.assertEqual(response.status_code, 200)
Classes
class TestViews (methodName='runTest')-
Similar to TransactionTestCase, but use
transaction.atomic()to achieve test isolation.In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).
On database backends with no transaction support, TestCase behaves as TransactionTestCase.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class TestViews(TestCase): def setUp(self): # Every test needs access to the client and request factory. self.client = Client() self.factory = RequestFactory() self.user = User.objects.create_user( username='jacob', email='jacob@…', password='top_secret') def testLogin(self): request = self.factory.get('/login/') request.user = self.user post = request.POST.copy() # to make it mutable post['todo'] = 1 request.POST = post response = login_request(request) self.assertEqual(response.status_code, 200) def testSavingTodoList(self): response = self.client.get(reverse('todo:createNewTodoList')) self.assertEqual(response.status_code, 302) # print(response) def test_delete_todo_list(self): request = self.factory.get('/todo/') request.user = self.user todo = List.objects.create( title_text="test list", created_on=timezone.now(), updated_on=timezone.now(), user_id_id=self.user.id ) ListItem.objects.create( item_name="test item", item_text="This is a test item on a test list", created_on=timezone.now(), list=todo, is_done=False, ) post = request.POST.copy() post['todo'] = 1 request.POST = post response = delete_todo(request) self.assertEqual(response.status_code, 302) def test_index(self): request = self.factory.get('/todo/') request.user = self.user response = index(request) self.assertEqual(response.status_code, 200) def test_template_from_todo_redirect(self): client = self.client response = client.get(reverse('todo:template_from_todo')) self.assertEquals(response.status_code, 302) def test_template_from_todo_function(self): request = self.factory.get('/todo/') request.user = self.user todo = List.objects.create( title_text="test list", created_on=timezone.now(), updated_on=timezone.now(), user_id_id=request.user.id ) item = ListItem.objects.create( item_name="test item", item_text="This is a test item on a test list", created_on=timezone.now(), list=todo, is_done=False, ) post = request.POST.copy() # to make it mutable post['todo'] = 1 request.POST = post response = template_from_todo(request) self.assertEqual(response.status_code, 302) def test_template_display(self): request = self.factory.get('/todo/') request.user = self.user new_template = Template.objects.create( title_text="test template", created_on=timezone.now(), updated_on=timezone.now(), user_id_id=request.user.id ) template_item = TemplateItem.objects.create( item_text="test item", created_on=timezone.now(), template=new_template ) post = request.POST.copy() # to make it mutable post['todo'] = 1 request.POST = post response = template(request, 1) self.assertEqual(response.status_code, 200)Ancestors
- django.test.testcases.TestCase
- django.test.testcases.TransactionTestCase
- django.test.testcases.SimpleTestCase
- unittest.case.TestCase
Methods
def setUp(self)-
Hook method for setting up the test fixture before exercising it.
Expand source code
def setUp(self): # Every test needs access to the client and request factory. self.client = Client() self.factory = RequestFactory() self.user = User.objects.create_user( username='jacob', email='jacob@…', password='top_secret') def testLogin(self)-
Expand source code
def testLogin(self): request = self.factory.get('/login/') request.user = self.user post = request.POST.copy() # to make it mutable post['todo'] = 1 request.POST = post response = login_request(request) self.assertEqual(response.status_code, 200) def testSavingTodoList(self)-
Expand source code
def testSavingTodoList(self): response = self.client.get(reverse('todo:createNewTodoList')) self.assertEqual(response.status_code, 302) # print(response) def test_delete_todo_list(self)-
Expand source code
def test_delete_todo_list(self): request = self.factory.get('/todo/') request.user = self.user todo = List.objects.create( title_text="test list", created_on=timezone.now(), updated_on=timezone.now(), user_id_id=self.user.id ) ListItem.objects.create( item_name="test item", item_text="This is a test item on a test list", created_on=timezone.now(), list=todo, is_done=False, ) post = request.POST.copy() post['todo'] = 1 request.POST = post response = delete_todo(request) self.assertEqual(response.status_code, 302) def test_index(self)-
Expand source code
def test_index(self): request = self.factory.get('/todo/') request.user = self.user response = index(request) self.assertEqual(response.status_code, 200) def test_template_display(self)-
Expand source code
def test_template_display(self): request = self.factory.get('/todo/') request.user = self.user new_template = Template.objects.create( title_text="test template", created_on=timezone.now(), updated_on=timezone.now(), user_id_id=request.user.id ) template_item = TemplateItem.objects.create( item_text="test item", created_on=timezone.now(), template=new_template ) post = request.POST.copy() # to make it mutable post['todo'] = 1 request.POST = post response = template(request, 1) self.assertEqual(response.status_code, 200) def test_template_from_todo_function(self)-
Expand source code
def test_template_from_todo_function(self): request = self.factory.get('/todo/') request.user = self.user todo = List.objects.create( title_text="test list", created_on=timezone.now(), updated_on=timezone.now(), user_id_id=request.user.id ) item = ListItem.objects.create( item_name="test item", item_text="This is a test item on a test list", created_on=timezone.now(), list=todo, is_done=False, ) post = request.POST.copy() # to make it mutable post['todo'] = 1 request.POST = post response = template_from_todo(request) self.assertEqual(response.status_code, 302) def test_template_from_todo_redirect(self)-
Expand source code
def test_template_from_todo_redirect(self): client = self.client response = client.get(reverse('todo:template_from_todo')) self.assertEquals(response.status_code, 302)