Skip to content
Snippets Groups Projects
Commit 5b4e287d authored by Sukanya K's avatar Sukanya K
Browse files

first commit

parent 25187083
No related branches found
No related tags found
No related merge requests found
Showing
with 183 additions and 0 deletions
File added
File added
File added
File added
import os
import pytest
import requests
from unittest.mock import patch
from utils.api_client import send_api_request
@patch("requests.post")
@patch("time.time")
def test_send_api_request_success(mock_time, mock_post):
# Mock API response
mock_response = mock_post.return_value
mock_response.json.return_value = {"status": "success", "data": "Sample data"}
# Mock timing for response calculation
mock_time.side_effect = [1000, 1002] # Start and end time (2 seconds response time)
url = "https://step-fastapi-email-pro-qa-1054173481485.asia-south1.run.app/evaluate_email"
payload = {
"user_id": 7600001,
"question_id": 100456,
"answer_id": 120000789,
"cefr_level": "C1",
"subject": "Subject: Project Feedback",
"scenario": "addressing issue or giving feedback",
"scenario_question": "You are a team leader in a company, and one of your team members recently completed an important project. You need to provide feedback on their work, talking about both the positives and the areas for improvement. The goal is to note their hard work, encourage them, and give guidance for future projects.",
"email_content": "Hi Emma, I wanted to say a big thank you for all your hard work on the Marketing Campaign Launch project. It was a big project, and you did a great job!..."
}
response_data, response_time = send_api_request(url, payload)
assert response_data == {"status": "success", "data": "Sample data"}
assert response_time == 2
@patch("requests.post")
def test_send_api_request_error(mock_post):
# Simulate request failure
mock_post.side_effect = requests.exceptions.RequestException("API Failure")
url = "https://step-fastapi-email-pro-qa-1054173481485.asia-south1.run.app/evaluate_email"
payload = {}
response_data, response_time = send_api_request(url, payload)
assert response_data == {}
assert response_time == 0
import pytest
import openpyxl
from utils.excel_operations import load_excel, save_excel, create_output_workbook
import os
@pytest.fixture
def sample_workbook(tmp_path):
"""Create a sample workbook for testing."""
file_path = tmp_path / "test.xlsx"
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.append(["Name", "Age", "City"])
sheet.append(["John Doe", 30, "New York"])
workbook.save(file_path)
return file_path
def test_load_excel(sample_workbook):
"""Test loading an existing Excel file."""
workbook = load_excel(sample_workbook)
sheet = workbook.active
assert sheet.cell(row=1, column=1).value == "Name"
assert sheet.cell(row=2, column=1).value == "John Doe"
def test_save_excel(tmp_path):
"""Test saving an Excel file."""
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.append(["Product", "Price"])
sheet.append(["Laptop", 1200])
file_path = tmp_path / "output.xlsx"
save_excel(workbook, file_path)
# Verify the file is created and data is saved correctly
loaded_workbook = openpyxl.load_workbook(file_path)
loaded_sheet = loaded_workbook.active
assert loaded_sheet.cell(row=1, column=1).value == "Product"
assert loaded_sheet.cell(row=2, column=2).value == 1200
def test_create_output_workbook():
"""Test creating a new workbook with headers."""
headers = ["ID", "Name", "Department"]
workbook = create_output_workbook(headers)
sheet = workbook.active
assert sheet.cell(row=1, column=1).value == "ID"
assert sheet.cell(row=1, column=3).value == "Department"
import pytest
from utils.payload_mapping import get_payload_mapping
from config import PAYLOAD_FIELDS_STATIC
def test_get_payload_mapping_complete_data():
input_data = {
"cefr_level": "c1",
"scenario": "addressing issue or giving feedback",
"scenario_question": "You are a team leader providing project feedback.",
"subject": "Subject: Project Feedback",
"email_content": "Hi Emma, great work on the project!"
}
expected_payload = PAYLOAD_FIELDS_STATIC.copy()
expected_payload.update(input_data)
assert get_payload_mapping(input_data) == expected_payload
def test_get_payload_mapping_partial_data():
input_data = {
"cefr_level": "b1",
"email_content": "Reminder: Please submit your report."
}
expected_payload = PAYLOAD_FIELDS_STATIC.copy()
expected_payload.update(input_data)
assert get_payload_mapping(input_data) == expected_payload
def test_get_payload_mapping_empty_data():
input_data = {}
expected_payload = PAYLOAD_FIELDS_STATIC.copy()
assert get_payload_mapping(input_data) == expected_payload
def test_get_payload_mapping_overwrite_static_fields():
input_data = {
"user_id": 7600001, # Attempt to overwrite static field
"email_content": "Custom content for testing."
}
expected_payload = PAYLOAD_FIELDS_STATIC.copy()
expected_payload["email_content"] = "Custom content for testing."
# Static fields should not change
expected_payload["user_id"] = PAYLOAD_FIELDS_STATIC["user_id"]
assert get_payload_mapping(input_data) == expected_payload
# utils/__init__.py
from .excel_operations import load_excel, save_excel, create_output_workbook
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
# utils/api_client.py
import requests
import time
def send_api_request(url, payload):
try:
# Assuming it's a POST request
start_time = time.time()
response = requests.post(url, json=payload)
response_time = time.time() - start_time
# Assuming response.json() gives us the actual data as a dictionary
return response.json(), response_time
except Exception as e:
print(f"API Request Error: {str(e)}")
return {}, 0 # Return an empty dict and 0 time on error
# utils/excel_operations.py
import openpyxl
def load_excel(file_path):
"""Load an Excel file and return the workbook."""
return openpyxl.load_workbook(file_path)
def save_excel(workbook, file_path):
"""Save the workbook to a file."""
workbook.save(file_path)
def create_output_workbook(headers_name):
"""Create a new workbook and add headers."""
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.append(headers_name)
return workbook
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment