def setUp(self): super().setUp() self.statement1 = Statement(text="Testing...", in_response_to="Why are you counting?") self.statement2 = Statement(text="Testing one, two, three.", in_response_to=self.statement1.text)
def setUp(self): super(DjangoAdapterFilterTestCase, self).setUp() self.statement1 = StatementModel(text="Testing...") self.statement1.add_response( StatementModel(text="Why are you counting?")) self.statement2 = StatementModel(text="Testing one, two, three.") self.statement2.add_response(self.statement1)
def setUp(self): super(DjangoAdapterFilterTestCase, self).setUp() self.statement1 = StatementModel(text="Testing...") self.statement1.add_response( StatementModel(text="Why are you counting?") ) self.statement2 = StatementModel(text="Testing one, two, three.") self.statement2.add_response(self.statement1)
def test_confidence(self): """ Test that the confidence value is not saved to the database. The confidence attribute on statements is intended to just hold the confidence of the statement when it returned as a response to some input. Because of that, the value of the confidence score should never be stored in the database with the statement. """ statement = StatementModel(text='Test statement') statement.confidence = 0.5 statement.save() statement_updated = StatementModel.objects.get(pk=statement.id) self.assertEqual(statement_updated.confidence, 0)
def test_confidence(self): """ Test that the confidence value is not saved to the database. The confidence attribute on statements is intended to just hold the confidence of the statement when it returned as a response to some input. Because of that, the value of the confidence score should never be stored in the database with the statement. """ statement = StatementModel(text='Test statement') statement.confidence = 0.5 statement.save() statement_updated = StatementModel.objects.get(pk=statement.id) self.assertEqual(statement_updated.confidence, 0)
def test_update_adds_new_statement(self): statement = Statement(text="New statement") self.adapter.update(statement) results = self.adapter.filter(text="New statement") self.assertEqual(results.count(), 1) self.assertEqual(results.first().text, statement.text)
def test_update_adds_new_statement(self): statement = Statement(text="New statement") self.adapter.update(statement) results = list(self.adapter.filter(text="New statement")) self.assertEqual(len(results), 1) self.assertEqual(results[0].text, statement.text)
def test_input_adapter(self): adapter = InputAdapter(self.chatbot) statement = Statement(text='_') result = adapter.process_input(statement) self.assertEqual(result.text, '_')
def test_output_adapter(self): from chatterbot.output import OutputAdapter adapter = OutputAdapter() statement = Statement(text='_') result = adapter.process_response(statement) self.assertEqual(result.text, '_')
class StatementIntegrationTestCase(TestCase): """ Test case to make sure that the Django Statement model and ChatterBot Statement object have a common interface. """ def setUp(self): super(StatementIntegrationTestCase, self).setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now) def test_text(self): self.assertTrue(hasattr(self.object, 'text')) self.assertTrue(hasattr(self.model, 'text')) def test_in_response_to(self): self.assertTrue(hasattr(self.object, 'in_response_to')) self.assertTrue(hasattr(self.model, 'in_response_to')) def test_extra_data(self): self.assertTrue(hasattr(self.object, 'extra_data')) self.assertTrue(hasattr(self.model, 'extra_data')) def test__str__(self): self.assertTrue(hasattr(self.object, '__str__')) self.assertTrue(hasattr(self.model, '__str__')) self.assertEqual(str(self.object), str(self.model)) def test_add_extra_data(self): self.object.add_extra_data('key', 'value') self.model.add_extra_data('key', 'value') def test_serialize(self): object_data = self.object.serialize() model_data = self.model.serialize() self.assertEqual(object_data, model_data)
def test_low_confidence(self): from chatterbot.logic import LowConfidenceAdapter adapter = LowConfidenceAdapter(self.chatbot) statement = Statement(text='Why is the sky blue?') response = adapter.process(statement) self.assertEqual(response.text, adapter.default_responses[0])
def test_variable_type_input_adapter(self): from chatterbot.input import VariableInputTypeAdapter adapter = VariableInputTypeAdapter() statement = Statement(text='_') result = adapter.process_input(statement) self.assertEqual(result.text, '_')
def setUp(self): super(StatementIntegrationTestCase, self).setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now)
def test_mathematical_evaluation(self): from chatterbot.logic import MathematicalEvaluation adapter = MathematicalEvaluation(self.chatbot) statement = Statement(text='What is 6 + 6?') response = adapter.process(statement) self.assertEqual(response.text, '6 + 6 = 12') self.assertEqual(response.confidence, 1)
def test_time(self): from chatterbot.logic import TimeLogicAdapter adapter = TimeLogicAdapter(self.chatbot) statement = Statement(text='What time is it?') response = adapter.process(statement) self.assertIn('The current time is', response.text) self.assertEqual(response.confidence, 1)
def setUp(self): super().setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now) # Simulate both statements being saved self.model.save() self.object.id = self.model.id
def setUp(self): super().setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now) # Simulate both statements being saved self.model.save() self.object.id = self.model.id
class StatementIntegrationTestCase(TestCase): """ Test case to make sure that the Django Statement model and ChatterBot Statement object have a common interface. """ def setUp(self): super(StatementIntegrationTestCase, self).setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now) # Simulate both statements being saved self.model.save() self.object.id = self.model.id def test_text(self): self.assertTrue(hasattr(self.object, 'text')) self.assertTrue(hasattr(self.model, 'text')) def test_in_response_to(self): self.assertTrue(hasattr(self.object, 'in_response_to')) self.assertTrue(hasattr(self.model, 'in_response_to')) def test_conversation(self): self.assertTrue(hasattr(self.object, 'conversation')) self.assertTrue(hasattr(self.model, 'conversation')) def test_tags(self): self.assertTrue(hasattr(self.object, 'tags')) self.assertTrue(hasattr(self.model, 'tags')) def test__str__(self): self.assertTrue(hasattr(self.object, '__str__')) self.assertTrue(hasattr(self.model, '__str__')) self.assertEqual(str(self.object), str(self.model)) def test_add_tags(self): self.object.add_tags('a', 'b') self.model.add_tags('a', 'b') self.assertIn('a', self.object.get_tags()) self.assertIn('a', self.model.get_tags()) def test_serialize(self): object_data = self.object.serialize() model_data = self.model.serialize() self.assertEqual(object_data, model_data)
class StatementIntegrationTestCase(TestCase): """ Test case to make sure that the Django Statement model and ChatterBot Statement object have a common interface. """ def setUp(self): super().setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now) # Simulate both statements being saved self.model.save() self.object.id = self.model.id def test_text(self): self.assertTrue(hasattr(self.object, 'text')) self.assertTrue(hasattr(self.model, 'text')) def test_in_response_to(self): self.assertTrue(hasattr(self.object, 'in_response_to')) self.assertTrue(hasattr(self.model, 'in_response_to')) def test_conversation(self): self.assertTrue(hasattr(self.object, 'conversation')) self.assertTrue(hasattr(self.model, 'conversation')) def test_tags(self): self.assertTrue(hasattr(self.object, 'tags')) self.assertTrue(hasattr(self.model, 'tags')) def test__str__(self): self.assertTrue(hasattr(self.object, '__str__')) self.assertTrue(hasattr(self.model, '__str__')) self.assertEqual(str(self.object), str(self.model)) def test_add_tags(self): self.object.add_tags('a', 'b') self.model.add_tags('a', 'b') self.assertIn('a', self.object.get_tags()) self.assertIn('a', self.model.get_tags()) def test_serialize(self): object_data = self.object.serialize() model_data = self.model.serialize() self.assertEqual(object_data, model_data)
def createpost(request): if request.method == 'POST': if request.POST.get('title') and request.POST.get('content'): post = Statement() post.text = request.POST.get('title') post.in_response_to = request.POST.get('content') post.save() return render(request, 'try.html') else: return render(request, 'try.html')
def test_best_match(self): from chatterbot.logic import BestMatch adapter = BestMatch() adapter.set_chatbot(self.chatbot) statement1 = Statement(text='Do you like programming?') statement1.save() statement2 = Statement(text='Yes') statement2.save() response = Response(statement=statement1, response=statement2) response.save() response = adapter.process(statement1) self.assertEqual(response.text, 'Yes') self.assertEqual(response.confidence, 1)
def test_best_match(self): from chatterbot.logic import BestMatch adapter = BestMatch() adapter.set_chatbot(self.chatbot) statement1 = Statement(text='Do you like programming?') statement1.save() statement2 = Statement(text='Yes') statement2.save() response = Response(statement=statement1, response=statement2) response.save() response = adapter.process(statement1) self.assertEqual(response.text, 'Yes') self.assertEqual(response.confidence, 1)
def test_get_response_statements(self): """ Test that we are able to get a list of only statements that are known to be in response to another statement. """ s1 = StatementModel(text="What... is your quest?") s2 = StatementModel(text="This is a phone.") s3 = StatementModel(text="A what?") s4 = StatementModel(text="A phone.") s3.add_response(s2) s4.add_response(s3) for statement in [s1, s2, s3, s4]: self.adapter.update(statement) responses = self.adapter.get_response_statements() self.assertEqual(len(responses), 2) self.assertTrue(responses.filter(in_response__response__text="This is a phone.").exists()) self.assertTrue(responses.filter(in_response__response__text="A what?").exists())
def test_get_response_statements(self): """ Test that we are able to get a list of only statements that are known to be in response to another statement. """ s1 = StatementModel(text="What... is your quest?") s2 = StatementModel(text="This is a phone.") s3 = StatementModel(text="A what?") s4 = StatementModel(text="A phone.") s3.add_response(s2) s4.add_response(s3) for statement in [s1, s2, s3, s4]: self.adapter.update(statement) responses = self.adapter.get_response_statements() self.assertEqual(len(responses), 2) self.assertTrue(responses.filter(in_response__statement__text="This is a phone.").exists()) self.assertTrue(responses.filter(in_response__statement__text="A what?").exists())
def setUp(self): super(StatementIntegrationTestCase, self).setUp() date_created = timezone.now() self.object = StatementObject(text='_', created_at=date_created) self.model = StatementModel(text='_', created_at=date_created)
class DjangoAdapterFilterTestCase(DjangoAdapterTestCase): def setUp(self): super(DjangoAdapterFilterTestCase, self).setUp() self.statement1 = StatementModel(text="Testing...") self.statement1.add_response( StatementModel(text="Why are you counting?") ) self.statement2 = StatementModel(text="Testing one, two, three.") self.statement2.add_response(self.statement1) def test_filter_text_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(text="Howdy") self.assertEqual(len(results), 0) def test_filter_in_response_to_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(in_response_to="Maybe") self.assertEqual(len(results), 0) def test_filter_equal_results(self): statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter(in_response_to=[]) self.assertEqual(results.count(), 2) self.assertTrue(results.filter(text=statement1.text).exists()) self.assertTrue(results.filter(text=statement2.text).exists()) def test_filter_contains_result(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( in_response_to__contains="Why are you counting?" ) self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_contains_no_result(self): self.adapter.update(self.statement1) results = self.adapter.filter( in_response_to__contains="How do you do?" ) self.assertEqual(results.count(), 0) def test_filter_multiple_parameters(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Testing...", in_response_to__contains="Why are you counting?" ) self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_multiple_parameters_no_results(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Test", in_response_to__contains="Not an existing response." ) self.assertEqual(len(results), 0) def test_filter_no_parameters(self): """ If no parameters are passed to the filter, then all statements should be returned. """ statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter() self.assertEqual(len(results), 2) def test_filter_returns_statement_with_multiple_responses(self): statement = StatementModel.objects.create(text="You are welcome.") statement.add_response(StatementModel(text="Thanks.")) statement.add_response(StatementModel(text="Thank you.")) self.adapter.update(statement) response = self.adapter.filter( in_response_to__contains="Thanks." ) # Get the first response response = response[0] self.assertEqual(len(response.in_response_to), 2) def test_response_list_in_results(self): """ If a statement with response values is found using the filter method, they should be returned as response objects. """ statement = StatementModel.objects.create( text="The first is to help yourself, the second is to help others.", ) statement.add_response(StatementModel(text="Why do people have two hands?")) self.adapter.update(statement) found = self.adapter.filter(text=statement.text) self.assertEqual(len(found[0].in_response_to), 1) self.assertEqual(type(found[0].in_response_to[0]), ResponseModel)
class DjangoAdapterFilterTestCase(DjangoAdapterTestCase): def setUp(self): super(DjangoAdapterFilterTestCase, self).setUp() self.statement1 = StatementModel(text="Testing...") self.statement1.add_response( StatementModel(text="Why are you counting?")) self.statement2 = StatementModel(text="Testing one, two, three.") self.statement2.add_response(self.statement1) def test_filter_text_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(text="Howdy") self.assertEqual(len(results), 0) def test_filter_in_response_to_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(in_response_to="Maybe") self.assertEqual(len(results), 0) def test_filter_equal_results(self): statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter(in_response_to=[]) self.assertEqual(results.count(), 2) self.assertTrue(results.filter(text=statement1.text).exists()) self.assertTrue(results.filter(text=statement2.text).exists()) def test_filter_contains_result(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( in_response_to__contains="Why are you counting?") self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_contains_no_result(self): self.adapter.update(self.statement1) results = self.adapter.filter( in_response_to__contains="How do you do?") self.assertEqual(results.count(), 0) def test_filter_multiple_parameters(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Testing...", in_response_to__contains="Why are you counting?") self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_multiple_parameters_no_results(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Test", in_response_to__contains="Not an existing response.") self.assertEqual(len(results), 0) def test_filter_no_parameters(self): """ If no parameters are passed to the filter, then all statements should be returned. """ statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter() self.assertEqual(len(results), 2) def test_filter_returns_statement_with_multiple_responses(self): statement = StatementModel.objects.create(text="You are welcome.") statement.add_response(StatementModel(text="Thanks.")) statement.add_response(StatementModel(text="Thank you.")) self.adapter.update(statement) response = self.adapter.filter(in_response_to__contains="Thanks.") # Get the first response response = response.first() self.assertEqual(response.responses.count(), 2) def test_response_list_in_results(self): """ If a statement with response values is found using the filter method, they should be returned as response objects. """ statement = StatementModel.objects.create( text="The first is to help yourself, the second is to help others.", ) statement.add_response( StatementModel(text="Why do people have two hands?")) self.adapter.update(statement) found = self.adapter.filter(text=statement.text) self.assertEqual(found.count(), 1) self.assertEqual(found.first().responses.count(), 1) self.assertEqual(type(found.first().responses.first()), ResponseModel) def test_confidence(self): """ Test that the confidence value is not saved to the database. The confidence attribute on statements is intended to just hold the confidence of the statement when it returned as a response to some input. Because of that, the value of the confidence score should never be stored in the database with the statement. """ statement = StatementModel(text='Test statement') statement.confidence = 0.5 statement.save() statement_updated = StatementModel.objects.get(pk=statement.id) self.assertEqual(statement_updated.confidence, 0)
class DjangoAdapterFilterTestCase(DjangoAdapterTestCase): def setUp(self): super(DjangoAdapterFilterTestCase, self).setUp() self.statement1 = StatementModel(text="Testing...") self.statement1.add_response( StatementModel(text="Why are you counting?") ) self.statement2 = StatementModel(text="Testing one, two, three.") self.statement2.add_response(self.statement1) def test_filter_text_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(text="Howdy") self.assertEqual(len(results), 0) def test_filter_in_response_to_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(in_response_to="Maybe") self.assertEqual(len(results), 0) def test_filter_equal_results(self): statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter(in_response_to=[]) self.assertEqual(results.count(), 2) self.assertTrue(results.filter(text=statement1.text).exists()) self.assertTrue(results.filter(text=statement2.text).exists()) def test_filter_contains_result(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( in_response_to__contains="Why are you counting?" ) self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_contains_no_result(self): self.adapter.update(self.statement1) results = self.adapter.filter( in_response_to__contains="How do you do?" ) self.assertEqual(results.count(), 0) def test_filter_multiple_parameters(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Testing...", in_response_to__contains="Why are you counting?" ) self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_multiple_parameters_no_results(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Test", in_response_to__contains="Not an existing response." ) self.assertEqual(len(results), 0) def test_filter_no_parameters(self): """ If no parameters are passed to the filter, then all statements should be returned. """ statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter() self.assertEqual(len(results), 2) def test_filter_returns_statement_with_multiple_responses(self): statement = StatementModel.objects.create(text="You are welcome.") statement.add_response(StatementModel(text="Thanks.")) statement.add_response(StatementModel(text="Thank you.")) self.adapter.update(statement) response = self.adapter.filter( in_response_to__contains="Thanks." ) # Get the first response response = response[0] self.assertEqual(len(response.in_response_to), 2) def test_response_list_in_results(self): """ If a statement with response values is found using the filter method, they should be returned as response objects. """ statement = StatementModel.objects.create( text="The first is to help yourself, the second is to help others.", ) statement.add_response(StatementModel(text="Why do people have two hands?")) self.adapter.update(statement) found = self.adapter.filter(text=statement.text) self.assertEqual(len(found[0].in_response_to), 1) self.assertEqual(type(found[0].in_response_to[0]), ResponseModel)
class StatementIntegrationTestCase(TestCase): """ Test case to make sure that the Django Statement model and ChatterBot Statement object have a common interface. """ def setUp(self): super(StatementIntegrationTestCase, self).setUp() self.object = StatementObject(text='_') self.model = StatementModel(text='_') def test_text(self): self.assertTrue(hasattr(self.object, 'text')) self.assertTrue(hasattr(self.model, 'text')) def test_in_response_to(self): self.assertTrue(hasattr(self.object, 'in_response_to')) self.assertTrue(hasattr(self.model, 'in_response_to')) def test_extra_data(self): self.assertTrue(hasattr(self.object, 'extra_data')) self.assertTrue(hasattr(self.model, 'extra_data')) def test__str__(self): self.assertTrue(hasattr(self.object, '__str__')) self.assertTrue(hasattr(self.model, '__str__')) self.assertEqual(str(self.object), str(self.model)) def test_add_extra_data(self): self.object.add_extra_data('key', 'value') self.model.add_extra_data('key', 'value') def test_add_response(self): self.assertTrue(hasattr(self.object, 'add_response')) self.assertTrue(hasattr(self.model, 'add_response')) def test_remove_response(self): self.object.add_response(ResponseObject('Hello')) model_response_statement = StatementModel.objects.create(text='Hello') self.model.save() self.model.in_response.create(statement=self.model, response=model_response_statement) object_removed = self.object.remove_response('Hello') model_removed = self.model.remove_response('Hello') self.assertTrue(object_removed) self.assertTrue(model_removed) def test_get_response_count(self): self.object.add_response(ResponseObject('Hello', occurrence=2)) model_response_statement = StatementModel.objects.create(text='Hello') self.model.save() ResponseModel.objects.create( statement=self.model, response=model_response_statement ) ResponseModel.objects.create( statement=self.model, response=model_response_statement ) object_count = self.object.get_response_count(StatementObject(text='Hello')) model_count = self.model.get_response_count(StatementModel(text='Hello')) self.assertEqual(object_count, 2) self.assertEqual(model_count, 2) def test_serialize(self): object_data = self.object.serialize() model_data = self.model.serialize() self.assertEqual(object_data, model_data) def test_response_statement_cache(self): self.assertTrue(hasattr(self.object, 'response_statement_cache')) self.assertTrue(hasattr(self.model, 'response_statement_cache'))
def setUp(self): super(StatementIntegrationTestCase, self).setUp() self.object = StatementObject(text='_') self.model = StatementModel(text='_')
def setUp(self): super(StatementIntegrationTestCase, self).setUp() date_created = timezone.now() self.object = StatementObject(text='_', created_at=date_created) self.model = StatementModel(text='_', created_at=date_created)
class DjangoAdapterFilterTestCase(DjangoAdapterTestCase): def setUp(self): super(DjangoAdapterFilterTestCase, self).setUp() self.statement1 = StatementModel(text="Testing...") self.statement1.add_response( StatementModel(text="Why are you counting?") ) self.statement2 = StatementModel(text="Testing one, two, three.") self.statement2.add_response(self.statement1) def test_filter_text_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(text="Howdy") self.assertEqual(len(results), 0) def test_filter_in_response_to_no_matches(self): self.adapter.update(self.statement1) results = self.adapter.filter(in_response_to="Maybe") self.assertEqual(len(results), 0) def test_filter_equal_results(self): statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter(in_response_to=[]) self.assertEqual(results.count(), 2) self.assertTrue(results.filter(text=statement1.text).exists()) self.assertTrue(results.filter(text=statement2.text).exists()) def test_filter_contains_result(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( in_response_to__contains="Why are you counting?" ) self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_contains_no_result(self): self.adapter.update(self.statement1) results = self.adapter.filter( in_response_to__contains="How do you do?" ) self.assertEqual(results.count(), 0) def test_filter_multiple_parameters(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Testing...", in_response_to__contains="Why are you counting?" ) self.assertEqual(results.count(), 1) self.assertTrue(results.filter(text=self.statement1.text).exists()) def test_filter_multiple_parameters_no_results(self): self.adapter.update(self.statement1) self.adapter.update(self.statement2) results = self.adapter.filter( text="Test", in_response_to__contains="Not an existing response." ) self.assertEqual(len(results), 0) def test_filter_no_parameters(self): """ If no parameters are passed to the filter, then all statements should be returned. """ statement1 = StatementModel(text="Testing...") statement2 = StatementModel(text="Testing one, two, three.") self.adapter.update(statement1) self.adapter.update(statement2) results = self.adapter.filter() self.assertEqual(len(results), 2) def test_filter_returns_statement_with_multiple_responses(self): statement = StatementModel.objects.create(text="You are welcome.") statement.add_response(StatementModel(text="Thanks.")) statement.add_response(StatementModel(text="Thank you.")) self.adapter.update(statement) response = self.adapter.filter( in_response_to__contains="Thanks." ) # Get the first response response = response.first() self.assertEqual(response.responses.count(), 2) def test_response_list_in_results(self): """ If a statement with response values is found using the filter method, they should be returned as response objects. """ statement = StatementModel.objects.create( text="The first is to help yourself, the second is to help others.", ) statement.add_response(StatementModel(text="Why do people have two hands?")) self.adapter.update(statement) found = self.adapter.filter(text=statement.text) self.assertEqual(found.count(), 1) self.assertEqual(found.first().responses.count(), 1) self.assertEqual(type(found.first().responses.first()), ResponseModel) def test_confidence(self): """ Test that the confidence value is not saved to the database. The confidence attribute on statements is intended to just hold the confidence of the statement when it returned as a response to some input. Because of that, the value of the confidence score should never be stored in the database with the statement. """ statement = StatementModel(text='Test statement') statement.confidence = 0.5 statement.save() statement_updated = StatementModel.objects.get(pk=statement.id) self.assertEqual(statement_updated.confidence, 0)