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())
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 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)