Skip to content

Commit

Permalink
Add missing serialize method to Response model
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jan 7, 2017
1 parent d81039f commit 8110c96
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions chatterbot/ext/django_chatterbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,15 @@ def __str__(self):
statement if len(statement) <= 20 else statement[:17] + '...',
response if len(response) <= 40 else response[:37] + '...'
)

def serialize(self):
"""
:returns: A dictionary representation of the statement object.
:rtype: dict
"""
data = {}

data['text'] = self.response.text
data['occurrence'] = self.occurrence

return data
23 changes: 23 additions & 0 deletions tests_django/integration_tests/test_statement_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from chatterbot.conversation import Statement as StatementObject
from chatterbot.conversation import Response as ResponseObject
from chatterbot.ext.django_chatterbot.models import Statement as StatementModel
from chatterbot.ext.django_chatterbot.models import Response as ResponseModel


class StatementIntegrationTestCase(TestCase):
Expand Down Expand Up @@ -82,3 +83,25 @@ def test_serialize(self):
def test_response_statement_cache(self):
self.assertTrue(hasattr(self.object, 'response_statement_cache'))
self.assertTrue(hasattr(self.model, 'response_statement_cache'))


class ResponseIntegrationTestCase(TestCase):

"""
Test case to make sure that the Django Response model
and ChatterBot Response object have a common interface.
"""

def setUp(self):
super(ResponseIntegrationTestCase, self).setUp()
date_created = timezone.now()
statement_object = StatementObject(text='_', created_at=date_created)
statement_model = StatementModel.objects.create(text='_', created_at=date_created)
self.object = ResponseObject(statement_object.text)
self.model = ResponseModel(statement=statement_model, response=statement_model)

def test_serialize(self):
object_data = self.object.serialize()
model_data = self.model.serialize()

self.assertEqual(object_data, model_data)

0 comments on commit 8110c96

Please sign in to comment.