Repository: microsoft/botbuilder-python
Pull request #182 (2019-05-21 18:57:07)

Telemetry emits correct EventData for "No Answer Found in KB" case
Fixes issue #179 

## Description
* When QnAMaker API returns "`No good match found in KB.`", it will be filtered properly now in `QnAMaker._format_qna_results()`.
* Added unit test for "No good match found in KB" case



Patch:
diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py b/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py
index 41d76c77..8c3a09a4 100644
--- a/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py
+++ b/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py
@@ -305,9 +305,7 @@ class QnAMaker(QnAMakerTelemetryClient):
             json_res = await result.json()
 
         answers_within_threshold = [
-            { **answer,'score': answer['score']/100 } 
-            if answer['score']/100 > options.score_threshold 
-            else {**answer} for answer in json_res['answers'] 
+            { **answer,'score': answer['score']/100 } for answer in json_res['answers'] if answer['score']/100 > options.score_threshold
         ]
         sorted_answers = sorted(answers_within_threshold, key = lambda ans: ans['score'], reverse = True)
 


Test patch:
diff --git a/libraries/botbuilder-ai/tests/qna/test_data/NoAnswerFoundInKb.json b/libraries/botbuilder-ai/tests/qna/test_data/NoAnswerFoundInKb.json
new file mode 100644
index 00000000..aaf78a6f
--- /dev/null
+++ b/libraries/botbuilder-ai/tests/qna/test_data/NoAnswerFoundInKb.json
@@ -0,0 +1,13 @@
+{
+    "answers": [
+        {
+            "questions": [],
+            "answer": "No good match found in KB.",
+            "score": 0,
+            "id": -1,
+            "source": null,
+            "metadata": []
+        }
+    ],
+    "debugInfo": null
+}
\ No newline at end of file
diff --git a/libraries/botbuilder-ai/tests/qna/test_qna.py b/libraries/botbuilder-ai/tests/qna/test_qna.py
index f92fafe6..9ba46b6a 100644
--- a/libraries/botbuilder-ai/tests/qna/test_qna.py
+++ b/libraries/botbuilder-ai/tests/qna/test_qna.py
@@ -291,6 +291,43 @@ class QnaApplicationTest(aiounittest.AsyncTestCase):
             self.assertEqual(expected_answer, results[0].answer[0])
             self.assertEqual('Editorial', results[0].source)
     
+    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))
+
+    
     async def test_telemetry_pii(self):
         # Arrange
         question: str = 'how do I clean the stove?'