Added tests to make sure content is saved with leading whitespaces in the db.

This commit is contained in:
Martin Mahner 2018-03-12 13:42:20 +01:00
parent 6255885bea
commit b1b7aa51a4
3 changed files with 21 additions and 5 deletions

View file

@ -239,3 +239,13 @@ class SnippetAPITestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(Snippet.objects.count(), 1) self.assertEqual(Snippet.objects.count(), 1)
self.assertEqual(Snippet.objects.all()[0].lexer, 'php') self.assertEqual(Snippet.objects.all()[0].lexer, 'php')
def test_leading_white_is_retained(self):
"""
Leading Whitespace is retained in the db.
"""
content = u' one\n two\n three\n four'
self.client.post(self.api_url, {'content': content})
self.assertEqual(Snippet.objects.all()[0].content, content)

View file

@ -401,3 +401,12 @@ class SnippetTestCase(TestCase):
slug_list = Snippet.objects.values_list( slug_list = Snippet.objects.values_list(
'secret_id', flat=True).order_by('published') 'secret_id', flat=True).order_by('published')
self.assertEqual(len(set(slug_list)), 100) self.assertEqual(len(set(slug_list)), 100)
def test_leading_white_is_retained_in_db(self):
"""
Leading Whitespace is retained in the db.
"""
content = u' one\n two\n three\n four'
data = self.valid_form_data(content=content)
self.client.post(self.new_url, data, follow=True)
self.assertEqual(Snippet.objects.all()[0].content, content)

View file

@ -6,15 +6,13 @@ import json
from django.conf import settings from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse
from django.db.models import Count from django.db.models import Count
from django.http import (Http404, HttpResponse, HttpResponseBadRequest, from django.http import (Http404, HttpResponse, HttpResponseBadRequest,
HttpResponseRedirect) HttpResponseRedirect)
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator from django.urls import reverse
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.defaults import page_not_found as django_page_not_found, \ from django.views.defaults import page_not_found as django_page_not_found, \
server_error as django_server_error server_error as django_server_error
from django.views.generic import FormView from django.views.generic import FormView
@ -112,7 +110,6 @@ class SnippetDetailView(SnippetView, DetailView):
def highlight_snippet(self): def highlight_snippet(self):
snippet = self.get_object() snippet = self.get_object()
h = pygmentize(snippet.content, snippet.lexer) h = pygmentize(snippet.content, snippet.lexer)
h = h.replace(u' ', u'  ')
h = h.replace(u'\t', '    ') h = h.replace(u'\t', '    ')
return h return h