Skip to content

Commit

Permalink
Add ordering parameter to filter method
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Dec 27, 2016
1 parent 9255201 commit ab7197c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion chatterbot/storage/django_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def filter(self, **kwargs):
statements = StatementModel.objects.filter(Q(**kwargs) | Q(**parameters))

if order:
statements.order_by(order)
statements = statements.order_by(order)

return statements

Expand Down
8 changes: 8 additions & 0 deletions chatterbot/storage/jsonfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ def filter(self, **kwargs):
Returns a list of statements in the database
that match the parameters specified.
"""
from operator import attrgetter

results = []

order = kwargs.pop('order_by', None)

for key in self._keys():
values = self.database.data(key=key)

Expand All @@ -141,6 +145,10 @@ def filter(self, **kwargs):

results.append(self.json_to_object(values))

if order:
# Do an in place sort of the results
results.sort(key=attrgetter(order), reverse=False)

return results

def update(self, statement, **kwargs):
Expand Down
9 changes: 7 additions & 2 deletions chatterbot/storage/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def __init__(self, **kwargs):
from pymongo import MongoClient

self.database_name = self.kwargs.get(
"database", "chatterbot-database"
'database', 'chatterbot-database'
)
self.database_uri = self.kwargs.get(
"database_uri", "mongodb://localhost:27017/"
'database_uri', 'mongodb://localhost:27017/'
)

# Use the default host and port
Expand Down Expand Up @@ -166,6 +166,8 @@ def filter(self, **kwargs):
"""
query = self.base_query

order_by = kwargs.pop('order_by', None)

# Convert Response objects to data
if 'in_response_to' in kwargs:
serialized_responses = []
Expand All @@ -185,6 +187,9 @@ def filter(self, **kwargs):

matches = self.statements.find(query.value())

if order_by:
matches = matches.sort(order_by)

results = []

for match in list(matches):
Expand Down
43 changes: 43 additions & 0 deletions tests/storage_adapter_tests/test_json_file_storage_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,49 @@ def test_response_list_in_results(self):
self.assertEqual(type(found[0].in_response_to[0]), Response)


class JsonFileStorageOrderingTestCase(JsonAdapterTestCase):
"""
Test cases for the ordering of sets of statements.
"""

def test_order_by_text(self):
statement_a = Statement(text='A is the first letter of the alphabet.')
statement_b = Statement(text='B is the second letter of the alphabet.')

self.adapter.update(statement_a)
self.adapter.update(statement_b)

results = self.adapter.filter(order_by='text')

self.assertEqual(len(results), 2)
self.assertEqual(results[0], statement_a)
self.assertEqual(results[1], statement_b)

def test_order_by_created_at(self):
from datetime import datetime, timedelta

today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)

statement_a = Statement(
text='A is the first letter of the alphabet.',
created_at=today
)
statement_b = Statement(
text='B is the second letter of the alphabet.',
created_at=yesterday
)

self.adapter.update(statement_a)
self.adapter.update(statement_b)

results = self.adapter.filter(order_by='created_at')

self.assertEqual(len(results), 2)
self.assertEqual(results[0], statement_b)
self.assertEqual(results[1], statement_a)


class ReadOnlyJsonFileStorageAdapterTestCase(JsonAdapterTestCase):

def test_update_does_not_add_new_statement(self):
Expand Down
43 changes: 43 additions & 0 deletions tests/storage_adapter_tests/test_mongo_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,49 @@ def test_response_list_in_results(self):
self.assertEqual(type(found[0].in_response_to[0]), Response)


class MongoOrderingTestCase(MongoAdapterTestCase):
"""
Test cases for the ordering of sets of statements.
"""

def test_order_by_text(self):
statement_a = Statement(text='A is the first letter of the alphabet.')
statement_b = Statement(text='B is the second letter of the alphabet.')

self.adapter.update(statement_a)
self.adapter.update(statement_b)

results = self.adapter.filter(order_by='text')

self.assertEqual(len(results), 2)
self.assertEqual(results[0], statement_a)
self.assertEqual(results[1], statement_b)

def test_order_by_created_at(self):
from datetime import datetime, timedelta

today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)

statement_a = Statement(
text='A is the first letter of the alphabet.',
created_at=today
)
statement_b = Statement(
text='B is the second letter of the alphabet.',
created_at=yesterday
)

self.adapter.update(statement_a)
self.adapter.update(statement_b)

results = self.adapter.filter(order_by='created_at')

self.assertEqual(len(results), 2)
self.assertEqual(results[0], statement_b)
self.assertEqual(results[1], statement_a)


class ReadOnlyMongoDatabaseAdapterTestCase(MongoAdapterTestCase):

def test_update_does_not_add_new_statement(self):
Expand Down
28 changes: 28 additions & 0 deletions tests_django/test_django_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,34 @@ def test_response_list_in_results(self):
self.assertEqual(type(found[0].in_response_to[0]), ResponseModel)


class DjangoOrderingTestCase(DjangoStorageAdapterTestCase):
"""
Test cases for the ordering of sets of statements.
"""

def test_order_by_text(self):
statement_a = StatementModel.objects.create(text='A is the first letter of the alphabet.')
statement_b = StatementModel.objects.create(text='B is the second letter of the alphabet.')

results = self.adapter.filter(order_by='text')

self.assertEqual(len(results), 2)
self.assertEqual(results[0], statement_a)
self.assertEqual(results[1], statement_b)

def test_order_by_created_at(self):
from datetime import datetime, timedelta

statement_b = StatementModel.objects.create(text='B is the second letter of the alphabet.')
statement_a = StatementModel.objects.create(text='A is the first letter of the alphabet.')

results = self.adapter.filter(order_by='created_at')

self.assertEqual(len(results), 2)
self.assertEqual(results[0], statement_b)
self.assertEqual(results[1], statement_a)


class ReadOnlyDjangoAdapterTestCase(DjangoAdapterTestCase):

def test_update_does_not_add_new_statement(self):
Expand Down

0 comments on commit ab7197c

Please sign in to comment.