diff --git a/tests/README.md b/tests/README.md index 4f8c9d15..c74ec785 100644 --- a/tests/README.md +++ b/tests/README.md @@ -63,6 +63,7 @@ To run them against a different Apify API environment, also set `APIFY_INTEGRATI - `client`: an `ApifyClient` or `ApifyClientAsync` for the main test user. Each test that uses it runs four times, once for each combination of sync or async client and `impit` or `httpx2` transport. - `client_with_request_body_compressor`: a sync client with the `impit` transport, run once per built-in request-body compressor (gzip, brotli, and zstd). `request_body_compressor` gives the compressor of the current run, with its `compress` method wrapped in a `Mock`. - `is_async`: `True` when the test runs with the async client. +- `build_id_of_own_actor`: the ID of a finished build of an Actor owned by the main test user. The API serves a build log only to a user who can write to or build the Actor, so a test that reads a build log uses this build, not a build of a public Actor. - `test_dataset_of_another_user` and `test_kvs_of_another_user`: a dataset and a key-value store owned by the second user, with known content and the signatures for accessing it. ### Writing integration tests diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 73ae46d4..45418911 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -16,6 +16,7 @@ KvsFixture, get_api_url, get_crypto_random_object_id, + get_random_resource_name, open_apify_client, ) from apify_client import ApifyClient, ApifyClientAsync @@ -114,6 +115,46 @@ def test_kvs_of_another_user(api_token_2: str) -> Generator[KvsFixture]: kvs_client.delete() +@pytest.fixture(scope='session') +def build_id_of_own_actor(api_token: str) -> Generator[str]: + """ID of a finished build of an Actor owned by the primary test user. + + The API serves a build log only to a user who can write to or build the Actor, so the builds of a public Actor + such as `apify/hello-world` have no log the test user can read. + """ + api_url = get_api_url() + client = ApifyClient(api_token, api_url=api_url) + + actor = client.actors().create( + name=get_random_resource_name('actor'), + versions=[ + { + 'versionNumber': '0.1', + 'sourceType': 'SOURCE_FILES', + 'buildTag': 'latest', + 'sourceFiles': [ + { + 'name': 'main.js', + 'format': 'TEXT', + 'content': 'console.log("Hello")', + } + ], + } + ], + ) + actor_client = client.actor(actor.id) + + try: + build = actor_client.build(version_number='0.1') + finished_build = client.build(build.id).wait_for_finish() + assert finished_build is not None + assert finished_build.status in ('SUCCEEDED', 'FAILED') + + yield build.id + finally: + actor_client.delete() + + @pytest.fixture( params=[ pytest.param(HttpClientClasses(sync=ImpitHttpClient, async_=ImpitHttpClientAsync), id='impit'), diff --git a/tests/integration/test_build.py b/tests/integration/test_build.py index 648ffeea..a2ed02fa 100644 --- a/tests/integration/test_build.py +++ b/tests/integration/test_build.py @@ -86,27 +86,9 @@ async def test_user_builds_list(client: ApifyClient | ApifyClientAsync) -> None: assert isinstance(builds_page.items, list) -async def test_build_log(client: ApifyClient | ApifyClientAsync) -> None: +async def test_build_log(client: ApifyClient | ApifyClientAsync, build_id_of_own_actor: str) -> None: """Test getting build log.""" - # First list builds to get a completed build ID - actor = client.actor(HELLO_WORLD_ACTOR) - builds_page = await maybe_await(actor.builds().list(limit=5)) - assert isinstance(builds_page, ListOfBuilds) - assert builds_page.items - - # Find a completed build (SUCCEEDED status) - completed_build = None - for build in builds_page.items: - if build.status and build.status == 'SUCCEEDED': - completed_build = build - break - - if completed_build is None: - # If no succeeded build found, use any build - completed_build = builds_page.items[0] - - # Get the build log - log_client = client.build(completed_build.id).log() + log_client = client.build(build_id_of_own_actor).log() log_content = await maybe_await(log_client.get()) # Build logs should be available for completed builds diff --git a/tests/integration/test_log.py b/tests/integration/test_log.py index 150292af..bf6799e0 100644 --- a/tests/integration/test_log.py +++ b/tests/integration/test_log.py @@ -9,7 +9,7 @@ from typing import TYPE_CHECKING from .._utils import maybe_await -from apify_client._models import ListOfBuilds, Run +from apify_client._models import Run from apify_client._resource_clients import RunClient, RunClientAsync from apify_client.http_clients import HttpResponse @@ -43,22 +43,13 @@ async def test_log_get_from_run(client: ApifyClient | ApifyClientAsync) -> None: await maybe_await(run_client.delete()) -async def test_log_get_from_build(client: ApifyClient | ApifyClientAsync) -> None: +async def test_log_get_from_build(client: ApifyClient | ApifyClientAsync, build_id_of_own_actor: str) -> None: """Test retrieving log from a build.""" - # Get a build from hello-world actor - actor = client.actor(HELLO_WORLD_ACTOR) - builds_page = await maybe_await(actor.builds().list(limit=1)) - assert isinstance(builds_page, ListOfBuilds) - assert builds_page.items - build_id = builds_page.items[0].id - - # Get log from the build - build = client.build(build_id) + build = client.build(build_id_of_own_actor) log = await maybe_await(build.log().get()) - # Build log may be None or empty for some builds - if log is not None: - assert isinstance(log, str) + assert isinstance(log, str) + assert len(log) > 0 async def test_log_get_as_bytes(client: ApifyClient | ApifyClientAsync) -> None: