|
| 1 | +import json |
| 2 | +import logging |
| 3 | + |
| 4 | +import aiohttp |
| 5 | +import pytest |
| 6 | + |
| 7 | +from prepdocslib.mediadescriber import ContentUnderstandingDescriber |
| 8 | + |
| 9 | +from .mocks import MockAzureCredential, MockResponse |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.asyncio |
| 13 | +async def test_contentunderstanding_analyze(monkeypatch, caplog): |
| 14 | + |
| 15 | + def mock_post(*args, **kwargs): |
| 16 | + if kwargs.get("url").find("badanalyzer") > 0: |
| 17 | + return MockResponse( |
| 18 | + status=200, |
| 19 | + headers={ |
| 20 | + "Operation-Location": "https://testcontentunderstanding.cognitiveservices.azure.com/contentunderstanding/analyzers/badanalyzer/operations/7f313e00-4da1-4b19-a25e-53f121c24d10?api-version=2024-12-01-preview" |
| 21 | + }, |
| 22 | + ) |
| 23 | + if kwargs.get("url").endswith("contentunderstanding/analyzers/image_analyzer:analyze"): |
| 24 | + return MockResponse( |
| 25 | + status=200, |
| 26 | + headers={ |
| 27 | + "Operation-Location": "https://testcontentunderstanding.cognitiveservices.azure.com/contentunderstanding/analyzers/image_analyzer/results/53e4c016-d2c0-48a9-a9f4-38891f7d45f0?api-version=2024-12-01-preview" |
| 28 | + }, |
| 29 | + ) |
| 30 | + else: |
| 31 | + raise Exception("Unexpected URL for mock call to ClientSession.post()") |
| 32 | + |
| 33 | + monkeypatch.setattr(aiohttp.ClientSession, "post", mock_post) |
| 34 | + |
| 35 | + num_poll_calls = 0 |
| 36 | + |
| 37 | + def mock_get(self, url, **kwargs): |
| 38 | + if url.endswith( |
| 39 | + "contentunderstanding/analyzers/image_analyzer/results/53e4c016-d2c0-48a9-a9f4-38891f7d45f0?api-version=2024-12-01-preview" |
| 40 | + ): |
| 41 | + return MockResponse( |
| 42 | + status=200, |
| 43 | + text=json.dumps( |
| 44 | + { |
| 45 | + "id": "f8c4c1c0-71c3-410c-a723-d223e0a84a88", |
| 46 | + "status": "Succeeded", |
| 47 | + "result": { |
| 48 | + "analyzerId": "image_analyzer", |
| 49 | + "apiVersion": "2024-12-01-preview", |
| 50 | + "createdAt": "2024-12-05T17:33:04Z", |
| 51 | + "warnings": [], |
| 52 | + "contents": [ |
| 53 | + { |
| 54 | + "markdown": "\n", |
| 55 | + "fields": { |
| 56 | + "Description": { |
| 57 | + "type": "string", |
| 58 | + "valueString": "The bar chart titled 'Prices (2024 Indexed to 100)' compares the indexed prices of Oil, Bitcoin, and S&P 500 from 2024 to 2028. Each year is represented by a set of three horizontal bars, with Oil in gray, Bitcoin in orange, and S&P 500 in blue. The index is based on the year 2024, where all values start at 100. Over the years, Bitcoin shows the most significant increase, reaching around 130 by 2028, while Oil and S&P 500 show moderate increases.\n\n<table><thead><tr><td>Year</td><td>Oil</td><td>Bitcoin</td><td>S&P 500</td></tr></thead><tbody><tr><td>2024</td><td>100</td><td>100</td><td>100</td></tr><tr><td>2025</td><td>105</td><td>110</td><td>1 08</td></tr><tr><td>2026</td><td>110</td><td>115</td><td>112</td></tr><tr><td>2027</td><td>115</td><td>120</td><td>116</td></tr><tr> <td>2028</td><td>120</td><td>130</td><td>120</td></tr></tbody></table>", |
| 59 | + } |
| 60 | + }, |
| 61 | + "kind": "document", |
| 62 | + "startPageNumber": 1, |
| 63 | + "endPageNumber": 1, |
| 64 | + "unit": "pixel", |
| 65 | + "pages": [{"pageNumber": 1}], |
| 66 | + } |
| 67 | + ], |
| 68 | + }, |
| 69 | + } |
| 70 | + ), |
| 71 | + ) |
| 72 | + elif url.endswith( |
| 73 | + "https://testcontentunderstanding.cognitiveservices.azure.com/contentunderstanding/analyzers/badanalyzer/operations/7f313e00-4da1-4b19-a25e-53f121c24d10?api-version=2024-12-01-preview" |
| 74 | + ): |
| 75 | + return MockResponse(status=200, text=json.dumps({"status": "Failed"})) |
| 76 | + elif url.endswith( |
| 77 | + "https://testcontentunderstanding.cognitiveservices.azure.com/contentunderstanding/analyzers/image_analyzer/operations/7f313e00-4da1-4b19-a25e-53f121c24d10?api-version=2024-12-01-preview" |
| 78 | + ): |
| 79 | + nonlocal num_poll_calls |
| 80 | + num_poll_calls += 1 |
| 81 | + if num_poll_calls == 1: |
| 82 | + return MockResponse(status=200, text=json.dumps({"status": "Running"})) |
| 83 | + elif num_poll_calls > 1: |
| 84 | + return MockResponse(status=200, text=json.dumps({"status": "Succeeded"})) |
| 85 | + else: |
| 86 | + raise Exception("Unexpected URL for mock call to ClientSession.get()") |
| 87 | + |
| 88 | + monkeypatch.setattr(aiohttp.ClientSession, "get", mock_get) |
| 89 | + |
| 90 | + def mock_put(self, *args, **kwargs): |
| 91 | + if kwargs.get("url").find("existinganalyzer") > 0: |
| 92 | + return MockResponse(status=409) |
| 93 | + if kwargs.get("url").find("wrongservicename") > 0: |
| 94 | + return MockResponse( |
| 95 | + status=404, |
| 96 | + text=json.dumps( |
| 97 | + {"error": {"code": "ResourceNotFound", "message": "The specified resource does not exist."}} |
| 98 | + ), |
| 99 | + ) |
| 100 | + elif kwargs.get("url").endswith("contentunderstanding/analyzers/image_analyzer"): |
| 101 | + return MockResponse( |
| 102 | + status=201, |
| 103 | + headers={ |
| 104 | + "Operation-Location": "https://testcontentunderstanding.cognitiveservices.azure.com/contentunderstanding/analyzers/image_analyzer/operations/7f313e00-4da1-4b19-a25e-53f121c24d10?api-version=2024-12-01-preview" |
| 105 | + }, |
| 106 | + ) |
| 107 | + else: |
| 108 | + raise Exception("Unexpected URL for mock call to ClientSession.put()") |
| 109 | + |
| 110 | + monkeypatch.setattr(aiohttp.ClientSession, "put", mock_put) |
| 111 | + |
| 112 | + describer = ContentUnderstandingDescriber( |
| 113 | + endpoint="https://testcontentunderstanding.cognitiveservices.azure.com", credential=MockAzureCredential() |
| 114 | + ) |
| 115 | + await describer.create_analyzer() |
| 116 | + await describer.describe_image(b"imagebytes") |
| 117 | + |
| 118 | + describer_wrong_endpoint = ContentUnderstandingDescriber( |
| 119 | + endpoint="https://wrongservicename.cognitiveservices.azure.com", credential=MockAzureCredential() |
| 120 | + ) |
| 121 | + with pytest.raises(Exception): |
| 122 | + await describer_wrong_endpoint.create_analyzer() |
| 123 | + |
| 124 | + describer_existing_analyzer = ContentUnderstandingDescriber( |
| 125 | + endpoint="https://existinganalyzer.cognitiveservices.azure.com", credential=MockAzureCredential() |
| 126 | + ) |
| 127 | + with caplog.at_level(logging.INFO): |
| 128 | + await describer_existing_analyzer.create_analyzer() |
| 129 | + assert "Analyzer 'image_analyzer' already exists." in caplog.text |
| 130 | + |
| 131 | + describer_bad_analyze = ContentUnderstandingDescriber( |
| 132 | + endpoint="https://badanalyzer.cognitiveservices.azure.com", credential=MockAzureCredential() |
| 133 | + ) |
| 134 | + with pytest.raises(Exception): |
| 135 | + await describer_bad_analyze.describe_image(b"imagebytes") |
0 commit comments