Skip to content

Commit

Permalink
Research assistant unit test pylint correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradheep-Microsoft committed Jan 13, 2025
1 parent a1082d8 commit 27f3465
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions ResearchAssistant/App/test_app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import json
import os
from unittest.mock import MagicMock, Mock, patch
from flask import Flask, request
from flask import Flask
import pytest
import requests
import urllib

from app import (extract_value, fetchUserGroups, format_as_ndjson,
from app import (extract_value, fetchUserGroups,
formatApiResponseNoStreaming, formatApiResponseStreaming,
generateFilterString, is_chat_model, parse_multi_columns,
prepare_body_headers_with_data, should_use_data,
stream_with_data, conversation_with_data, draft_document_generate)
stream_with_data, draft_document_generate)

AZURE_SEARCH_SERVICE = os.environ.get("AZURE_SEARCH_SERVICE", "")
AZURE_OPENAI_KEY = os.environ.get("AZURE_OPENAI_KEY", "")
AZURE_SEARCH_PERMITTED_GROUPS_COLUMN = os.environ.get(
"AZURE_SEARCH_PERMITTED_GROUPS_COLUMN", ""
)


def test_parse_multi_columns():
assert parse_multi_columns("a|b|c") == ["a", "b", "c"]
assert parse_multi_columns("a,b,c") == ["a", "b", "c"]
Expand Down Expand Up @@ -160,9 +160,9 @@ def test_generateFilterString(mock_fetchUserGroups):
userToken = "fake_token"

filter_string = generateFilterString(userToken)
print("filter string",filter_string)
assert filter_string == "None/any(g:search.in(g, '1, 2'))"


def test_prepare_body_headers_with_data():
# Create a mock request
mock_request = MagicMock()
Expand Down Expand Up @@ -208,19 +208,6 @@ def test_prepare_body_headers_with_data():
assert headers["x-ms-useragent"] == "GitHubSampleWebApp/PublicAPI/3.0.0"


def test_invalid_datasource_type():
mock_request = MagicMock()
mock_request.json = {"messages": ["Hello, world!"], "index_name": "grants"}


with patch("app.DATASOURCE_TYPE", "InvalidType"):
with pytest.raises(Exception) as exc_info:
prepare_body_headers_with_data(mock_request)
assert "DATASOURCE_TYPE is not configured or unknown: InvalidType" in str(
exc_info.value
)


def test_invalid_datasource_type():
mock_request = MagicMock()
mock_request.json = {"messages": ["Hello, world!"], "index_name": "grants"}
Expand Down Expand Up @@ -318,16 +305,16 @@ def test_stream_with_data_azure_success():
print(results, "result test case")
assert len(results) == 1


# Mock constants
USE_AZURE_AI_STUDIO = "true"
AZURE_OPENAI_PREVIEW_API_VERSION = "2023-06-01-preview"
DEBUG_LOGGING = False

AZURE_SEARCH_SERVICE = os.environ.get("AZURE_SEARCH_SERVICE", "mysearchservice")


def test_stream_with_data_azure_error():

body = {
"messages": [
{
Expand Down Expand Up @@ -380,10 +367,9 @@ def test_stream_with_data_azure_error():
}
],
}

if USE_AZURE_AI_STUDIO.lower() == "true":
body = body

body = body
headers = {
"Content-Type": "application/json",
"api-key": "",
Expand All @@ -410,6 +396,7 @@ def test_stream_with_data_azure_error():
print(results, "result test case")
assert len(results) == 1


def test_formatApiResponseNoStreaming():
rawResponse = {
"id": "1",
Expand Down Expand Up @@ -463,6 +450,8 @@ def test_extract_value():
assert extract_value("unknown", text) == "N/A"

app = Flask(__name__)


app.add_url_rule("/draft_document/generate_section", "draft_document_generate", draft_document_generate, methods=["POST"])


Expand Down Expand Up @@ -541,13 +530,15 @@ def test_draft_document_generate_with_context(mock_os_environ, mock_urlopen, cli
assert "content" in response_json
assert response_json["content"] == "Generated content with context."


@pytest.fixture
def clients():
app = Flask(__name__)
app.route('/draft_document/generate_section', methods=['POST'])(draft_document_generate)
client = app.test_client()
yield client


@patch("urllib.request.urlopen")
@patch("os.environ.get")
def test_draft_document_generate_http_error(mock_env_get, mock_urlopen, client):
Expand Down Expand Up @@ -582,5 +573,3 @@ def test_draft_document_generate_http_error(mock_env_get, mock_urlopen, client):
)

assert response.status_code == 200


0 comments on commit 27f3465

Please sign in to comment.