-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for backend functions (#644)
* Add test for batch processing function * Remove comment * Try to remove sys path modifications * Fix imports * Fix sys path append statements by making paths absolute * Remove extra sys.path.append that isnt necessary * Apply suggestions from code review Co-authored-by: Chinedum Echeta <60179183+cecheta@users.noreply.github.com> * Rename test file, add a test for no url in request * Make sure ConfigHelper etc are mocked out * Mock out config helper * Improve sys.path.append * Mock EnvHelper as well to speed up test execution time * Utilise get_user_function() method to enable tests * re-add env sample * Init log level from os environ --------- Co-authored-by: Chinedum Echeta <60179183+cecheta@users.noreply.github.com>
- Loading branch information
1 parent
78dcb6d
commit 0a7f180
Showing
10 changed files
with
255 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,3 @@ AZURE_SPEECH_SERVICE_REGION= | |
AZURE_AUTH_TYPE=keys | ||
USE_KEY_VAULT=true | ||
AZURE_KEY_VAULT_ENDPOINT= | ||
LOGLEVEL=INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import sys | ||
import os | ||
from unittest.mock import patch | ||
import azure.functions as func | ||
|
||
|
||
sys.path.append(os.path.join(os.path.dirname(sys.path[0]), "backend", "batch")) | ||
|
||
from backend.batch.AddURLEmbeddings import add_url_embeddings # noqa: E402 | ||
|
||
|
||
@patch("backend.batch.AddURLEmbeddings.ConfigHelper") | ||
@patch("backend.batch.AddURLEmbeddings.DocumentProcessor") | ||
def test_add_url_embeddings_when_url_set_in_body(_, __): | ||
fake_request = func.HttpRequest( | ||
method="POST", | ||
url="", | ||
body=b'{"url": "https://example.com"}', | ||
headers={"Content-Type": "application/json"}, | ||
) | ||
|
||
response = add_url_embeddings.build().get_user_function()(fake_request) | ||
|
||
assert response.status_code == 200 | ||
|
||
|
||
@patch("backend.batch.AddURLEmbeddings.ConfigHelper") | ||
@patch("backend.batch.AddURLEmbeddings.DocumentProcessor") | ||
def test_add_url_embeddings_when_url_set_in_param(_, __): | ||
fake_request = func.HttpRequest( | ||
method="POST", | ||
url="", | ||
body=b"", | ||
headers={"Content-Type": "application/json"}, | ||
params={"url": "https://example.com"}, | ||
) | ||
|
||
response = add_url_embeddings.build().get_user_function()(fake_request) | ||
|
||
assert response.status_code == 200 | ||
|
||
|
||
@patch("backend.batch.AddURLEmbeddings.ConfigHelper") | ||
@patch("backend.batch.AddURLEmbeddings.DocumentProcessor") | ||
def test_add_url_embeddings_returns_400_when_url_not_set(_, __): | ||
fake_request = func.HttpRequest( | ||
method="POST", | ||
url="", | ||
body=b"", | ||
params={}, | ||
) | ||
|
||
response = add_url_embeddings.build().get_user_function()(fake_request) | ||
|
||
assert response.status_code == 400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import sys | ||
import os | ||
from unittest.mock import patch, Mock | ||
from azure.functions import QueueMessage | ||
|
||
|
||
sys.path.append(os.path.join(os.path.dirname(sys.path[0]), "backend", "batch")) | ||
|
||
from backend.batch.BatchPushResults import ( # noqa: E402 | ||
batch_push_results, | ||
_get_file_name_from_message, | ||
) | ||
|
||
|
||
def test_get_file_name_from_message(): | ||
mock_queue_message = QueueMessage( | ||
body='{"message": "test message", "filename": "test_filename.md"}' | ||
) | ||
|
||
file_name = _get_file_name_from_message(mock_queue_message) | ||
|
||
assert file_name == "test_filename.md" | ||
|
||
|
||
def test_get_file_name_from_message_no_filename(): | ||
mock_queue_message = QueueMessage( | ||
body='{"data": { "url": "test/test/test_filename.md"} }' | ||
) | ||
|
||
file_name = _get_file_name_from_message(mock_queue_message) | ||
|
||
assert file_name == "test_filename.md" | ||
|
||
|
||
@patch("backend.batch.BatchPushResults.ConfigHelper") | ||
@patch("backend.batch.BatchPushResults.AzureBlobStorageClient") | ||
@patch("backend.batch.BatchPushResults.DocumentProcessor") | ||
def test_batch_push_results( | ||
mock_document_processor, | ||
mock_azure_blob_storage_client, | ||
mock_config_helper, | ||
): | ||
mock_queue_message = QueueMessage( | ||
body='{"message": "test message", "filename": "test/test/test_filename.md"}' | ||
) | ||
|
||
mock_blob_client_instance = mock_azure_blob_storage_client.return_value | ||
mock_blob_client_instance.get_blob_sas.return_value = "test_blob_sas" | ||
|
||
mock_document_processor_instance = mock_document_processor.return_value | ||
|
||
md_processor = Mock() | ||
md_processor.document_type.lower.return_value = "md" | ||
txt_processor = Mock() | ||
txt_processor.document_type.lower.return_value = "txt" | ||
mock_processors = [md_processor, txt_processor] | ||
mock_config_helper.get_active_config_or_default.return_value.document_processors = ( | ||
mock_processors | ||
) | ||
|
||
batch_push_results.build().get_user_function()(mock_queue_message) | ||
|
||
mock_document_processor_instance.process.assert_called_once_with( | ||
source_url="test_blob_sas", processors=[md_processor] | ||
) | ||
mock_blob_client_instance.upsert_blob_metadata.assert_called_once_with( | ||
"test/test/test_filename.md", {"embeddings_added": "true"} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import sys | ||
import os | ||
from unittest.mock import patch, Mock | ||
|
||
sys.path.append(os.path.join(os.path.dirname(sys.path[0]), "backend", "batch")) | ||
|
||
from backend.batch.BatchStartProcessing import batch_start_processing # noqa: E402 | ||
|
||
|
||
@patch("backend.batch.BatchStartProcessing.create_queue_client") | ||
@patch("backend.batch.BatchStartProcessing.AzureBlobStorageClient") | ||
def test_batch_start_processing_processes_all( | ||
mock_blob_storage_client, mock_create_queue_client | ||
): | ||
mock_http_request = Mock() | ||
mock_http_request.params = dict() | ||
mock_http_request.params["process_all"] = "true" | ||
|
||
mock_queue_client = Mock() | ||
mock_create_queue_client.return_value = mock_queue_client | ||
|
||
mock_blob_storage_client.return_value.get_all_files.return_value = [ | ||
{"filename": "file_name_one", "embeddings_added": False} | ||
] | ||
|
||
response = batch_start_processing.build().get_user_function()(mock_http_request) | ||
|
||
assert response.status_code == 200 | ||
|
||
mock_queue_client.send_message.assert_called_once_with( | ||
b'{"filename": "file_name_one"}', | ||
) | ||
|
||
|
||
@patch("backend.batch.BatchStartProcessing.create_queue_client") | ||
@patch("backend.batch.BatchStartProcessing.AzureBlobStorageClient") | ||
def test_batch_start_processing_filters_filter_no_embeddings( | ||
mock_blob_storage_client, mock_create_queue_client | ||
): | ||
mock_http_request = Mock() | ||
mock_http_request.params = dict() | ||
mock_http_request.params["process_all"] = "false" | ||
|
||
mock_queue_client = Mock() | ||
mock_create_queue_client.return_value = mock_queue_client | ||
|
||
mock_blob_storage_client.return_value.get_all_files.return_value = [ | ||
{ | ||
"filename": "file_name_one", | ||
"embeddings_added": True, # will get filtered out | ||
}, | ||
{"filename": "file_name_two", "embeddings_added": False}, | ||
] | ||
response = batch_start_processing.build().get_user_function()(mock_http_request) | ||
|
||
assert response.status_code == 200 | ||
|
||
mock_queue_client.send_message.assert_called_once_with( | ||
b'{"filename": "file_name_two"}', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sys | ||
import os | ||
from unittest.mock import patch, Mock, ANY | ||
import json | ||
|
||
sys.path.append(os.path.join(os.path.dirname(sys.path[0]), "backend", "batch")) | ||
|
||
from backend.batch.GetConversationResponse import ( # noqa: E402 | ||
get_conversation_response, | ||
) | ||
|
||
|
||
@patch("backend.batch.GetConversationResponse.ConfigHelper") | ||
@patch("backend.batch.GetConversationResponse.Orchestrator") | ||
def test_get_conversation_response(mock_create_message_orchestrator, _): | ||
mock_http_request = Mock() | ||
request_json = { | ||
"messages": [ | ||
{"content": "Do I have meetings today?", "role": "user"}, | ||
{"content": "It is sunny today", "role": "assistant"}, | ||
{"content": "What is the weather like today?", "role": "user"}, | ||
], | ||
"conversation_id": "13245", | ||
} | ||
mock_http_request.get_json.return_value = request_json | ||
|
||
mock_message_orchestrator = Mock() | ||
mock_message_orchestrator.handle_message.return_value = [ | ||
"You don't have any meetings today" | ||
] | ||
|
||
mock_create_message_orchestrator.return_value = mock_message_orchestrator | ||
|
||
response = get_conversation_response.build().get_user_function()(mock_http_request) | ||
|
||
assert response.status_code == 200 | ||
|
||
mock_message_orchestrator.handle_message.assert_called_once_with( | ||
user_message="What is the weather like today?", | ||
chat_history=[("Do I have meetings today?", "It is sunny today")], | ||
conversation_id="13245", | ||
orchestrator=ANY, | ||
) | ||
|
||
response_json = json.loads(response.get_body()) | ||
assert response_json["id"] == "response.id" | ||
assert response_json["choices"] == [ | ||
{"messages": ["You don't have any meetings today"]} | ||
] |