Skip to content
Open
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
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,6 +115,46 @@ def test_kvs_of_another_user(api_token_2: str) -> Generator[KvsFixture]:
kvs_client.delete()


@pytest.fixture(scope='session')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: With xdist, a session fixture runs once per worker, so the 3.11 job built 4 Actors (gw6, gw11, gw13, gw14). The job is still faster than the last green master run, so it's fine as is. Just noting why the tail of the run is slower.

✍️ Drafted by Claude Code

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'),
Expand Down
22 changes: 2 additions & 20 deletions tests/integration/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This test now does the same thing as test_log_get_from_build, but asserts only is not None. Could it use the same isinstance(log_content, str) and len(log_content) > 0 checks, or could we drop one of the two tests?

✍️ Drafted by Claude Code

"""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
Expand Down
19 changes: 5 additions & 14 deletions tests/integration/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
Loading