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

Memory and file tests #745

Merged
merged 10 commits into from
Jun 3, 2017
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
22 changes: 12 additions & 10 deletions chatterbot/storage/sqlalchemy_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from chatterbot.conversation import Response
from chatterbot.conversation import Statement


Base = None

try:
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class StatementTable(Base):
Expand Down Expand Up @@ -80,16 +80,18 @@ def __init__(self, **kwargs):
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

self.database_name = self.kwargs.get(
"database", "chatterbot-database"
)
self.database_name = self.kwargs.get("database")

if self.database_name:

# if some annoying blank space wrong...
db_name = self.database_name.strip()
# Create a sqlite file if a database name is provided
self.database_uri = self.kwargs.get(
"database_uri", "sqlite:///" + self.database_name + ".db"
)

# default uses sqlite
# The default uses sqlite in-memory database
self.database_uri = self.kwargs.get(
"database_uri", "sqlite:///" + db_name + ".db"
"database_uri", "sqlite://"
)

self.engine = create_engine(self.database_uri)
Expand Down Expand Up @@ -178,7 +180,7 @@ def filter(self, **kwargs):
if isinstance(_filter, list):
if len(_filter) == 0:
_query = _response_query.filter(
StatementTable.in_response_to == None) # NOQA Here must use == instead of is
StatementTable.in_response_to == None) # NOQA Here must use == instead of is
else:
for f in _filter:
_query = _response_query.filter(
Expand All @@ -187,7 +189,7 @@ def filter(self, **kwargs):
if fp == 'in_response_to__contains':
_query = _response_query.join(ResponseTable).filter(ResponseTable.text == _filter)
else:
_query = _response_query.filter(StatementTable.in_response_to == None) # NOQA
_query = _response_query.filter(StatementTable.in_response_to == None) # NOQA
else:
if _query:
_query = _query.filter(ResponseTable.text_search.like('%' + _filter + '%'))
Expand Down
16 changes: 16 additions & 0 deletions tests/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,19 @@ def get_kwargs(self):
kwargs['database'] = self.random_string()
kwargs['storage_adapter'] = 'chatterbot.storage.MongoDatabaseAdapter'
return kwargs


class ChatBotSQLTestCase(ChatBotTestCase):

def setUp(self):
"""
Create the tables in the database before each test is run.
"""
super(ChatBotSQLTestCase, self).setUp()
self.chatbot.storage.create()

def get_kwargs(self):
kwargs = super(ChatBotSQLTestCase, self).get_kwargs()
del kwargs['database']
kwargs['storage_adapter'] = 'chatterbot.storage.SQLAlchemyDatabaseAdapter'
return kwargs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tests.base_case import ChatBotTestCase
from tests.base_case import ChatBotSQLTestCase


class SqlAlchemyStorageIntegrationTests(ChatBotTestCase):
class SqlAlchemyStorageIntegrationTests(ChatBotSQLTestCase):

def test_database_is_updated(self):
"""
Expand Down
10 changes: 2 additions & 8 deletions tests/storage_adapter_tests/test_sqlalchemy_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ def setUpClass(cls):
"""
Instantiate the adapter before any tests in the test case run.
"""
cls.adapter = SQLAlchemyDatabaseAdapter(
database='testdb'
)

@classmethod
def tearDownClass(cls):
import os
os.remove('testdb.db')
cls.adapter = SQLAlchemyDatabaseAdapter()

def setUp(self):
"""
Expand Down Expand Up @@ -207,6 +200,7 @@ def test_get_response_statements(self):


class SQLAlchemyStorageAdapterFilterTestCase(SQLAlchemyAdapterTestCase):

def setUp(self):
super(SQLAlchemyStorageAdapterFilterTestCase, self).setUp()

Expand Down