From 447b9039a763df5baf3de566f5a75545435ecda7 Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Wed, 3 May 2017 21:51:24 -0400 Subject: [PATCH] Stop numerous sqlite databases from being created This makes a change so that a new sqlite database isn't created for every test case. There are currently issues with disconnecting from sessions in the beta version of the SQL adapter and these are preventing the test database file from being able to be deleted after the tests have completed. --- chatterbot/storage/sqlalchemy_storage.py | 5 ++++- .../test_sqlalchemy_adapter.py | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/chatterbot/storage/sqlalchemy_storage.py b/chatterbot/storage/sqlalchemy_storage.py index df213f2de..7b2f4fb53 100644 --- a/chatterbot/storage/sqlalchemy_storage.py +++ b/chatterbot/storage/sqlalchemy_storage.py @@ -43,7 +43,7 @@ class ResponseTable(_base): def get_reponse_serialized(context): params = context.current_parameters - del (params['text_search']) + del params['text_search'] return json.dumps(params) id = Column(Integer) @@ -216,6 +216,9 @@ def filter(self, **kwargs): return results + def get_session(self): + return self.__get_session() + def update(self, statement): """ Modifies an entry in the database. diff --git a/tests/storage_adapter_tests/test_sqlalchemy_adapter.py b/tests/storage_adapter_tests/test_sqlalchemy_adapter.py index 3cd781726..825be346f 100644 --- a/tests/storage_adapter_tests/test_sqlalchemy_adapter.py +++ b/tests/storage_adapter_tests/test_sqlalchemy_adapter.py @@ -1,26 +1,28 @@ from unittest import TestCase - from chatterbot.conversation import Statement, Response from chatterbot.storage.sqlalchemy_storage import SQLAlchemyDatabaseAdapter class SQLAlchemyAdapterTestCase(TestCase): + def setUp(self): """ - Instantiate the adapter. + Instantiate the adapter before any tests in the test case run. """ - from random import randint - - # Generate a random name for the database - database_name = str(randint(0, 9000)) - self.adapter = SQLAlchemyDatabaseAdapter( - database='sqlite_' + database_name, + database='testdb', drop_create=True ) + def tearDown(self): + """ + Remove the test database. + """ + self.adapter.drop() + class SQLAlchemyDatabaseAdapterTestCase(SQLAlchemyAdapterTestCase): + def test_count_returns_zero(self): """ The count method should return a value of 0 @@ -334,6 +336,7 @@ def test_response_list_in_results(self): class ReadOnlySQLAlchemyDatabaseAdapterTestCase(SQLAlchemyAdapterTestCase): + def test_update_does_not_add_new_statement(self): self.adapter.read_only = True