Skip to content

Commit

Permalink
Remove messed up string thing
Browse files Browse the repository at this point in the history
This removes a mess of code in the API view that was replacing the
response dictionary with a utf-8 encoded string and then putting
that string inside another dictionary.

Tests have been updated to make sure that this won't happen in the
future.
  • Loading branch information
gunthercox committed Jan 28, 2017
1 parent d71240a commit 54c96e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
14 changes: 0 additions & 14 deletions chatterbot/ext/django_chatterbot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,6 @@ def post(self, request, *args, **kwargs):
response = self.chatterbot.get_response(input_data, chat_session.id_string)
response_data = response.serialize()

try:
unicode = unicode
except NameError:
# Must be python 3
unicode = str
basestring = (str, bytes)

if isinstance(response_data, basestring):
response_data = response_data.encode('utf8')
else:
response_data = unicode(response_data).encode('utf8')

response_data = {'text': str(response_data)}

return JsonResponse(response_data, status=200)

def get(self, request, *args, **kwargs):
Expand Down
45 changes: 28 additions & 17 deletions tests_django/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import json
from django.test import TestCase
from django.core.urlresolvers import reverse
Expand All @@ -13,6 +14,9 @@ def test_post(self):
"""
Test that a response is returned.
"""
from datetime import datetime
from dateutil.parser import parse

data = {
'text': 'How are you?'
}
Expand All @@ -23,25 +27,23 @@ def test_post(self):
format='json'
)

content = json.loads(response.content)

self.assertEqual(response.status_code, 200)
self.assertIn('text', str(response.content))
self.assertIn('in_response_to', str(response.content))
self.assertIn('text', content)
self.assertGreater(len(content['text']), 1)
self.assertIn('in_response_to', content)
self.assertIn('created_at', content)
self.assertTrue(
isinstance(parse(content['created_at']), datetime)
)

def test_response_is_in_json(self):
def test_post_unicode(self):
"""
Test Response is in JSON
Test that a response is returned.
"""
def is_json(content):
try:
if isinstance(content, bytes):
content = content.decode(encoding='utf-8')
json_object = json.loads(content)
except ValueError:
return False
return True

data = {
'text': 'Is this JSON Data?'
'text': u'سلام'
}
response = self.client.post(
self.api_url,
Expand All @@ -50,15 +52,19 @@ def is_json(content):
format='json'
)

content = json.loads(response.content)

self.assertEqual(response.status_code, 200)
self.assertTrue(is_json(response.content))
self.assertIn('text', content)
self.assertGreater(len(content['text']), 1)
self.assertIn('in_response_to', content)

def test_unicode_post(self):
def test_escaped_unicode_post(self):
"""
Test that unicode reponse
"""
data = {
'text' : u'\u2013'
'text' : '\u2013'
}
response = self.client.post(
self.api_url,
Expand Down Expand Up @@ -100,6 +106,11 @@ def test_patch(self):

self.assertEqual(response.status_code, 405)

def test_put(self):
response = self.client.put(self.api_url)

self.assertEqual(response.status_code, 405)

def test_delete(self):
response = self.client.delete(self.api_url)

Expand Down

0 comments on commit 54c96e4

Please sign in to comment.