Skip to content

Commit eaa4b76

Browse files
committed
refactor(tests): move inline imports to top of file
All imports in test functions moved to module-level per project convention.
1 parent 0683245 commit eaa4b76

3 files changed

Lines changed: 20 additions & 60 deletions

File tree

tests/aignostics/application/cli_test.py

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import contextlib
44
import json
55
import platform
6+
import random
67
import re
78
from collections.abc import Generator
89
from datetime import UTC, datetime, timedelta
@@ -11,6 +12,19 @@
1112
from unittest.mock import MagicMock, patch
1213

1314
import 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+
)
1428
from loguru import logger
1529
from tenacity import Retrying, retry, stop_after_attempt, wait_exponential
1630
from typer.testing import CliRunner
@@ -851,8 +865,6 @@ def test_cli_run_list_for_organization(runner: CliRunner) -> None:
851865
@pytest.mark.unit
852866
def 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
867879
def 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
902912
def 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
12271225
def 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
17771769
def 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
18741864
def 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
18921880
def 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
19801966
def 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
20001984
def 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
20302012
def 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()

tests/aignostics/platform/e2e_test.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
77
"""
88

9+
import itertools
910
import os
1011
import tempfile
1112
from datetime import UTC, datetime, timedelta
1213
from pathlib import Path
1314

1415
import pytest
16+
import requests
1517
from aignx.codegen.models import (
1618
ArtifactOutput,
1719
ArtifactState,
@@ -770,8 +772,6 @@ def _find_available_artifact_in_recent_heta_run() -> tuple[Run, str] | None:
770772
Returns:
771773
tuple[Run, str] | None: A bound (Run, output_artifact_id) pair, or None.
772774
"""
773-
import itertools
774-
775775
client = platform.Client()
776776
# client.runs.list yields Run handles directly; iterate lazily and cap to N
777777
# so the canary stays well under its 60s timeout even on a busy staging env.
@@ -827,10 +827,6 @@ def test_platform_artifact_file_endpoint_resolves_to_working_url(record_property
827827
"""
828828
record_property("tested-item-id", "SPEC-PLATFORM-SERVICE")
829829

830-
# Local import: requests is already an SDK dep; importing at use-site avoids
831-
# adding to the module-level imports of an otherwise import-heavy test file.
832-
import requests
833-
834830
found = _find_available_artifact_in_recent_heta_run()
835831
if found is None:
836832
pytest.skip(

tests/aignostics/system/cli_test.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""Tests to verify the CLI functionality of the system module."""
22

3+
import json
34
import logging
45
import os
56
from pathlib import Path
67
from unittest.mock import AsyncMock, MagicMock, patch
78

89
import pytest
10+
import yaml
911
from typer.testing import CliRunner
1012

1113
from aignostics.cli import cli
12-
from aignostics.utils import __project_name__
14+
from aignostics.utils import Health, __project_name__
1315
from tests.conftest import normalize_output
1416

1517
THE_VALUE = "test_secret_value_not_real_for_testing_only"
@@ -20,8 +22,6 @@
2022
def test_cli_health_json_format(mock_service: MagicMock, runner: CliRunner, record_property) -> None:
2123
"""Check health CLI renders UP status correctly as JSON."""
2224
record_property("tested-item-id", "TEST-SYSTEM-CLI-HEALTH-JSON")
23-
from aignostics.utils import Health
24-
2525
mock_service.health = AsyncMock(return_value=Health(status=Health.Code.UP))
2626
result = runner.invoke(cli, ["system", "health"])
2727
assert result.exit_code == 0
@@ -33,8 +33,6 @@ def test_cli_health_json_format(mock_service: MagicMock, runner: CliRunner, reco
3333
def test_cli_health_yaml_format(mock_service: MagicMock, runner: CliRunner, record_property) -> None:
3434
"""Check health CLI renders UP status correctly as YAML."""
3535
record_property("tested-item-id", "TEST-SYSTEM-CLI-HEALTH-YAML")
36-
from aignostics.utils import Health
37-
3836
mock_service.health = AsyncMock(return_value=Health(status=Health.Code.UP))
3937
result = runner.invoke(cli, ["system", "health", "--output-format", "yaml"])
4038
assert result.exit_code == 0
@@ -48,8 +46,6 @@ def test_cli_health_yaml_format(mock_service: MagicMock, runner: CliRunner, reco
4846
@pytest.mark.timeout(timeout=60)
4947
def test_cli_health_json(runner: CliRunner) -> None:
5048
"""Check health CLI returns valid JSON with a valid status value."""
51-
import json
52-
5349
result = runner.invoke(cli, ["system", "health"])
5450
data = json.loads(result.stdout)
5551
assert data["status"] in {"UP", "DEGRADED", "DOWN"}
@@ -59,8 +55,6 @@ def test_cli_health_json(runner: CliRunner) -> None:
5955
@pytest.mark.timeout(timeout=30)
6056
def test_cli_health_yaml(runner: CliRunner) -> None:
6157
"""Check health CLI returns valid YAML with a valid status value."""
62-
import yaml
63-
6458
result = runner.invoke(cli, ["system", "health", "--output-format", "yaml"])
6559
data = yaml.safe_load(result.stdout)
6660
assert data["status"] in {"UP", "DEGRADED", "DOWN"}
@@ -118,8 +112,6 @@ def test_cli_info_secrets(runner: CliRunner, caplog: pytest.LogCaptureFixture, r
118112
@patch("aignostics.system._cli._service")
119113
def test_cli_health_up_exits_zero(mock_service: MagicMock, runner: CliRunner) -> None:
120114
"""Check health command exits with code 0 when status is UP."""
121-
from aignostics.utils import Health
122-
123115
mock_service.health = AsyncMock(return_value=Health(status=Health.Code.UP))
124116
result = runner.invoke(cli, ["system", "health"])
125117
assert result.exit_code == 0
@@ -129,8 +121,6 @@ def test_cli_health_up_exits_zero(mock_service: MagicMock, runner: CliRunner) ->
129121
@patch("aignostics.system._cli._service")
130122
def test_cli_health_degraded_exits_zero(mock_service: MagicMock, runner: CliRunner) -> None:
131123
"""Check health command exits with code 0 when status is DEGRADED."""
132-
from aignostics.utils import Health
133-
134124
mock_service.health = AsyncMock(return_value=Health(status=Health.Code.DEGRADED, reason="some component degraded"))
135125
result = runner.invoke(cli, ["system", "health"])
136126
assert result.exit_code == 0
@@ -140,8 +130,6 @@ def test_cli_health_degraded_exits_zero(mock_service: MagicMock, runner: CliRunn
140130
@patch("aignostics.system._cli._service")
141131
def test_cli_health_down_exits_one(mock_service: MagicMock, runner: CliRunner) -> None:
142132
"""Check health command exits with code 1 when status is DOWN."""
143-
from aignostics.utils import Health
144-
145133
mock_service.health = AsyncMock(return_value=Health(status=Health.Code.DOWN, reason="service unavailable"))
146134
result = runner.invoke(cli, ["system", "health"])
147135
assert result.exit_code == 1

0 commit comments

Comments
 (0)