Skip to content

Commit

Permalink
Merge API view test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jun 12, 2018
1 parent 885d648 commit c6b51c2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 65 deletions.
6 changes: 5 additions & 1 deletion examples/django_app/example_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def post(self, request, *args, **kwargs):
input_data = json.loads(request.read().decode('utf-8'))

if 'text' not in input_data:
raise ValidationError('The attribute "text" is required.')
return JsonResponse({
'text': [
'The attribute "text" is required.'
]
}, status=400)

conversation = self.get_conversation(request)

Expand Down
65 changes: 53 additions & 12 deletions examples/django_app/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,35 @@ def setUp(self):
super(ApiTestCase, self).setUp()
self.api_url = reverse('chatterbot')

def _get_json(self, response):
from django.utils.encoding import force_text
return json.loads(force_text(response.content))

def test_invalid_text(self):
response = self.client.post(
self.api_url,
data=json.dumps({
'type': 'classmethod'
}),
content_type='application/json',
format='json'
)

content = json.loads(response.content.decode('utf-8'))

self.assertEqual(response.status_code, 400)
self.assertIn('text', content)
self.assertEqual(['The attribute "text" is required.'], content['detail'])

def test_post(self):
"""
Test that a response is returned.
"""
data = {
'text': 'How are you?'
}
response = self.client.post(
self.api_url,
data=json.dumps(data),
data=json.dumps({
'text': 'How are you?'
}),
content_type='application/json',
format='json'
)
Expand All @@ -35,12 +54,11 @@ def test_post_unicode(self):
"""
Test that a response is returned.
"""
data = {
'text': u'سلام'
}
response = self.client.post(
self.api_url,
data=json.dumps(data),
data=json.dumps({
'text': u'سلام'
}),
content_type='application/json',
format='json'
)
Expand All @@ -56,12 +74,11 @@ def test_escaped_unicode_post(self):
"""
Test that unicode reponse
"""
data = {
'text': '\u2013'
}
response = self.client.post(
self.api_url,
data=json.dumps(data),
data=json.dumps({
'text': '\u2013'
}),
content_type='application/json',
format=json
)
Expand Down Expand Up @@ -94,6 +111,30 @@ def test_get(self):

self.assertEqual(response.status_code, 200)


def test_get_conversation_empty(self):
response = self.client.get(self.api_url)
data = self._get_json(response)

self.assertIn('conversation', data)
self.assertEqual(len(data['conversation']), 0)

def test_get_conversation(self):
self.client.post(
self.api_url,
data=json.dumps({'text': 'How are you?'}),
content_type='application/json',
format='json'
)

response = self.client.get(self.api_url)
data = self._get_json(response)

self.assertIn('conversation', data)
self.assertEqual(len(data['conversation']), 2)
self.assertIn('text', data['conversation'][0])
self.assertIn('text', data['conversation'][1])

def test_patch(self):
response = self.client.patch(self.api_url)

Expand Down
37 changes: 0 additions & 37 deletions examples/django_app/tests/test_api_view.py

This file was deleted.

15 changes: 0 additions & 15 deletions examples/django_app/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.test import TestCase
from django.core.exceptions import ValidationError
from example_app.views import ChatterBotApiView


Expand All @@ -15,20 +14,6 @@ def setUp(self):
super(ViewTestCase, self).setUp()
self.view = ChatterBotApiView()

def test_validate_text(self):
try:
self.view.validate({
'text': 'How are you?'
})
except ValidationError:
self.fail('Test raised ValidationError unexpectedly!')

def test_validate_invalid_text(self):
with self.assertRaises(ValidationError):
self.view.validate({
'type': 'classmethod'
})

def test_get_conversation(self):
conversation_id = self.view.chatterbot.storage.create_conversation()

Expand Down

0 comments on commit c6b51c2

Please sign in to comment.