Skip to content

Commit f177e5c

Browse files
committed
Fix up more app tests
1 parent ebfcfc5 commit f177e5c

File tree

10 files changed

+249
-79
lines changed

10 files changed

+249
-79
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ coverage.xml
5454
.hypothesis/
5555
.pytest_cache/
5656
cover/
57+
coverage_report.html
5758

5859
# Translations
5960
*.mo

CONTRIBUTING.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ Run the tests:
6363
python -m pytest
6464
```
6565

66-
Check the coverage report to make sure your changes are covered.
66+
If test snapshots need updating (and the changes are expected), you can update them by running:
6767

6868
```shell
69-
python -m pytest --cov
69+
python -m pytest --snapshot-update
70+
```
71+
72+
Once tests are passing, generate a coverage report to make sure your changes are covered:
73+
74+
```shell
75+
pytest --cov --cov-report=xml
76+
diff-cover coverage.xml --format html:coverage_report.html
77+
open coverage_report.html
7078
```
7179

7280
## Running E2E tests

app/backend/approaches/approach.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from abc import ABC
32
from collections.abc import AsyncGenerator, Awaitable
43
from dataclasses import dataclass
@@ -24,6 +23,7 @@
2423
VectorizedQuery,
2524
VectorQuery,
2625
)
26+
from azure.storage.blob.aio import ContainerClient
2727
from openai import AsyncOpenAI, AsyncStream
2828
from openai.types import CompletionUsage
2929
from openai.types.chat import (
@@ -168,11 +168,12 @@ def __init__(
168168
embedding_dimensions: int,
169169
embedding_field: str,
170170
openai_host: str,
171-
vision_endpoint: str,
172-
vision_token_provider: Callable[[], Awaitable[str]],
173171
prompt_manager: PromptManager,
174172
reasoning_effort: Optional[str] = None,
175173
multimodal_enabled: bool = False,
174+
vision_endpoint: Optional[str] = None,
175+
vision_token_provider: Optional[Callable[[], Awaitable[str]]] = None,
176+
images_blob_container_client: Optional[ContainerClient] = None,
176177
):
177178
self.search_client = search_client
178179
self.openai_client = openai_client
@@ -184,12 +185,13 @@ def __init__(
184185
self.embedding_dimensions = embedding_dimensions
185186
self.embedding_field = embedding_field
186187
self.openai_host = openai_host
187-
self.vision_endpoint = vision_endpoint
188-
self.vision_token_provider = vision_token_provider
189188
self.prompt_manager = prompt_manager
190189
self.reasoning_effort = reasoning_effort
191190
self.include_token_usage = True
192191
self.multimodal_enabled = multimodal_enabled
192+
self.vision_endpoint = vision_endpoint
193+
self.vision_token_provider = vision_token_provider
194+
self.images_blob_container_client = images_blob_container_client
193195

194196
def get_default_llm_inputs(self) -> str:
195197
"""
@@ -381,7 +383,7 @@ def nonewlines(s: str) -> str:
381383

382384
for doc in results:
383385
# Get the citation for the source page
384-
citation = self.get_citation(doc.sourcepage or "")
386+
citation = self.get_citation(doc.sourcepage)
385387
citations.append(citation)
386388

387389
# If semantic captions are used, extract captions; otherwise, use content
@@ -403,20 +405,13 @@ def nonewlines(s: str) -> str:
403405

404406
return text_sources, image_sources, citations
405407

406-
def get_citation(self, sourcepage: str) -> str:
407-
path, ext = os.path.splitext(sourcepage)
408-
if ext.lower() == ".png":
409-
page_idx = path.rfind("-")
410-
page_number = int(path[page_idx + 1 :])
411-
return f"{path[:page_idx]}.pdf#page={page_number}"
412-
return sourcepage
413-
414-
def get_image_citation(self, sourcepage: str, image_url: str):
415-
source_page_citation = self.get_citation(sourcepage)
416-
# extract the image filename from image_url, last part after the last slash
408+
def get_citation(self, sourcepage: Optional[str]):
409+
return sourcepage or ""
410+
411+
def get_image_citation(self, sourcepage: Optional[str], image_url: str):
412+
sourcepage_citation = self.get_citation(sourcepage)
417413
image_filename = image_url.split("/")[-1]
418-
if source_page_citation:
419-
return f"{source_page_citation}({image_filename})"
414+
return f"{sourcepage_citation}({image_filename})"
420415

421416
async def compute_text_embedding(self, q: str):
422417
SUPPORTED_DIMENSIONS_MODEL = {

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ pytest-snapshot
1212
pre-commit
1313
pip-tools
1414
mypy==1.14.1
15+
diff_cover

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def mock_env(monkeypatch, request):
405405
with mock.patch.dict(os.environ, clear=True):
406406
monkeypatch.setenv("AZURE_STORAGE_ACCOUNT", "test-storage-account")
407407
monkeypatch.setenv("AZURE_STORAGE_CONTAINER", "test-storage-container")
408+
monkeypatch.setenv("AZURE_IMAGESTORAGE_CONTAINER", "test-image-container")
408409
monkeypatch.setenv("AZURE_STORAGE_RESOURCE_GROUP", "test-storage-rg")
409410
monkeypatch.setenv("AZURE_SUBSCRIPTION_ID", "test-storage-subid")
410411
monkeypatch.setenv("ENABLE_LANGUAGE_PICKER", "true")

tests/mocks.py

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,44 +86,64 @@ def __init__(self, text, highlights=None, additional_properties=None):
8686
class MockAsyncSearchResultsIterator:
8787
def __init__(self, search_text, vector_queries: Optional[list[VectorQuery]]):
8888
if search_text == "interest rates" or (
89-
vector_queries and any([vector.fields == "imageEmbedding" for vector in vector_queries])
89+
vector_queries and any([vector.fields == "images/embeddings" for vector in vector_queries])
9090
):
9191
self.data = [
9292
[
9393
{
94+
"id": "file-Financial_Market_Analysis_Report_2023_pdf-46696E616E6369616C204D61726B657420416E616C79736973205265706F727420323032332E706466-page-7",
95+
"content": ' This\nsection examines the correlations between stock indices, cryptocurrency prices, and commodity prices,\nrevealing how changes in one market can have ripple effects across the financial ecosystem.### Impact of Macroeconomic Factors\n\n\n<figure><figcaption>Impact of Interest Rates, Inflation, and GDP Growth on Financial Markets<br>The image is a line graph titled "on Financial Markets" displaying data from 2018 to 2023. It tracks three variables: Interest Rates %, Inflation Data %, and GDP Growth %, each represented by a different colored line (blue for Interest Rates, orange for Inflation Data, and gray for GDP Growth). Interest Rates % start around 2% in 2018, dip to about 0.25% in 2021, then rise to 1.5% in 2023. Inflation Data % begin at approximately 1.9% in 2018, rise to a peak near 3.4% in 2022, and then decrease to 2.5% in 2023. GDP Growth % shows significant fluctuations, starting at 3% in 2018, plunging to almost -4% in 2020, then rebounding to around 4.5% in 2021 before gradually declining to around 2.8% in 2023.</figcaption></figure>\n\n\nMacroeconomic factors such as interest\nrates, inflation, and GDP growth play a\npivotal role in shaping financial markets.',
9496
"category": None,
97+
"sourcepage": "Financial Market Analysis Report 2023.pdf#page=7",
9598
"sourcefile": "Financial Market Analysis Report 2023.pdf",
96-
"image_embedding": [
97-
-0.86035156,
98-
1.3310547,
99-
3.9804688,
100-
-0.6425781,
101-
-2.7246094,
102-
-1.6308594,
103-
-0.69091797,
104-
-2.2539062,
105-
-0.09942627,
99+
"oids": None,
100+
"groups": None,
101+
"captions": [],
102+
"score": 0.03333333507180214,
103+
"reranker_score": 3.207321882247925,
104+
"search_agent_query": None,
105+
"images": [],
106+
},
107+
{
108+
"id": "file-Financial_Market_Analysis_Report_2023_pdf-46696E616E6369616C204D61726B657420416E616C79736973205265706F727420323032332E706466-page-8",
109+
"content": "</figcaption></figure>\n\n\nMacroeconomic factors such as interest\nrates, inflation, and GDP growth play a\npivotal role in shaping financial markets.\nThis section analyzes how these factors\nhave influenced stock, cryptocurrency,\nand commodity markets over recent\nyears, providing insights into the\ncomplex relationship between the\neconomy and financial market\nperformance.## Future Predictions and Trends\n\n\n<figure><figcaption>Relative Growth Trends for S&P 500, Bitcoin, and Oil Prices (2024 Indexed to 100)<br>This horizontal bar chart shows prices indexed to 100 for the years 2024 to 2028. It compares the prices of Oil, Bitcoin, and the S&P 500 across these years. In 2024, all three have an index value of 100. From 2025 to 2028, all three generally increase, with Bitcoin consistently having the highest index value, followed closely by the S&P 500 and then Oil. The chart uses grey bars for Oil, orange bars for Bitcoin, and blue bars for the S&P 500.</figcaption></figure>\n\n\nBased on historical data, current trends,\nand economic indicators, this section\npresents predictions for the future of\nfinancial markets.",
110+
"category": None,
111+
"sourcepage": "Financial Market Analysis Report 2023.pdf#page=8",
112+
"sourcefile": "Financial Market Analysis Report 2023.pdf",
113+
"oids": None,
114+
"groups": None,
115+
"captions": [],
116+
"score": 0.04945354908704758,
117+
"reranker_score": 2.573531150817871,
118+
"search_agent_query": None,
119+
"images": [
120+
{
121+
"url": "https://sticygqdubf4x6w.blob.core.windows.net/images/Financial%20Market%20Analysis%20Report%202023.pdf/page7/figure8_1.png",
122+
"description": '<figure><figcaption>Impact of Interest Rates, Inflation, and GDP Growth on Financial Markets<br>The image is a line graph titled "on Financial Markets" displaying data from 2018 to 2023. It tracks three variables: Interest Rates %, Inflation Data %, and GDP Growth %, each represented by a different colored line (blue for Interest Rates, orange for Inflation Data, and gray for GDP Growth). Interest Rates % start around 2% in 2018, dip to about 0.25% in 2021, then rise to 1.5% in 2023. Inflation Data % begin at approximately 1.9% in 2018, rise to a peak near 3.4% in 2022, and then decrease to 2.5% in 2023. GDP Growth % shows significant fluctuations, starting at 3% in 2018, plunging to almost -4% in 2020, then rebounding to around 4.5% in 2021 before gradually declining to around 2.8% in 2023.</figcaption></figure>',
123+
"boundingbox": [63.1008, 187.9416, 561.3408000000001, 483.5088],
124+
}
106125
],
107-
"content": "3</td><td>1</td></tr></table>\nFinancial markets are interconnected, with movements in one segment often influencing others. This section examines the correlations between stock indices, cryptocurrency prices, and commodity prices, revealing how changes in one market can have ripple effects across the financial ecosystem.Impact of Macroeconomic Factors\nImpact of Interest Rates, Inflation, and GDP Growth on Financial Markets\n5\n4\n3\n2\n1\n0\n-1 2018 2019\n-2\n-3\n-4\n-5\n2020\n2021 2022 2023\nMacroeconomic factors such as interest rates, inflation, and GDP growth play a pivotal role in shaping financial markets. This section analyzes how these factors have influenced stock, cryptocurrency, and commodity markets over recent years, providing insights into the complex relationship between the economy and financial market performance.\n-Interest Rates % -Inflation Data % GDP Growth % :unselected: :unselected:Future Predictions and Trends\nRelative Growth Trends for S&P 500, Bitcoin, and Oil Prices (2024 Indexed to 100)\n2028\nBased on historical data, current trends, and economic indicators, this section presents predictions ",
108-
"id": "file-Financial_Market_Analysis_Report_2023_pdf-46696E616E6369616C204D61726B657420416E616C79736973205265706F727420323032332E706466-page-14",
109-
"sourcepage": "Financial Market Analysis Report 2023-6.png",
110-
"embedding": [
111-
-0.012668486,
112-
-0.02251158,
113-
0.008822813,
114-
-0.02531081,
115-
-0.014493219,
116-
-0.019503059,
117-
-0.015605063,
118-
-0.0141138835,
119-
-0.019699266,
120-
...,
126+
},
127+
{
128+
"id": "file-Financial_Market_Analysis_Report_2023_pdf-46696E616E6369616C204D61726B657420416E616C79736973205265706F727420323032332E706466-page-1",
129+
"content": 'advanced data\nanalytics to present a clear picture of the complex interplay between\ndifferent financial markets and their potential trajectories## Introduction to Financial Markets\n\n\n<figure><figcaption>Global Financial Market Distribution (2023)<br>The pie chart features four categories: Stocks, Bonds, Cryptocurrencies, and Commodities. Stocks take up the largest portion of the chart, represented in blue, accounting for 40%. Bonds are the second largest, shown in orange, making up 25%. Cryptocurrencies are depicted in gray and cover 20% of the chart. Commodities are the smallest segment, shown in yellow, comprising 15%.</figcaption></figure>\n\n\nThe global financial market is a vast and intricate network of\nexchanges, instruments, and assets, ranging from traditional stocks\nand bonds to modern cryptocurrencies and commodities. Each\nsegment plays a crucial role in the overall economy, and their\ninteractions can have profound effects on global financial stability.\nThis section provides an overview of these segments and sets the\nstage for a detailed analysis## Stock Market Overview\n\n\n<figure><figcaption><br>The image is a line graph titled "5-Year Trend of the S&P 500 Index.',
130+
"category": None,
131+
"sourcepage": "Financial Market Analysis Report 2023.pdf#page=2",
132+
"sourcefile": "Financial Market Analysis Report 2023.pdf",
133+
"oids": None,
134+
"groups": None,
135+
"captions": [],
136+
"score": 0.0317540317773819,
137+
"reranker_score": 1.8846203088760376,
138+
"search_agent_query": None,
139+
"images": [
140+
{
141+
"url": "https://sticygqdubf4x6w.blob.core.windows.net/images/Financial%20Market%20Analysis%20Report%202023.pdf/page7/figure8_1.png",
142+
"description": '<figure><figcaption>Impact of Interest Rates, Inflation, and GDP Growth on Financial Markets<br>The image is a line graph titled "on Financial Markets" displaying data from 2018 to 2023. It tracks three variables: Interest Rates %, Inflation Data %, and GDP Growth %, each represented by a different colored line (blue for Interest Rates, orange for Inflation Data, and gray for GDP Growth). Interest Rates % start around 2% in 2018, dip to about 0.25% in 2021, then rise to 1.5% in 2023. Inflation Data % begin at approximately 1.9% in 2018, rise to a peak near 3.4% in 2022, and then decrease to 2.5% in 2023. GDP Growth % shows significant fluctuations, starting at 3% in 2018, plunging to almost -4% in 2020, then rebounding to around 4.5% in 2021 before gradually declining to around 2.8% in 2023.</figcaption></figure>',
143+
"boundingbox": [63.1008, 187.9416, 561.3408000000001, 483.5088],
144+
}
121145
],
122-
"@search.score": 0.04972677677869797,
123-
"@search.reranker_score": 3.1704962253570557,
124-
"@search.highlights": None,
125-
"@search.captions": None,
126-
}
146+
},
127147
]
128148
]
129149
else:

0 commit comments

Comments
 (0)