Esempio n. 1
0
    async def test_trace_test(self):
        activity = Activity(
            type=ActivityTypes.message,
            text="how do I clean the stove?",
            conversation=ConversationAccount(),
            recipient=ChannelAccount(),
            from_property=ChannelAccount(),
        )

        response_json = QnaApplicationTest._get_json_for_file(
            "ReturnsAnswer.json")
        qna = QnAMaker(QnaApplicationTest.tests_endpoint)

        context = TestContext(activity)

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            result = await qna.get_answers(context)

            qna_trace_activities = list(
                filter(
                    lambda act: act.type == "trace" and act.name == "QnAMaker",
                    context.sent,
                ))
            trace_activity = qna_trace_activities[0]

            self.assertEqual("trace", trace_activity.type)
            self.assertEqual("QnAMaker", trace_activity.name)
            self.assertEqual("QnAMaker Trace", trace_activity.label)
            self.assertEqual("https://www.qnamaker.ai/schemas/trace",
                             trace_activity.value_type)
            self.assertEqual(True, hasattr(trace_activity, "value"))
            self.assertEqual(True, hasattr(trace_activity.value, "message"))
            self.assertEqual(True,
                             hasattr(trace_activity.value, "query_results"))
            self.assertEqual(True,
                             hasattr(trace_activity.value, "score_threshold"))
            self.assertEqual(True, hasattr(trace_activity.value, "top"))
            self.assertEqual(True,
                             hasattr(trace_activity.value, "strict_filters"))
            self.assertEqual(self._knowledge_base_id,
                             trace_activity.value.knowledge_base_id)

            return result
Esempio n. 2
0
    async def test_returns_answer_with_timeout(self):
        question: str = "how do I clean the stove?"
        options = QnAMakerOptions(timeout=999999)
        qna = QnAMaker(QnaApplicationTest.tests_endpoint, options)
        context = QnaApplicationTest._get_context(question, TestAdapter())
        response_json = QnaApplicationTest._get_json_for_file(
            "ReturnsAnswer.json")

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            result = await qna.get_answers(context, options)

            self.assertIsNotNone(result)
            self.assertEqual(options.timeout,
                             qna._generate_answer_helper.options.timeout)
Esempio n. 3
0
    async def test_should_answer_with_high_score_provided_qna_id(self):
        qna = QnAMaker(QnaApplicationTest.tests_endpoint)
        question: str = "where can I buy?"

        options = QnAMakerOptions(top=2, qna_id=55)
        turn_context = QnaApplicationTest._get_context(question, TestAdapter())
        response_json = QnaApplicationTest._get_json_for_file(
            "AnswerWithHighScoreProvidedContext.json")

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            results = await qna.get_answers(turn_context, options)
            self.assertEqual(1, len(results),
                             "Should have received 1 answers.")
            self.assertEqual(1, results[0].score, "Score should be high.")
Esempio n. 4
0
    async def test_should_answer_with_prompts(self):
        options = QnAMakerOptions(top=2)
        qna = QnAMaker(QnaApplicationTest.tests_endpoint, options)
        question: str = "how do I clean the stove?"
        turn_context = QnaApplicationTest._get_context(question, TestAdapter())
        response_json = QnaApplicationTest._get_json_for_file(
            "AnswerWithPrompts.json")

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            results = await qna.get_answers(turn_context, options)
            self.assertEqual(1, len(results),
                             "Should have received 1 answers.")
            self.assertEqual(1, len(results[0].context.prompts),
                             "Should have received 1 prompt.")
