Skip to content
Merged
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
77 changes: 74 additions & 3 deletions tests/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import asyncio
import inspect
import os
import secrets
import string
import time
from collections.abc import AsyncIterator, Iterator
from collections.abc import AsyncGenerator, AsyncIterator, Iterator
from contextlib import asynccontextmanager, contextmanager
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, overload
from datetime import timedelta
from typing import TYPE_CHECKING, Any, Literal, Protocol, TypeVar, cast, overload

import pytest

from apify_client import ApifyClient, ApifyClientAsync
from apify_client._consts import DEFAULT_API_URL

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable
from collections.abc import Awaitable, Callable, Generator

from apify_client.http_clients import HttpClient, HttpClientAsync

Expand All @@ -23,6 +29,8 @@

T = TypeVar('T')

ClientType = Literal['sync', 'async']


class _HasId(Protocol):
"""Items returned by collection `iterate()` endpoints all expose `.id`."""
Expand Down Expand Up @@ -65,6 +73,69 @@ class HttpClientClasses:
async_: type[HttpClientAsync]


def get_api_url() -> str:
"""Return the API URL for integration tests, from `API_URL_ENV_VAR` or the default."""
return os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL


@asynccontextmanager
async def open_apify_client(
token: str, client_type: ClientType, http_client_classes: HttpClientClasses
) -> AsyncGenerator[ApifyClient | ApifyClientAsync]:
"""Build a sync or async client with the given token and HTTP client implementation, closing it on exit."""
api_url = get_api_url()
if client_type == 'sync':
http_client = http_client_classes.sync()
try:
yield ApifyClient.with_custom_http_client(
token,
api_url=api_url,
http_client=http_client,
)
finally:
http_client.close()
return

http_client_async = http_client_classes.async_()
try:
yield ApifyClientAsync.with_custom_http_client(
token,
api_url=api_url,
http_client=http_client_async,
)
finally:
await http_client_async.aclose()


@contextmanager
def create_built_actor(
client: ApifyClient, name: str, source_files: list[dict[str, Any]], **create_kwargs: Any
) -> Generator[str]:
"""Create an Actor from source files and build it, yield its ID, and delete it on exit."""
actor = client.actors().create(
name=name,
versions=[
{
'versionNumber': '0.0',
'sourceType': 'SOURCE_FILES',
'buildTag': 'latest',
'sourceFiles': source_files,
}
],
**create_kwargs,
)
actor_client = client.actor(actor.id)

try:
build = actor_client.build(version_number='0.0')
finished_build = client.build(build.id).wait_for_finish(wait_duration=timedelta(minutes=5))
assert finished_build is not None
assert finished_build.status == 'SUCCEEDED'
yield actor.id
finally:
actor_client.delete()


def get_crypto_random_object_id(length: int = 17) -> str:
"""Generate a cryptographically secure random object ID."""
chars = 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789'
Expand Down
35 changes: 10 additions & 25 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import pytest

