33import contextlib
44import json
55import platform
6+ import random
67import re
78from collections .abc import Generator
89from datetime import UTC , datetime , timedelta
1112from unittest .mock import MagicMock , patch
1213
1314import pytest
15+ from aignx .codegen .exceptions import ForbiddenException
16+ from aignx .codegen .exceptions import NotFoundException as ApiNotFound
17+ from aignx .codegen .models import (
18+ ItemOutput ,
19+ ItemResultReadResponse ,
20+ ItemState ,
21+ ItemTerminationReason ,
22+ RunItemStatistics ,
23+ RunOutput ,
24+ RunReadResponse ,
25+ RunState ,
26+ RunTerminationReason ,
27+ )
1428from loguru import logger
1529from tenacity import Retrying , retry , stop_after_attempt , wait_exponential
1630from typer .testing import CliRunner
@@ -851,8 +865,6 @@ def test_cli_run_list_for_organization(runner: CliRunner) -> None:
851865@pytest .mark .unit
852866def test_cli_run_list_forbidden_with_organization (runner : CliRunner ) -> None :
853867 """Check ForbiddenException with --for-organization shows org-specific access denied message."""
854- from aignx .codegen .exceptions import ForbiddenException
855-
856868 with patch .object (
857869 ApplicationService , "application_runs" , side_effect = ForbiddenException (status = 403 , reason = "Forbidden" )
858870 ):
@@ -866,8 +878,6 @@ def test_cli_run_list_forbidden_with_organization(runner: CliRunner) -> None:
866878@pytest .mark .unit
867879def test_cli_run_list_forbidden_without_organization (runner : CliRunner ) -> None :
868880 """Check ForbiddenException without --for-organization shows generic access denied message."""
869- from aignx .codegen .exceptions import ForbiddenException
870-
871881 with patch .object (
872882 ApplicationService , "application_runs" , side_effect = ForbiddenException (status = 403 , reason = "Forbidden" )
873883 ):
@@ -901,18 +911,6 @@ def test_cli_run_describe_not_found(runner: CliRunner, record_property) -> None:
901911@pytest .mark .integration
902912def test_cli_run_describe_json_includes_items (runner : CliRunner ) -> None :
903913 """Check run describe --format=json includes items in output."""
904- from aignx .codegen .models import (
905- ItemOutput ,
906- ItemResultReadResponse ,
907- ItemState ,
908- ItemTerminationReason ,
909- RunItemStatistics ,
910- RunOutput ,
911- RunReadResponse ,
912- RunState ,
913- RunTerminationReason ,
914- )
915-
916914 mock_run_data = RunReadResponse (
917915 run_id = "test-run-id-123" ,
918916 application_id = "test-app" ,
@@ -1226,9 +1224,6 @@ def test_cli_run_update_item_metadata_not_dict(runner: CliRunner) -> None:
12261224@pytest .mark .sequential
12271225def test_cli_run_dump_and_update_custom_metadata (runner : CliRunner , tmp_path : Path ) -> None :
12281226 """Test dumping and updating custom metadata via CLI commands."""
1229- import json
1230- import random
1231-
12321227 unique_tag = f"test_metadata_{ datetime .now (tz = UTC ).timestamp ()} "
12331228 with submitted_run (runner , tmp_path , CSV_CONTENT_SPOT0 , extra_args = ["--tags" , unique_tag , "--force" ]) as run_id :
12341229 # Step 1: Dump initial custom metadata of run
@@ -1317,11 +1312,8 @@ def test_cli_run_dump_and_update_custom_metadata(runner: CliRunner, tmp_path: Pa
13171312@pytest .mark .e2e
13181313@pytest .mark .timeout (timeout = 240 )
13191314@pytest .mark .sequential
1320- def test_cli_run_dump_and_update_item_custom_metadata (runner : CliRunner , tmp_path : Path ) -> None : # noqa: PLR0915
1315+ def test_cli_run_dump_and_update_item_custom_metadata (runner : CliRunner , tmp_path : Path ) -> None :
13211316 """Test dumping and updating item custom metadata via CLI commands."""
1322- import json
1323- import random
1324-
13251317 unique_tag = f"test_item_metadata_{ datetime .now (tz = UTC ).timestamp ()} "
13261318 # CSV_CONTENT_SPOT0 uses SPOT_0_FILENAME as external_id, which the describe output surfaces
13271319 # as "Item External ID: `...`" — the get_external_id() helper below captures it dynamically.
@@ -1777,8 +1769,6 @@ def test_cli_application_version_document_describe_success(runner: CliRunner, re
17771769def test_cli_application_version_document_describe_not_found (runner : CliRunner , record_property ) -> None :
17781770 """`application version document describe` exits 2 with a clear message on 404."""
17791771 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-03" )
1780- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
1781-
17821772 fake_documents = MagicMock ()
17831773 fake_documents .details .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
17841774 fake_client = MagicMock ()
@@ -1874,8 +1864,6 @@ def test_cli_application_version_document_list_json_empty(runner: CliRunner, rec
18741864def test_cli_application_version_document_list_resolve_not_found_text (runner : CliRunner , record_property ) -> None :
18751865 """`application version document list` exits 2 when the application version cannot be resolved."""
18761866 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-01" )
1877- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
1878-
18791867 fake_client = MagicMock ()
18801868 fake_client .applications .versions .documents .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
18811869
@@ -1892,8 +1880,6 @@ def test_cli_application_version_document_list_resolve_not_found_text(runner: Cl
18921880def test_cli_application_version_document_list_resolve_not_found_json (runner : CliRunner , record_property ) -> None :
18931881 """`application version document list --format json` emits structured error on 404."""
18941882 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-01" )
1895- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
1896-
18971883 fake_client = MagicMock ()
18981884 fake_client .applications .versions .documents .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
18991885
@@ -1980,8 +1966,6 @@ def test_cli_application_version_document_describe_json_success(runner: CliRunne
19801966def test_cli_application_version_document_describe_resolve_not_found_text (runner : CliRunner , record_property ) -> None :
19811967 """`describe` exits 2 when the application version cannot be resolved (text format)."""
19821968 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-03" )
1983- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
1984-
19851969 fake_client = MagicMock ()
19861970 fake_client .applications .versions .documents .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
19871971
@@ -2000,8 +1984,6 @@ def test_cli_application_version_document_describe_resolve_not_found_text(runner
20001984def test_cli_application_version_document_describe_resolve_not_found_json (runner : CliRunner , record_property ) -> None :
20011985 """`describe --format json` emits structured error when version cannot be resolved."""
20021986 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-03" )
2003- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
2004-
20051987 fake_client = MagicMock ()
20061988 fake_client .applications .versions .documents .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
20071989
@@ -2030,8 +2012,6 @@ def test_cli_application_version_document_describe_resolve_not_found_json(runner
20302012def test_cli_application_version_document_describe_not_found_json (runner : CliRunner , record_property ) -> None :
20312013 """`describe --format json` emits structured error when the document is missing."""
20322014 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-03" )
2033- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
2034-
20352015 fake_documents = MagicMock ()
20362016 fake_documents .details .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
20372017 fake_client = MagicMock ()
@@ -2115,8 +2095,6 @@ def test_cli_application_version_document_download_resolve_not_found(
21152095) -> None :
21162096 """`download` exits 2 when the application version cannot be resolved."""
21172097 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-04" )
2118- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
2119-
21202098 fake_client = MagicMock ()
21212099 fake_client .applications .versions .documents .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
21222100
@@ -2146,8 +2124,6 @@ def test_cli_application_version_document_download_not_found(
21462124) -> None :
21472125 """`download` exits 2 with a clear message when the document does not exist."""
21482126 record_property ("tested-item-id" , "TC-APPLICATION-CLI-05-04" )
2149- from aignx .codegen .exceptions import NotFoundException as ApiNotFound
2150-
21512127 fake_documents = MagicMock ()
21522128 fake_documents .download_to_path .side_effect = ApiNotFound (status = 404 , reason = API_REASON_NOT_FOUND )
21532129 fake_client = MagicMock ()
0 commit comments