Esempio n. 5
0
    async def test_should_answer_with_low_score_without_provided_context(self):
        qna = QnAMaker(QnaApplicationTest.tests_endpoint)
        question: str = "where can I buy?"
        options = QnAMakerOptions(top=2, context=None)

        turn_context = QnaApplicationTest._get_context(question, TestAdapter())
        response_json = QnaApplicationTest._get_json_for_file(
            "AnswerWithLowScoreProvidedWithoutContext.json")

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            results = await qna.get_answers(turn_context, options)
            self.assertEqual(2, len(results),
                             "Should have received more than one answers.")
            self.assertEqual(True, results[0].score < 1,
                             "Score should be low.")
        async def execute_qna_dialog(turn_context: TurnContext) -> None:
            if turn_context.activity.type != ActivityTypes.message:
                raise TypeError(
                    "Failed to execute QnA dialog. Should have received a message activity."
                )

            response_json = self._get_json_res(turn_context.activity.text)
            dialog_context = await dialogs.create_context(turn_context)
            with patch(
                    "aiohttp.ClientSession.post",
                    return_value=aiounittest.futurized(response_json),
            ):
                results = await dialog_context.continue_dialog()

                if results.status == DialogTurnStatus.Empty:
                    await dialog_context.begin_dialog("QnAMakerDialog")

                await convo_state.save_changes(turn_context)
 def test_matrix_nio_backend_serve_once_login_error(self):
     backend = matrix_nio.MatrixNioBackend(self.bot_config)
     backend.client = nio.AsyncClient("test.matrix.org",
                                      user="******",
                                      device_id="test_device")
     user_id = "@example:localhost"
     login_response = LoginError.from_dict({
         "errcode": "ERROR_LOGGING_IN",
         "error": "Error logging in",
         "retry_after_ms": 10000
     })
     login_raw_mock = mock.Mock(
         return_value=aiounittest.futurized(login_response))
     backend.client.login_raw = login_raw_mock
     with self.assertRaises(ValueError):
         backend.serve_once()
     login_raw_mock.assert_called_once_with(
         self.bot_config.BOT_IDENTITY["auth_dict"])
Esempio n. 8
0
    async def test_telemetry_pii(self):
        # Arrange
        question: str = 'how do I clean the stove?'
        response_json = QnaApplicationTest._get_json_for_file(
            'ReturnsAnswer.json')
        telemetry_client = unittest.mock.create_autospec(BotTelemetryClient)
        log_personal_information = False
        context = QnaApplicationTest._get_context(question, TestAdapter())
        qna = QnAMaker(QnaApplicationTest.tests_endpoint,
                       telemetry_client=telemetry_client,
                       log_personal_information=log_personal_information)

        # Act
        with patch('aiohttp.ClientSession.post',
                   return_value=aiounittest.futurized(response_json)):
            results = await qna.get_answers(context)

            telemetry_args = telemetry_client.track_event.call_args_list[0][1]
            telemetry_properties = telemetry_args['properties']
            telemetry_metrics = telemetry_args['measurements']
            number_of_args = len(telemetry_args)
            first_answer = telemetry_args['properties'][
                QnATelemetryConstants.answer_property][0]
            expected_answer = 'BaseCamp: You can use a damp rag to clean around the Power Pack'

            # Assert - Validate PII properties not logged.
            self.assertEqual(1, telemetry_client.track_event.call_count)
            self.assertEqual(3, number_of_args)
            self.assertEqual('QnaMessage', telemetry_args['name'])
            self.assertTrue('answer' in telemetry_properties)
            self.assertTrue('knowledgeBaseId' in telemetry_properties)
            self.assertTrue('matchedQuestion' in telemetry_properties)
            self.assertTrue('question' not in telemetry_properties)
            self.assertTrue('questionId' in telemetry_properties)
            self.assertTrue('articleFound' in telemetry_properties)
            self.assertEqual(expected_answer, first_answer)
            self.assertTrue('score' in telemetry_metrics)
            self.assertEqual(1, telemetry_metrics['score'][0])

            # Assert - Validate we didn't break QnA functionality.
            self.assertIsNotNone(results)
            self.assertEqual(1, len(results))
            self.assertEqual(expected_answer, results[0].answer[0])
            self.assertEqual('Editorial', results[0].source)
    def test_matrix_nio_backend_serve_once_logged_in_has_not_synced(self):
        backend = matrix_nio.MatrixNioBackend(self.bot_config)
        backend.client = nio.AsyncClient("test.matrix.org",
                                         user="******",
                                         device_id="test_device")
        # Needed for ensuring that backend.client.logged_in = True
        backend.client.access_token = True
        # Needed since path may be tricky to get
        with open(os.path.join(os.path.dirname(__file__),
                               "sync.json")) as json_file:
            data = json.loads(json_file.read())

        sync_mock = mock.Mock(
            return_value=aiounittest.futurized(SyncResponse.from_dict(data)))
        backend.client.sync = sync_mock
        backend.serve_once()
        self.assertTrue(backend.has_synced)
        self.assertEqual(backend.client.next_batch, data["next_batch"])
        sync_mock.assert_called_once_with(full_state=True)
