Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'has_storage' method #492

Merged
merged 1 commit into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions chatterbot/logic/best_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@

class BestMatch(LogicAdapter):
"""
A logic adater that returns a response based on known responses to the
closest matches to the input statement.
A logic adater that returns a response based on known responses to
the closest matches to the input statement.
"""

@property
def has_storage(self):
"""
Return true if the adapter has access to the chatbot's storage adapter.
"""
return self.chatbot and self.chatbot.storage

def get(self, input_statement):
"""
Takes a statement string and a list of statement strings.
Expand All @@ -23,7 +16,7 @@ def get(self, input_statement):
statement_list = self.chatbot.storage.get_response_statements()

if not statement_list:
if self.has_storage:
if self.chatbot.storage.count():
# Use a randomly picked statement
self.logger.info(
'No statements have known responses. ' +
Expand All @@ -48,10 +41,10 @@ def get(self, input_statement):

def can_process(self, statement):
"""
Check that the chatbot's storage adapter is available to the logic adapter
and there is at least one statement in the database.
Check that the chatbot's storage adapter is available to the logic
adapter and there is at least one statement in the database.
"""
return self.has_storage and self.chatbot.storage.count()
return self.chatbot.storage.count()

def process(self, input_statement):

Expand Down
13 changes: 0 additions & 13 deletions tests/base_case.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import os
from mock import Mock
from unittest import TestCase
from unittest import SkipTest
from chatterbot import ChatBot


class MockChatBot(object):
def __init__(self):
from chatterbot.logic import LogicAdapter
from chatterbot.storage import StorageAdapter

self.storage = StorageAdapter()

self.storage.get_random = Mock(
side_effect=LogicAdapter.EmptyDatasetException()
)


class ChatBotTestCase(TestCase):

def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
from mock import MagicMock
from chatterbot.logic import BestMatch
from chatterbot.conversation import Statement, Response
from tests.base_case import MockChatBot
from tests.base_case import ChatBotTestCase


class BestMatchLevenshteinDistanceTestCase(TestCase):
class BestMatchLevenshteinDistanceTestCase(ChatBotTestCase):
"""
Integration tests for the BestMatch logic adapter
using Levenshtein distance as a comparison function.
"""

def setUp(self):
super(BestMatchLevenshteinDistanceTestCase, self).setUp()
from chatterbot.comparisons import levenshtein_distance

self.adapter = BestMatch(
statement_comparison_function=levenshtein_distance
)

# Add a mock chatbot to the logic adapter
self.adapter.set_chatbot(MockChatBot())
self.adapter.set_chatbot(self.chatbot)

def test_get_closest_statement(self):
"""
Expand Down Expand Up @@ -83,9 +82,7 @@ def test_no_known_responses(self):
should be zero because it is a random choice.
"""
self.adapter.chatbot.storage.update = MagicMock()
self.adapter.chatbot.storage.filter = MagicMock(
return_value=[]
)
self.adapter.chatbot.storage.count = MagicMock(return_value=1)
self.adapter.chatbot.storage.get_random = MagicMock(
return_value=Statement("Random")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from unittest import TestCase
from mock import MagicMock
from chatterbot.logic import BestMatch
from chatterbot.conversation import Statement, Response
from tests.base_case import MockChatBot
from tests.base_case import ChatBotTestCase


class BestMatchSynsetDistanceTestCase(TestCase):
class BestMatchSynsetDistanceTestCase(ChatBotTestCase):
"""
Integration tests for the BestMatch logic adapter
using the synset_distance comparison function.
"""

def setUp(self):
super(BestMatchSynsetDistanceTestCase, self).setUp()
from chatterbot.utils import nltk_download_corpus
from chatterbot.comparisons import synset_distance

Expand All @@ -24,7 +24,7 @@ def setUp(self):
)

# Add a mock storage adapter to the logic adapter
self.adapter.set_chatbot(MockChatBot())
self.adapter.set_chatbot(self.chatbot)

def test_get_closest_statement(self):
"""
Expand Down Expand Up @@ -68,9 +68,7 @@ def test_no_known_responses(self):
should be zero because it is a random choice.
"""
self.adapter.chatbot.storage.update = MagicMock()
self.adapter.chatbot.storage.filter = MagicMock(
return_value=[]
)
self.adapter.chatbot.storage.count = MagicMock(return_value=1)
self.adapter.chatbot.storage.get_random = MagicMock(
return_value=Statement('Random')
)
Expand Down
16 changes: 7 additions & 9 deletions tests/logic_adapter_tests/test_best_match.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
from unittest import TestCase
from mock import MagicMock
from chatterbot.logic import BestMatch
from chatterbot.conversation import Statement, Response
from tests.base_case import MockChatBot
from chatterbot.conversation import Statement
from tests.base_case import ChatBotTestCase


class BestMatchTestCase(TestCase):
class BestMatchTestCase(ChatBotTestCase):
"""
Unit tests for the BestMatch logic adapter.
"""

def setUp(self):
from chatterbot.comparisons import levenshtein_distance

super(BestMatchTestCase, self).setUp()
self.adapter = BestMatch()

# Add a mock chatbot to the logic adapter
self.adapter.set_chatbot(MockChatBot())
self.adapter.set_chatbot(self.chatbot)

def test_no_choices(self):
"""
An exception should be raised if there is no data in the database.
"""
self.adapter.chatbot.storage.filter = MagicMock(return_value=[])
self.adapter.chatbot.storage.count = MagicMock(return_value=0)

statement = Statement('What is your quest?')

with self.assertRaises(BestMatch.EmptyDatasetException):
Expand Down
8 changes: 4 additions & 4 deletions tests/logic_adapter_tests/test_low_confidence_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
from mock import MagicMock
from chatterbot.logic import LowConfidenceAdapter
from chatterbot.conversation import Statement, Response
from tests.base_case import MockChatBot
from tests.base_case import ChatBotTestCase


class LowConfidenceAdapterTestCase(TestCase):
class LowConfidenceAdapterTestCase(ChatBotTestCase):
"""
Test cases for the LowConfidenceAdapter
Test cases for the LowConfidenceAdapter.
"""

def setUp(self):
super(LowConfidenceAdapterTestCase, self).setUp()
self.adapter = LowConfidenceAdapter()

# Add a mock storage adapter to the logic adapter
self.adapter.set_chatbot(MockChatBot())
self.adapter.set_chatbot(self.chatbot)

possible_choices = [
Statement('Who do you love?', in_response_to=[
Expand Down