Module shortner.tests.test_views
Expand source code
# from unittest import TestCase
from django.test import TestCase, Client
from django.urls import reverse
# from shortner.models import Link
import json
class TestViews(TestCase):
def setUp(self):
self.client = Client()
self.add_new_url = reverse("add_new")
self.update_url = reverse("update")
self.delete_url = reverse("delete")
def test_all_view(self):
req_long_url = "https://www.google.com/"
http_response = self.client.post(
self.add_new_url,
{"long_url": req_long_url},
content_type="application/json",
)
self.assertEquals(http_response.status_code, 201)
data = http_response.content
data = json.loads(data.decode("utf-8"))
long_url = data["long_url"]
stub = data["stub"]
special_code = data["special_code"]
self.assertEquals(10, len(stub))
self.assertNotEquals(9, len(stub))
self.assertEquals(long_url, req_long_url)
self.stub_url = reverse("stub", args=[stub])
http_response = self.client.get(self.stub_url)
self.assertEquals(http_response.status_code, 302)
new_long_url = "https://facebook.com/"
update_response = self.client.put(
self.update_url,
{
"new_long_url": new_long_url,
"special_code": special_code,
"stub": stub
},
content_type="application/json",
)
self.assertEquals(update_response.status_code, 201)
# Verify invalid put request with incorrect stub
update_response = self.client.put(
self.update_url,
{
"new_long_url": new_long_url,
"special_code": special_code,
"stub": stub + "0",
},
content_type="application/json",
)
self.assertEquals(update_response.status_code, 404)
self.stub_url = reverse("stub", args=[stub])
http_response = self.client.get(self.stub_url)
print(http_response)
self.assertEquals(http_response.status_code, 302)
self.assertEquals(http_response.url, new_long_url)
# Valid delete
delete_reponse = self.client.delete(
self.delete_url,
{"special_code": special_code},
content_type="application/json",
)
self.assertEquals(delete_reponse.status_code, 204)
# Verify error in deleting deleted entry
delete_reponse = self.client.delete(
self.delete_url,
{"special_code": special_code},
content_type="application/json",
)
self.assertEquals(delete_reponse.status_code, 404)
self.stub_url = reverse("stub", args=[stub])
http_response = self.client.get(self.stub_url)
self.assertEquals(http_response.status_code, 302)
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): self.client = Client() self.add_new_url = reverse("add_new") self.update_url = reverse("update") self.delete_url = reverse("delete") def test_all_view(self): req_long_url = "https://www.google.com/" http_response = self.client.post( self.add_new_url, {"long_url": req_long_url}, content_type="application/json", ) self.assertEquals(http_response.status_code, 201) data = http_response.content data = json.loads(data.decode("utf-8")) long_url = data["long_url"] stub = data["stub"] special_code = data["special_code"] self.assertEquals(10, len(stub)) self.assertNotEquals(9, len(stub)) self.assertEquals(long_url, req_long_url) self.stub_url = reverse("stub", args=[stub]) http_response = self.client.get(self.stub_url) self.assertEquals(http_response.status_code, 302) new_long_url = "https://facebook.com/" update_response = self.client.put( self.update_url, { "new_long_url": new_long_url, "special_code": special_code, "stub": stub }, content_type="application/json", ) self.assertEquals(update_response.status_code, 201) # Verify invalid put request with incorrect stub update_response = self.client.put( self.update_url, { "new_long_url": new_long_url, "special_code": special_code, "stub": stub + "0", }, content_type="application/json", ) self.assertEquals(update_response.status_code, 404) self.stub_url = reverse("stub", args=[stub]) http_response = self.client.get(self.stub_url) print(http_response) self.assertEquals(http_response.status_code, 302) self.assertEquals(http_response.url, new_long_url) # Valid delete delete_reponse = self.client.delete( self.delete_url, {"special_code": special_code}, content_type="application/json", ) self.assertEquals(delete_reponse.status_code, 204) # Verify error in deleting deleted entry delete_reponse = self.client.delete( self.delete_url, {"special_code": special_code}, content_type="application/json", ) self.assertEquals(delete_reponse.status_code, 404) self.stub_url = reverse("stub", args=[stub]) http_response = self.client.get(self.stub_url) self.assertEquals(http_response.status_code, 302)
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): self.client = Client() self.add_new_url = reverse("add_new") self.update_url = reverse("update") self.delete_url = reverse("delete")
def test_all_view(self)
-
Expand source code
def test_all_view(self): req_long_url = "https://www.google.com/" http_response = self.client.post( self.add_new_url, {"long_url": req_long_url}, content_type="application/json", ) self.assertEquals(http_response.status_code, 201) data = http_response.content data = json.loads(data.decode("utf-8")) long_url = data["long_url"] stub = data["stub"] special_code = data["special_code"] self.assertEquals(10, len(stub)) self.assertNotEquals(9, len(stub)) self.assertEquals(long_url, req_long_url) self.stub_url = reverse("stub", args=[stub]) http_response = self.client.get(self.stub_url) self.assertEquals(http_response.status_code, 302) new_long_url = "https://facebook.com/" update_response = self.client.put( self.update_url, { "new_long_url": new_long_url, "special_code": special_code, "stub": stub }, content_type="application/json", ) self.assertEquals(update_response.status_code, 201) # Verify invalid put request with incorrect stub update_response = self.client.put( self.update_url, { "new_long_url": new_long_url, "special_code": special_code, "stub": stub + "0", }, content_type="application/json", ) self.assertEquals(update_response.status_code, 404) self.stub_url = reverse("stub", args=[stub]) http_response = self.client.get(self.stub_url) print(http_response) self.assertEquals(http_response.status_code, 302) self.assertEquals(http_response.url, new_long_url) # Valid delete delete_reponse = self.client.delete( self.delete_url, {"special_code": special_code}, content_type="application/json", ) self.assertEquals(delete_reponse.status_code, 204) # Verify error in deleting deleted entry delete_reponse = self.client.delete( self.delete_url, {"special_code": special_code}, content_type="application/json", ) self.assertEquals(delete_reponse.status_code, 404) self.stub_url = reverse("stub", args=[stub]) http_response = self.client.get(self.stub_url) self.assertEquals(http_response.status_code, 302)