Esempio n. 10
0
    async def _get_service_result(
        cls,
        utterance: str,
        response_file: str,
        bot_adapter: BotAdapter = TestAdapter(),
        options: QnAMakerOptions = None,
    ) -> [dict]:
        response_json = QnaApplicationTest._get_json_for_file(response_file)

        qna = QnAMaker(QnaApplicationTest.tests_endpoint)
        context = QnaApplicationTest._get_context(utterance, bot_adapter)

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            result = await qna.get_answers(context, options)

            return result
Esempio n. 11
0
    async def test_trace_test(self):
        activity = Activity(
            type=ActivityTypes.message,
            text='how do I clean the stove?',
            conversation=ConversationAccount(),
            recipient=ChannelAccount(),
            from_property=ChannelAccount(),
        )

        response_json = QnaApplicationTest._get_json_for_file(
            'ReturnsAnswer.json')
        qna = QnAMaker(QnaApplicationTest.tests_endpoint)

        context = TestContext(activity)

        with patch('aiohttp.ClientSession.post',
                   return_value=aiounittest.futurized(response_json)):
            result = await qna.get_answers(context)

            qna_trace_activities = list(
                filter(
                    lambda act: act.type == 'trace' and act.name == 'QnAMaker',
                    context.sent))
            trace_activity = qna_trace_activities[0]

            self.assertEqual('trace', trace_activity.type)
            self.assertEqual('QnAMaker', trace_activity.name)
            self.assertEqual('QnAMaker Trace', trace_activity.label)
            self.assertEqual('https://www.qnamaker.ai/schemas/trace',
                             trace_activity.value_type)
            self.assertEqual(True, hasattr(trace_activity, 'value'))
            self.assertEqual(True, hasattr(trace_activity.value, 'message'))
            self.assertEqual(True,
                             hasattr(trace_activity.value, 'query_results'))
            self.assertEqual(True,
                             hasattr(trace_activity.value, 'score_threshold'))
            self.assertEqual(True, hasattr(trace_activity.value, 'top'))
            self.assertEqual(True,
                             hasattr(trace_activity.value, 'strict_filters'))
            self.assertEqual(self._knowledge_base_id,
                             trace_activity.value.knowledge_base_id[0])

            return result
Esempio n. 12
0
    async def test_telemetry_returns_answer_when_no_answer_found_in_kb(self):
        # Arrange
        question: str = 'gibberish question'
        response_json = QnaApplicationTest._get_json_for_file(
            'NoAnswerFoundInKb.json')
        telemetry_client = unittest.mock.create_autospec(BotTelemetryClient)
        qna = QnAMaker(QnaApplicationTest.tests_endpoint,
                       telemetry_client=telemetry_client,
                       log_personal_information=True)
        context = QnaApplicationTest._get_context(question, TestAdapter())

        # Act
        with patch('aiohttp.ClientSession.post',
                   return_value=aiounittest.futurized(response_json)):
            results = await qna.get_answers(context)

            telemetry_args = telemetry_client.track_event.call_args_list[0][1]
            telemetry_properties = telemetry_args['properties']
            number_of_args = len(telemetry_args)
            first_answer = telemetry_args['properties'][
                QnATelemetryConstants.answer_property]
            expected_answer = 'No Qna Answer matched'
            expected_matched_question = 'No Qna Question matched'

            # Assert - Check Telemetry logged.
            self.assertEqual(1, telemetry_client.track_event.call_count)
            self.assertEqual(3, number_of_args)
            self.assertEqual('QnaMessage', telemetry_args['name'])
            self.assertTrue('answer' in telemetry_properties)
            self.assertTrue('knowledgeBaseId' in telemetry_properties)
            self.assertTrue('matchedQuestion' in telemetry_properties)
            self.assertEqual(
                expected_matched_question, telemetry_properties[
                    QnATelemetryConstants.matched_question_property])
            self.assertTrue('question' in telemetry_properties)
            self.assertTrue('questionId' in telemetry_properties)
            self.assertTrue('articleFound' in telemetry_properties)
            self.assertEqual(expected_answer, first_answer)

            # Assert - Validate we didn't break QnA functionality.
            self.assertIsNotNone(results)
            self.assertEqual(0, len(results))
