Snippet tests

This commit is contained in:
Martin Mahner 2013-03-22 22:38:50 +01:00
parent ce29409340
commit bcdea97073
3 changed files with 86 additions and 15 deletions

View file

@ -1 +1,4 @@
from .test_api import * # -*- encoding: utf-8 -*-
from .test_api import *
from .test_snippet import *

View file

@ -5,6 +5,8 @@ from django.test.client import Client
from django.test import TestCase from django.test import TestCase
from ..models import Snippet from ..models import Snippet
from ..forms import EXPIRE_DEFAULT
from ..highlight import LEXER_DEFAULT
class SnippetAPITestCase(TestCase): class SnippetAPITestCase(TestCase):
@ -12,31 +14,33 @@ class SnippetAPITestCase(TestCase):
self.api_url = reverse('dpaste_api_create_snippet') self.api_url = reverse('dpaste_api_create_snippet')
self.client = Client() self.client = Client()
def test_no_post_data(self):
"""
No data passed. API returns a 400 Bad Request and no snippet
instance was created.
"""
response = self.client.post(self.api_url, {})
self.assertEqual(response.status_code, 400)
self.assertEqual(Snippet.objects.count(), 0)
def test_empty(self): def test_empty(self):
""" """
The browser sent a content field but with no data. The browser sent a content field but with no data.
""" """
# No data # No data
response = self.client.post(self.api_url, {'content': ''}) response = self.client.post(self.api_url, {})
self.assertEqual(response.status_code, 400)
self.assertEqual(Snippet.objects.count(), 0)
data = {}
# No content
data['content'] = ''
response = self.client.post(self.api_url, data)
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
self.assertEqual(Snippet.objects.count(), 0) self.assertEqual(Snippet.objects.count(), 0)
# Just some spaces # Just some spaces
response = self.client.post(self.api_url, {'content': ' '}) data['content'] = ' '
response = self.client.post(self.api_url, data)
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
self.assertEqual(Snippet.objects.count(), 0) self.assertEqual(Snippet.objects.count(), 0)
# Linebreaks or tabs only are not valid either # Linebreaks or tabs only are not valid either
response = self.client.post(self.api_url, {'content': '\n\t '}) data['content'] = '\n\t '
response = self.client.post(self.api_url, data)
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
self.assertEqual(Snippet.objects.count(), 0) self.assertEqual(Snippet.objects.count(), 0)
@ -44,8 +48,9 @@ class SnippetAPITestCase(TestCase):
""" """
A valid snippet, contains Unicode, tabs, spaces, linebreaks etc. A valid snippet, contains Unicode, tabs, spaces, linebreaks etc.
""" """
content = u"Hello Wörld.\n\tGood Bye" data = {'content': u"Hello Wörld.\n\tGood Bye"}
response = self.client.post(self.api_url, {'content': content})
response = self.client.post(self.api_url, data)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(Snippet.objects.count(), 1) self.assertEqual(Snippet.objects.count(), 1)
@ -59,4 +64,4 @@ class SnippetAPITestCase(TestCase):
response = self.client.get(snippet_url) response = self.client.get(snippet_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertContains(response, content) self.assertContains(response, data['content'])

View file

@ -0,0 +1,63 @@
# -*- encoding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.test.client import Client
from django.test import TestCase
from ..models import Snippet
from ..forms import EXPIRE_DEFAULT
from ..highlight import LEXER_DEFAULT
class SnippetTestCase(TestCase):
def setUp(self):
self.client = Client()
self.new_url = reverse('snippet_new')
def valid_form_data(self):
return {
'content': u"Hello Wörld.\n\tGood Bye",
'lexer': LEXER_DEFAULT,
'expire_options': EXPIRE_DEFAULT,
}
def test_empty(self):
"""
The browser sent a content field but with no data.
"""
# No data
self.client.post(self.new_url, {})
self.assertEqual(Snippet.objects.count(), 0)
data = self.valid_form_data()
# No content
data['content'] = ''
self.client.post(self.new_url, data)
self.assertEqual(Snippet.objects.count(), 0)
# Just some spaces
data['content'] = ' '
self.client.post(self.new_url, data)
self.assertEqual(Snippet.objects.count(), 0)
# Linebreaks or tabs only are not valid either
data['content'] = '\n\t '
self.client.post(self.new_url, data)
self.assertEqual(Snippet.objects.count(), 0)
def test_new_snippet(self):
data = self.valid_form_data()
response = self.client.post(self.new_url, data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(Snippet.objects.count(), 1)
self.assertContains(response, data['content'])
def test_reply(self):
data = self.valid_form_data()
response = self.client.post(self.new_url, data, follow=True)
response = self.client.post(response.request['PATH_INFO'], data, follow=True)
self.assertContains(response, data['content'])
self.assertEqual(response.status_code, 200)
self.assertEqual(Snippet.objects.count(), 2)