from .._utils import (
API_URL_ENV_VAR,
TOKEN_ENV_VAR,
TOKEN_ENV_VAR_2,
ClientType,
DatasetFixture,
HttpClientClasses,
KvsFixture,
get_api_url,
get_crypto_random_object_id,
open_apify_client,
)
from apify_client import ApifyClient, ApifyClientAsync
from apify_client._consts import DEFAULT_API_URL
from apify_client._utils.crypto import create_hmac_signature, create_storage_content_signature
from apify_client.http_clients import (
Httpx2HttpClient,
Expand Down Expand Up @@ -50,7 +51,7 @@ def api_token_2() -> str:
@pytest.fixture(scope='session')
def test_dataset_of_another_user(api_token_2: str) -> Generator[DatasetFixture]:
"""Dataset owned by secondary user for testing cross-user access restrictions."""
api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL
api_url = get_api_url()
client = ApifyClient(api_token_2, api_url=api_url)

# Create dataset with test data
Expand Down Expand Up @@ -79,7 +80,7 @@ def test_dataset_of_another_user(api_token_2: str) -> Generator[DatasetFixture]:
@pytest.fixture(scope='session')
def test_kvs_of_another_user(api_token_2: str) -> Generator[KvsFixture]:
"""Key-value store owned by secondary user for testing cross-user access restrictions."""
api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL
api_url = get_api_url()
client = ApifyClient(api_token_2, api_url=api_url)

# Create key-value store with test data
Expand Down Expand Up @@ -120,39 +121,23 @@ def http_client_classes(request: pytest.FixtureRequest) -> HttpClientClasses:


@pytest.fixture(params=['sync', 'async'])
def client_type(request: pytest.FixtureRequest) -> str:
def client_type(request: pytest.FixtureRequest) -> ClientType:
"""Parametrize tests to run with both sync and async clients."""
return request.param


@pytest.fixture
async def client(
client_type: str,
client_type: ClientType,
api_token: str,
http_client_classes: HttpClientClasses,
) -> AsyncGenerator[ApifyClient | ApifyClientAsync]:
"""Return each sync/async and HTTP client implementation combination."""
api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL
if client_type == 'sync':
http_client = http_client_classes.sync()
yield ApifyClient.with_custom_http_client(
api_token,
api_url=api_url,
http_client=http_client,
)
http_client.close()
return

http_client_async = http_client_classes.async_()
yield ApifyClientAsync.with_custom_http_client(
api_token,
api_url=api_url,
http_client=http_client_async,
)
await http_client_async.aclose()
async with open_apify_client(api_token, client_type, http_client_classes) as client:
yield client


@pytest.fixture
def is_async(client_type: str) -> bool:
def is_async(client_type: ClientType) -> bool:
"""True if current test is using async client."""
return client_type == 'async'
44 changes: 14 additions & 30 deletions tests/integration/test_actor_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
from __future__ import annotations

import json
import os
from datetime import timedelta
from typing import TYPE_CHECKING

import pytest

from .._utils import API_URL_ENV_VAR, get_random_resource_name, maybe_await
from .._utils import create_built_actor, get_api_url, get_random_resource_name, maybe_await
from apify_client import ApifyClient
from apify_client._consts import DEFAULT_API_URL

if TYPE_CHECKING:
from collections.abc import Generator
Expand Down Expand Up @@ -41,7 +39,7 @@
"""

DOCKERFILE = """
FROM node:22-alpine
FROM node:26-alpine
COPY main.js ./
CMD ["node", "main.js"]
"""
Expand All @@ -50,7 +48,7 @@
@pytest.fixture(scope='module')
def server_actor_id(api_token: str) -> Generator[str]:
"""Create and build a Server Actor running an echo server, and delete it after the module's tests."""
client = ApifyClient(api_token, api_url=os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL)
client = ApifyClient(api_token, api_url=get_api_url())
actor_name = get_random_resource_name('server')
actor_json = {
'actorSpecification': 1,
Expand All @@ -59,34 +57,20 @@ def server_actor_id(api_token: str) -> Generator[str]:
'usesStandbyMode': True,
'webServerSchema': OPENAPI_SPEC,
}
actor = client.actors().create(
name=actor_name,
versions=[
{
'versionNumber': '0.0',
'sourceType': 'SOURCE_FILES',
'buildTag': 'latest',
'sourceFiles': [
{'name': 'Dockerfile', 'format': 'TEXT', 'content': DOCKERFILE},
{'name': 'main.js', 'format': 'TEXT', 'content': ECHO_SERVER_SOURCE},
{'name': '.actor/actor.json', 'format': 'TEXT', 'content': json.dumps(actor_json)},
],
}
],
source_files = [
{'name': 'Dockerfile', 'format': 'TEXT', 'content': DOCKERFILE},
{'name': 'main.js', 'format': 'TEXT', 'content': ECHO_SERVER_SOURCE},
{'name': '.actor/actor.json', 'format': 'TEXT', 'content': json.dumps(actor_json)},
]
with create_built_actor(
client,
actor_name,
source_files,
server_is_enabled=True,
server_memory_mbytes=256,
server_idle_timeout=timedelta(minutes=1),
)
actor_client = client.actor(actor.id)

try:
build = actor_client.build(version_number='0.0')
finished_build = client.build(build.id).wait_for_finish(wait_duration=timedelta(minutes=5))
assert finished_build is not None
assert finished_build.status == 'SUCCEEDED'
yield actor.id
finally:
actor_client.delete()
) as actor_id:
yield actor_id


async def test_actor_server(client: ApifyClient | ApifyClientAsync, server_actor_id: str) -> None:
Expand Down
9 changes: 3 additions & 6 deletions tests/integration/test_apify_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

from __future__ import annotations

import os

from .._utils import API_URL_ENV_VAR, maybe_await
from .._utils import ClientType, get_api_url, maybe_await
from apify_client import ApifyClient, ApifyClientAsync
from apify_client._consts import DEFAULT_API_URL
from apify_client._models import UserPrivateInfo, UserPublicInfo
from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync

Expand All @@ -19,13 +16,13 @@ async def test_apify_client(client: ApifyClient | ApifyClientAsync) -> None:
assert me.username is not None


async def test_plain_constructor_reaches_the_api(client_type: str, api_token: str) -> None:
async def test_plain_constructor_reaches_the_api(client_type: ClientType, api_token: str) -> None:
"""The plain constructor - the form users write - reaches the API with the HTTP client it builds itself.

The rest of the suite injects a transport through `with_custom_http_client` to run against every built-in
implementation, which leaves the default construction path untested against the live API.
"""
api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL
api_url = get_api_url()

if client_type == 'sync':
sync_client = ApifyClient(api_token, api_url=api_url)
Expand Down
Loading
Loading