Esempio n. 13
0
    async def test_should_filter_low_score_variation(self):
        options = QnAMakerOptions(top=5)
        qna = QnAMaker(QnaApplicationTest.tests_endpoint, options)
        question: str = "Q11"
        context = QnaApplicationTest._get_context(question, TestAdapter())
        response_json = QnaApplicationTest._get_json_for_file("TopNAnswer.json")

        with patch(
            "aiohttp.ClientSession.post",
            return_value=aiounittest.futurized(response_json),
        ):
            results = await qna.get_answers(context)
            self.assertEqual(4, len(results), "Should have received 4 answers.")

            filtered_results = qna.get_low_score_variation(results)
            self.assertEqual(
                3,
                len(filtered_results),
                "Should have 3 filtered answers after low score variation.",
            )
Esempio n. 14
0
    async def test_call_method_proxy_with_query_params(self):
        mock_args = (
            'a',
            'b',
        )
        mock_kwargs = {'k1': 'v1'}
        mock_api = Mock()
        mock_api.servers = Mock()
        mock_api.servers.list = Mock()
        mock_res = Mock()
        mock_res.body = 'return_value'
        mock_api.servers.list.return_value = futurized(mock_res)
        mock_api.servers.actions = {'list': {'method': 'GET'}}

        obj = MockClass(api=mock_api)
        res = await obj.servers.list(*mock_args, **mock_kwargs)

        self.assertEqual(res, 'return_value')
        mock_api.servers.list.assert_called_once_with(*mock_args,
                                                      params=mock_kwargs)
