Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/http-client-python"
---

Add synchronous and asynchronous mock API tests for Azure LRO status metadata scenarios.
102 changes: 65 additions & 37 deletions packages/http-client-python/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/http-client-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@
"tsx": "^4.23.13"
},
"devDependencies": {
"@azure-tools/typespec-autorest": "~0.73.0",
"@azure-tools/typespec-azure-core": "~0.73.0",
"@azure-tools/typespec-azure-resource-manager": "~0.73.0",
"@azure-tools/typespec-azure-rulesets": "~0.73.0",
"@azure-tools/typespec-client-generator-core": "~0.73.0",
"@azure-tools/azure-http-specs": "0.1.0-alpha.46",
"@azure-tools/typespec-autorest": "0.74.0-dev.2",
"@azure-tools/typespec-azure-core": "0.74.0-dev.5",
"@azure-tools/typespec-azure-resource-manager": "0.74.0-dev.10",
"@azure-tools/typespec-azure-rulesets": "0.74.0-dev.11",
"@azure-tools/typespec-client-generator-core": "0.74.0-dev.11",
"@azure-tools/azure-http-specs": "0.1.0-alpha.47-dev.3",
"@typespec/compiler": "^1.17.0",
"@typespec/http": "^1.17.0",
"@typespec/openapi": "^1.17.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import datetime

import pytest
import pytest_asyncio
from azure.core.exceptions import HttpResponseError
from specs.azure.clientgenerator.core.lro.metadata import models
from specs.azure.clientgenerator.core.lro.metadata.aio import MetadataClient


@pytest_asyncio.fixture
async def client():
async with MetadataClient() as client:
yield client


@pytest.mark.asyncio
async def test_content_analysis_status(client: MetadataClient):
status = await client.get_content_analysis_status(operation_id="usage-123")

assert status.usage.document_pages_basic == 1
assert status.usage.tokens["input"] == 10


@pytest.mark.asyncio
async def test_analyze_with_usage(client: MetadataClient, async_polling_method):
result = await (
await client.begin_analyze_with_usage(
analyzer_id="analyzer-1", polling_interval=0, polling=async_polling_method
)
).result()

assert result == models.ContentAnalysisResult(contents="The document contains three sections.")


@pytest.mark.asyncio
async def test_language_job_status(client: MetadataClient):
status = await client.get_language_job_status(job_id="job-success")

assert status.job_id == "job-success"
assert status.display_name == "Analyze documents"
assert status.created_date_time == datetime.datetime(2025, 1, 1, 10, tzinfo=datetime.timezone.utc)
assert status.expiration_date_time == datetime.datetime(2025, 1, 2, 10, tzinfo=datetime.timezone.utc)
assert status.statistics.total == 3


@pytest.mark.asyncio
async def test_analyze_job(client: MetadataClient, async_polling_method):
result = await (await client.begin_analyze_job(polling_interval=0, polling=async_polling_method)).result()

assert result == models.LanguageJobResult(documents=["document-1", "document-2", "document-3"])


@pytest.mark.asyncio
async def test_analyze_job_with_failure(client: MetadataClient, async_polling_method):
with pytest.raises(HttpResponseError):
await (await client.begin_analyze_job_with_failure(polling_interval=0, polling=async_polling_method)).result()
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import datetime

import pytest
from azure.core.exceptions import HttpResponseError
from specs.azure.clientgenerator.core.lro.metadata import MetadataClient, models


@pytest.fixture
def client():
with MetadataClient() as client:
yield client


def test_content_analysis_status(client: MetadataClient):
status = client.get_content_analysis_status(operation_id="usage-123")

assert status.usage.document_pages_basic == 1
assert status.usage.tokens["input"] == 10


def test_analyze_with_usage(client: MetadataClient, polling_method):
result = client.begin_analyze_with_usage(
analyzer_id="analyzer-1", polling_interval=0, polling=polling_method
).result()

assert result == models.ContentAnalysisResult(contents="The document contains three sections.")


def test_language_job_status(client: MetadataClient):
status = client.get_language_job_status(job_id="job-success")

assert status.job_id == "job-success"
assert status.display_name == "Analyze documents"
assert status.created_date_time == datetime.datetime(2025, 1, 1, 10, tzinfo=datetime.timezone.utc)
assert status.expiration_date_time == datetime.datetime(2025, 1, 2, 10, tzinfo=datetime.timezone.utc)
assert status.statistics.total == 3


def test_analyze_job(client: MetadataClient, polling_method):
result = client.begin_analyze_job(polling_interval=0, polling=polling_method).result()

assert result == models.LanguageJobResult(documents=["document-1", "document-2", "document-3"])


def test_analyze_job_with_failure(client: MetadataClient, polling_method):
with pytest.raises(HttpResponseError):
client.begin_analyze_job_with_failure(polling_interval=0, polling=polling_method).result()