Skip to content

Commit d29df6f

Browse files
Refactor environment variable setup in tests
1 parent 5ff7666 commit d29df6f

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

src/backend/tests/test_app.py

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
os.environ["COSMOSDB_KEY"] = "mock-key"
1818
os.environ["COSMOSDB_DATABASE"] = "mock-database"
1919
os.environ["COSMOSDB_CONTAINER"] = "mock-container"
20-
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] = (
21-
"InstrumentationKey=mock-instrumentation-key;IngestionEndpoint=https://mock-ingestion-endpoint"
22-
)
20+
os.environ[
21+
"APPLICATIONINSIGHTS_CONNECTION_STRING"
22+
] = "InstrumentationKey=mock-instrumentation-key;IngestionEndpoint=https://mock-ingestion-endpoint"
2323
os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "mock-deployment-name"
2424
os.environ["AZURE_OPENAI_API_VERSION"] = "2023-01-01"
2525
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://mock-openai-endpoint"
@@ -30,9 +30,7 @@
3030
sys.path.insert(0, ROOT_DIR)
3131

3232
# Provide safe defaults for vars that app_config reads at import-time
33-
os.environ.setdefault(
34-
"AZURE_AI_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000"
35-
)
33+
os.environ.setdefault("AZURE_AI_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000")
3634
os.environ.setdefault("AZURE_AI_RESOURCE_GROUP", "rg-test")
3735
os.environ.setdefault("AZURE_AI_PROJECT_NAME", "proj-test")
3836
os.environ.setdefault("AZURE_AI_AGENT_ENDPOINT", "https://agents.example.com/")
@@ -45,7 +43,6 @@
4543
except ModuleNotFoundError:
4644
# fallback to app_kernel which exists in this repo
4745
import importlib
48-
4946
mod = importlib.import_module("src.backend.app_kernel")
5047
app = getattr(mod, "app", None)
5148
if app is None:
@@ -60,7 +57,6 @@
6057

6158
from fastapi.routing import APIRoute
6259

63-
6460
def _find_input_task_path(app):
6561
for r in app.routes:
6662
if isinstance(r, APIRoute):
@@ -71,7 +67,6 @@ def _find_input_task_path(app):
7167
return r.path
7268
return "/input_task" # fallback
7369

74-
7570
INPUT_TASK_PATH = _find_input_task_path(app)
7671

7772

@@ -98,27 +93,27 @@ def test_input_task_invalid_json():
9893
def test_process_request_endpoint_success():
9994
"""Test the /api/process_request endpoint with valid input."""
10095
headers = {"Authorization": "Bearer mock-token"}
101-
96+
10297
# Mock the RAI success function
103-
with patch("app_kernel.rai_success", return_value=True), patch(
104-
"app_kernel.initialize_runtime_and_context"
105-
) as mock_init, patch("app_kernel.track_event_if_configured") as mock_track:
106-
98+
with patch("app_kernel.rai_success", return_value=True), \
99+
patch("app_kernel.initialize_runtime_and_context") as mock_init, \
100+
patch("app_kernel.track_event_if_configured") as mock_track:
101+
107102
# Mock memory store
108103
mock_memory_store = MagicMock()
109104
mock_init.return_value = (MagicMock(), mock_memory_store)
110-
105+
111106
test_input = {
112107
"session_id": "test-session-123",
113-
"description": "Create a marketing plan for our new product",
108+
"description": "Create a marketing plan for our new product"
114109
}
115-
110+
116111
response = client.post("/api/process_request", json=test_input, headers=headers)
117-
112+
118113
# Print response details for debugging
119114
print(f"Response status: {response.status_code}")
120115
print(f"Response data: {response.json()}")
121-
116+
122117
# Check response
123118
assert response.status_code == 200
124119
data = response.json()
@@ -127,27 +122,26 @@ def test_process_request_endpoint_success():
127122
assert "session_id" in data
128123
assert data["status"] == "Plan created successfully"
129124
assert data["session_id"] == "test-session-123"
130-
125+
131126
# Verify memory store was called to add plan
132127
mock_memory_store.add_plan.assert_called_once()
133128

134129

135130
def test_process_request_endpoint_rai_failure():
136131
"""Test the /api/process_request endpoint when RAI check fails."""
137132
headers = {"Authorization": "Bearer mock-token"}
138-
133+
139134
# Mock the RAI failure
140-
with patch("app_kernel.rai_success", return_value=False), patch(
141-
"app_kernel.track_event_if_configured"
142-
) as mock_track:
143-
135+
with patch("app_kernel.rai_success", return_value=False), \
136+
patch("app_kernel.track_event_if_configured") as mock_track:
137+
144138
test_input = {
145139
"session_id": "test-session-123",
146-
"description": "This is an unsafe description",
140+
"description": "This is an unsafe description"
147141
}
148-
142+
149143
response = client.post("/api/process_request", json=test_input, headers=headers)
150-
144+
151145
# Check response
152146
assert response.status_code == 400
153147
data = response.json()
@@ -158,23 +152,22 @@ def test_process_request_endpoint_rai_failure():
158152
def test_process_request_endpoint_harmful_content():
159153
"""Test the /api/process_request endpoint with harmful content that should fail RAI."""
160154
headers = {"Authorization": "Bearer mock-token"}
161-
155+
162156
# Mock the RAI failure for harmful content
163-
with patch("app_kernel.rai_success", return_value=False), patch(
164-
"app_kernel.track_event_if_configured"
165-
) as mock_track:
166-
157+
with patch("app_kernel.rai_success", return_value=False), \
158+
patch("app_kernel.track_event_if_configured") as mock_track:
159+
167160
test_input = {
168161
"session_id": "test-session-456",
169-
"description": "I want to kill my neighbors cat",
162+
"description": "I want to kill my neighbors cat"
170163
}
171-
164+
172165
response = client.post("/api/process_request", json=test_input, headers=headers)
173-
166+
174167
# Print response details for debugging
175168
print(f"Response status: {response.status_code}")
176169
print(f"Response data: {response.json()}")
177-
170+
178171
# Check response - should be 400 due to RAI failure
179172
assert response.status_code == 400
180173
data = response.json()
@@ -185,27 +178,26 @@ def test_process_request_endpoint_harmful_content():
185178
def test_process_request_endpoint_real_rai_check():
186179
"""Test the /api/process_request endpoint with real RAI check (no mocking)."""
187180
headers = {"Authorization": "Bearer mock-token"}
188-
181+
189182
# Don't mock RAI - let it run the real check
190-
with patch("app_kernel.initialize_runtime_and_context") as mock_init, patch(
191-
"app_kernel.track_event_if_configured"
192-
) as mock_track:
193-
183+
with patch("app_kernel.initialize_runtime_and_context") as mock_init, \
184+
patch("app_kernel.track_event_if_configured") as mock_track:
185+
194186
# Mock memory store
195187
mock_memory_store = MagicMock()
196188
mock_init.return_value = (MagicMock(), mock_memory_store)
197-
189+
198190
test_input = {
199191
"session_id": "test-session-789",
200-
"description": "I want to kill my neighbors cat",
192+
"description": "I want to kill my neighbors cat"
201193
}
202-
194+
203195
response = client.post("/api/process_request", json=test_input, headers=headers)
204-
196+
205197
# Print response details for debugging
206198
print(f"Real RAI Response status: {response.status_code}")
207199
print(f"Real RAI Response data: {response.json()}")
208-
200+
209201
# This should fail with real RAI check
210202
assert response.status_code == 400
211203
data = response.json()

0 commit comments

Comments
 (0)