Esempio n. 15
0
    async def test_call_method_proxy_with_body(self):
        mock_args = (
            'a',
            'b',
        )
        mock_kwargs = {'k1': 'v1'}
        mock_api = Mock()
        mock_api.servers = Mock()
        mock_api.servers.create = Mock()
        mock_res = Mock()
        mock_res.body = 'return_value'
        mock_api.servers.create.return_value = futurized(mock_res)
        mock_api.servers.actions = {'create': {'method': 'POST'}}

        obj = MockClass(api=mock_api)
        res = await obj.servers.create(*mock_args, **mock_kwargs)

        self.assertEqual(res, 'return_value')
        mock_api.servers.create.assert_called_once_with(*mock_args,
                                                        body=mock_kwargs)
 async def test_matrix_nio_backend_build_identifier(self):
     backend = matrix_nio.MatrixNioBackend(self.bot_config)
     backend.client = nio.AsyncClient("test.matrix.org",
                                      user="******",
                                      device_id="test_device")
     test_name = "Test Name"
     backend.client.get_profile = mock.Mock(
         return_value=aiounittest.futurized(
             ProfileGetResponse.from_dict(
                 {
                     "displayname": test_name,
                     "avatar_url": "http://test.org/avatar.png"
                 })))
     test_id = "test_id"
     test_identifier = await asyncio.gather(
         backend.build_identifier(test_id))
     test_identifier = test_identifier[0]
     self.assertIsInstance(test_identifier, matrix_nio.MatrixNioPerson)
     self.assertEqual(test_identifier.id, test_id)
     self.assertEqual(test_identifier.fullname, test_name)
 async def test_patch_data(self):
     """Test that patch method works and returns data."""
     json_patch = [
         {
             "op": "add",
             "path": "/metadataObjects/-",
             "value": {
                 "accessionId": self.id_stub,
                 "schema": "study"
             }
         },
     ]
     self.collection.bulk_write.return_value = futurized(
         UpdateResult({}, True))
     success = await self.test_service.patch("test", self.id_stub,
                                             json_patch)
     self.collection.bulk_write.assert_called_once_with(
         [
             UpdateOne(
                 {"testId": "EGA123456"},
                 {
                     "$addToSet": {
                         "metadataObjects": {
                             "$each": [{
                                 "accessionId": "EGA123456",
                                 "schema": "study"
                             }]
                         }
                     }
                 },
                 False,
                 None,
                 None,
                 None,
             )
         ],
         ordered=False,
     )
     self.assertTrue(success)
 def test_matrix_nio_room_destroy_error(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     rooms = {"nio_room1": room1}
     matrix_client.rooms = rooms
     nio_room1 = matrix_nio.MatrixNioRoom("nio_room1",
                                          client=matrix_client,
                                          title="nio_room1 title",
                                          subject="nio_room1 subject")
     matrix_client.room_forget = mock.Mock(
         return_value=aiounittest.futurized(
             RoomForgetError.from_dict(
                 {
                     "errcode": "ERROR_DESTROYING_ROOM",
                     "error": "Error destroying room",
                     "retry_after_ms": 10000,
                     "soft_logout": "false"
                 }, "nio_room1")))
     with self.assertRaises(ValueError):
         nio_room1.destroy()
     matrix_client.room_forget.assert_called_once_with("nio_room1")
 def test_matrix_nio_joined_room_error(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     joined_nio_room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     joined_matrix_rooms = {"nio_room1": joined_nio_room1}
     matrix_client.rooms = joined_matrix_rooms
     errbot_nio_room1 = matrix_nio.MatrixNioRoom(
         "nio_room1",
         client=matrix_client,
         title="nio_room1 title",
         subject="nio_room1 subject")
     matrix_client.joined_rooms = mock.Mock(
         return_value=aiounittest.futurized(
             JoinedRoomsError.from_dict({
                 "errcode": "ERROR_FETCHING_JOINED_ROOMS",
                 "error": "Error fetching joined rooms",
                 "retry_after_ms": 10000
             })))
     result = None
     with self.assertRaises(ValueError):
         result = errbot_nio_room1.joined
     self.assertIsNone(result)
     matrix_client.joined_rooms.assert_called_once()
 async def test_matrix_nio_room_leave(self):
     client_leave = mock.Mock(
         return_value=aiounittest.futurized("whatever"))
     self.room1._client.room_leave = client_leave
     await self.room1.leave("a very good reason")
     client_leave.assert_called_once_with(self.room_id)
 async def test_matrix_nio_room_join(self):
     client_join = mock.Mock(return_value=aiounittest.futurized("whatever"))
     self.room1._client.join = client_join
     await self.room1.join("discarded", "discarded")
     client_join.assert_called_once_with(self.room_id)
Esempio n. 22
0
    async def test_telemetry_fill_props_override(self):
        # Arrange
        question: str = "how do I clean the stove?"
        response_json = QnaApplicationTest._get_json_for_file(
            "ReturnsAnswer.json")
        context: TurnContext = QnaApplicationTest._get_context(
            question, TestAdapter())
        options = QnAMakerOptions(top=1)
        telemetry_client = unittest.mock.create_autospec(BotTelemetryClient)
        log_personal_information = False

        # Act - Pass in properties during QnA invocation that override default properties
        #       In addition Override with derivation.  This presents an interesting question of order of setting
        #       properties.
        #       If I want to override "originalQuestion" property:
        #           - Set in "Stock" schema
        #           - Set in derived QnAMaker class
        #           - Set in GetAnswersAsync
        #       Logically, the GetAnswersAync should win.  But ultimately OnQnaResultsAsync decides since it is the last
        #       code to touch the properties before logging (since it actually logs the event).
        qna = QnaApplicationTest.OverrideFillTelemetry(
            QnaApplicationTest.tests_endpoint,
            options,
            None,
            telemetry_client,
            log_personal_information,
        )
        telemetry_properties: Dict[str, str] = {
            "knowledgeBaseId": "my_important_value",
            "matchedQuestion": "my_important_value2",
        }
        telemetry_metrics: Dict[str, float] = {"score": 3.14159}

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            results = await qna.get_answers(context, None,
                                            telemetry_properties,
                                            telemetry_metrics)

            # Assert - Added properties were added.
            first_call_args = telemetry_client.track_event.call_args_list[0][0]
            first_properties = first_call_args[1]
            expected_answer = (
                "BaseCamp: You can use a damp rag to clean around the Power Pack"
            )
            first_metrics = first_call_args[2]

            self.assertEqual(2, telemetry_client.track_event.call_count)
            self.assertEqual(3, len(first_call_args))
            self.assertEqual("QnaMessage", first_call_args[0])
            self.assertEqual(6, len(first_properties))
            self.assertTrue("knowledgeBaseId" in first_properties)
            self.assertEqual("my_important_value",
                             first_properties["knowledgeBaseId"])
            self.assertTrue("matchedQuestion" in first_properties)
            self.assertEqual("my_important_value2",
                             first_properties["matchedQuestion"])
            self.assertTrue("questionId" in first_properties)
            self.assertTrue("answer" in first_properties)
            self.assertEqual(expected_answer, first_properties["answer"])
            self.assertTrue("articleFound" in first_properties)
            self.assertTrue("my_important_property" in first_properties)
            self.assertEqual("my_important_value",
                             first_properties["my_important_property"])

            self.assertEqual(1, len(first_metrics))
            self.assertTrue("score" in first_metrics)
            self.assertEqual(3.14159, first_metrics["score"])

            # Assert - Validate we didn't break QnA functionality.
            self.assertIsNotNone(results)
            self.assertEqual(1, len(results))
            self.assertEqual(expected_answer, results[0].answer)
            self.assertEqual("Editorial", results[0].source)
Esempio n. 23
0
    async def test_telemetry_additional_props_override(self):
        question: str = "how do I clean the stove?"
        response_json = QnaApplicationTest._get_json_for_file(
            "ReturnsAnswer.json")
        context = QnaApplicationTest._get_context(question, TestAdapter())
        options = QnAMakerOptions(top=1)
        telemetry_client = unittest.mock.create_autospec(BotTelemetryClient)
        log_personal_information = False

        # Act - Pass in properties during QnA invocation that override default properties
        # NOTE: We are invoking this with PII turned OFF, and passing a PII property (originalQuestion).
        qna = QnAMaker(
            QnaApplicationTest.tests_endpoint,
            options,
            None,
            telemetry_client,
            log_personal_information,
        )
        telemetry_properties = {
            "knowledge_base_id": "my_important_value",
            "original_question": "my_important_value2",
        }
        telemetry_metrics = {"score": 3.14159}

        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            results = await qna.get_answers(context, None,
                                            telemetry_properties,
                                            telemetry_metrics)

            # Assert - Added properties were added.
            tracked_args = telemetry_client.track_event.call_args_list[0][1]
            tracked_properties = tracked_args["properties"]
            expected_answer = (
                "BaseCamp: You can use a damp rag to clean around the Power Pack"
            )
            tracked_metrics = tracked_args["measurements"]

            self.assertEqual(1, telemetry_client.track_event.call_count)
            self.assertEqual(3, len(tracked_args))
            self.assertEqual("QnaMessage", tracked_args["name"])
            self.assertTrue("knowledge_base_id" in tracked_properties)
            self.assertEqual("my_important_value",
                             tracked_properties["knowledge_base_id"])
            self.assertTrue("original_question" in tracked_properties)
            self.assertTrue("matchedQuestion" in tracked_properties)
            self.assertEqual("my_important_value2",
                             tracked_properties["original_question"])
            self.assertTrue("question" not in tracked_properties)
            self.assertTrue("questionId" in tracked_properties)
            self.assertTrue("answer" in tracked_properties)
            self.assertEqual(expected_answer, tracked_properties["answer"])
            self.assertTrue("my_important_property" not in tracked_properties)
            self.assertEqual(1, len(tracked_metrics))
            self.assertTrue("score" in tracked_metrics)
            self.assertEqual(3.14159, tracked_metrics["score"])

            # Assert - Validate we didn't break QnA functionality.
            self.assertIsNotNone(results)
            self.assertEqual(1, len(results))
            self.assertEqual(expected_answer, results[0].answer)
            self.assertEqual("Editorial", results[0].source)
Esempio n. 24
0
    async def test_telemetry_additional_props_metrics(self):
        # Arrange
        question: str = "how do I clean the stove?"
        response_json = QnaApplicationTest._get_json_for_file(
            "ReturnsAnswer.json")
        context = QnaApplicationTest._get_context(question, TestAdapter())
        options = QnAMakerOptions(top=1)
        telemetry_client = unittest.mock.create_autospec(BotTelemetryClient)
        log_personal_information = False

        # Act
        with patch(
                "aiohttp.ClientSession.post",
                return_value=aiounittest.futurized(response_json),
        ):
            qna = QnAMaker(
                QnaApplicationTest.tests_endpoint,
                options,
                None,
                telemetry_client,
                log_personal_information,
            )
            telemetry_properties: Dict[str, str] = {
                "my_important_property": "my_important_value"
            }
            telemetry_metrics: Dict[str, float] = {
                "my_important_metric": 3.14159
            }

            results = await qna.get_answers(context, None,
                                            telemetry_properties,
                                            telemetry_metrics)

            # Assert - Added properties were added.
            telemetry_args = telemetry_client.track_event.call_args_list[0][1]
            telemetry_properties = telemetry_args["properties"]
            expected_answer = (
                "BaseCamp: You can use a damp rag to clean around the Power Pack"
            )

            self.assertEqual(1, telemetry_client.track_event.call_count)
            self.assertEqual(3, len(telemetry_args))
            self.assertEqual("QnaMessage", telemetry_args["name"])
            self.assertTrue("knowledgeBaseId" in telemetry_properties)
            self.assertTrue("question" not in telemetry_properties)
            self.assertTrue("matchedQuestion" in telemetry_properties)
            self.assertTrue("questionId" in telemetry_properties)
            self.assertTrue("answer" in telemetry_properties)
            self.assertTrue(expected_answer, telemetry_properties["answer"])
            self.assertTrue("my_important_property" in telemetry_properties)
            self.assertEqual("my_important_value",
                             telemetry_properties["my_important_property"])

            tracked_metrics = telemetry_args["measurements"]

            self.assertEqual(2, len(tracked_metrics))
            self.assertTrue("score" in tracked_metrics)
            self.assertTrue("my_important_metric" in tracked_metrics)
            self.assertEqual(3.14159, tracked_metrics["my_important_metric"])

            # Assert - Validate we didn't break QnA functionality.
            self.assertIsNotNone(results)
            self.assertEqual(1, len(results))
            self.assertEqual(expected_answer, results[0].answer)
            self.assertEqual("Editorial", results[0].source)
 async def test_matrix_nio_room_invite(self):
     client_invite = mock.Mock(return_value=aiounittest.futurized(
         nio.responses.RoomInviteResponse()))
     self.room1._client.room_invite = client_invite
     await self.room1.invite(self.users)
     client_invite.assert_has_calls([call("12345"), call("54321")])
 async def test_matrix_nio_room_join_error(self):
     client_join = mock.Mock(return_value=aiounittest.futurized(
         nio.responses.JoinError("Join Error")))
     self.room1._client.join = client_join
     with self.assertRaises(matrix_nio.MatrixNioRoomError):
         await self.room1.join("discarded", "discarded")
 async def test_matrix_nio_room_leave_error(self):
     client_leave = mock.Mock(return_value=aiounittest.futurized(
         nio.responses.RoomLeaveError("Leave Error")))
     self.room1._client.room_leave = client_leave
     with self.assertRaises(matrix_nio.MatrixNioRoomError):
         await self.room1.leave("a very good reason")
Esempio n. 28
0
    async def test_callback_bad_claim(self):
        """Test callback bad nonce validation."""
        request = Mock_Request()
        request.query["state"] = "state"
        request.query["code"] = "code"
        request.app["Session"] = {"oidc_state": "state"}
        request.app["Cookies"] = set({})
        request.app["Crypt"] = cryptography.fernet.Fernet(cryptography.fernet.Fernet.generate_key())
        request.app["Salt"] = hashlib.sha256(urandom(512)).hexdigest()

        resp_token = MockResponse(jwt_data_bad_nonce, 200)
        resp_jwk = MockResponse(jwk_data, 200)

        with patch("aiohttp.ClientSession.post", return_value=resp_token):
            with patch("aiohttp.ClientSession.get", return_value=resp_jwk):
                with patch("metadata_backend.api.auth.AccessHandler._set_user", return_value=futurized(None)):
                    with self.assertRaises(HTTPForbidden):
                        await self.AccessHandler.callback(request)
Esempio n. 29
0
 async def test_user_deletion_is_called(self):
     """Test that user object would be deleted."""
     self.MockedUserOperator().delete_user.return_value = futurized(None)
     await self.client.delete("/users/current")
     self.MockedUserOperator().delete_user.assert_called_once()
 async def test_matrix_nio_room_create_error(self):
     client_create = mock.Mock(return_value=aiounittest.futurized(
         nio.responses.RoomCreateError("Create Error")))
     self.room1._client.room_create = client_create
     with self.assertRaises(matrix_nio.MatrixNioRoomError):
         await self.room1.create()