From bcdea97073f487c4aa67667552643fd762c0a437 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Fri, 22 Mar 2013 22:38:50 +0100 Subject: [PATCH] Snippet tests --- dpaste/tests/__init__.py | 5 ++- dpaste/tests/test_api.py | 33 +++++++++++-------- dpaste/tests/test_snippet.py | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 dpaste/tests/test_snippet.py diff --git a/dpaste/tests/__init__.py b/dpaste/tests/__init__.py index 858cf30..eac8a73 100644 --- a/dpaste/tests/__init__.py +++ b/dpaste/tests/__init__.py @@ -1 +1,4 @@ -from .test_api import * \ No newline at end of file +# -*- encoding: utf-8 -*- + +from .test_api import * +from .test_snippet import * diff --git a/dpaste/tests/test_api.py b/dpaste/tests/test_api.py index b64b350..a49b947 100644 --- a/dpaste/tests/test_api.py +++ b/dpaste/tests/test_api.py @@ -5,6 +5,8 @@ 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 SnippetAPITestCase(TestCase): @@ -12,31 +14,33 @@ class SnippetAPITestCase(TestCase): self.api_url = reverse('dpaste_api_create_snippet') 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): """ The browser sent a content field but with 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(Snippet.objects.count(), 0) # 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(Snippet.objects.count(), 0) # 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(Snippet.objects.count(), 0) @@ -44,8 +48,9 @@ class SnippetAPITestCase(TestCase): """ A valid snippet, contains Unicode, tabs, spaces, linebreaks etc. """ - content = u"Hello Wörld.\n\tGood Bye" - response = self.client.post(self.api_url, {'content': content}) + data = {'content': u"Hello Wörld.\n\tGood Bye"} + + response = self.client.post(self.api_url, data) self.assertEqual(response.status_code, 200) self.assertEqual(Snippet.objects.count(), 1) @@ -59,4 +64,4 @@ class SnippetAPITestCase(TestCase): response = self.client.get(snippet_url) self.assertEqual(response.status_code, 200) - self.assertContains(response, content) + self.assertContains(response, data['content']) diff --git a/dpaste/tests/test_snippet.py b/dpaste/tests/test_snippet.py new file mode 100644 index 0000000..65de29f --- /dev/null +++ b/dpaste/tests/test_snippet.py @@ -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) \ No newline at end of file