Skip to content

Commit 4935867

Browse files
committed
Add moooore tests
1 parent 193c1a7 commit 4935867

File tree

9 files changed

+673
-1
lines changed

9 files changed

+673
-1
lines changed

src/apify_client/_resource_clients/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def update(
8484
}
8585

8686
response = self._update(filter_out_none_values_recursively(updated_fields))
87-
return Run.model_validate(response)
87+
return GetRunResponse.model_validate(response).data
8888

8989
def delete(self) -> None:
9090
"""Delete the run.

tests/integration/test_actor.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING
44

5+
from .utils import get_random_resource_name
6+
57
if TYPE_CHECKING:
68
from apify_client import ApifyClient
79

@@ -55,3 +57,59 @@ def test_list_actors_sorting(apify_client: ApifyClient) -> None:
5557
assert actors_page is not None
5658
assert actors_page.items is not None
5759
assert isinstance(actors_page.items, list)
60+
61+
62+
def test_actor_create_update_delete(apify_client: ApifyClient) -> None:
63+
"""Test creating, updating, and deleting an actor."""
64+
actor_name = get_random_resource_name('actor')
65+
66+
# Create actor
67+
created_actor = apify_client.actors().create(
68+
name=actor_name,
69+
title='Test Actor',
70+
description='Test actor for integration tests',
71+
versions=[
72+
{
73+
'versionNumber': '0.1',
74+
'sourceType': 'SOURCE_FILES',
75+
'buildTag': 'latest',
76+
'sourceFiles': [
77+
{
78+
'name': 'main.js',
79+
'format': 'TEXT',
80+
'content': 'console.log("Hello")',
81+
}
82+
],
83+
}
84+
],
85+
)
86+
assert created_actor is not None
87+
assert created_actor.id is not None
88+
assert created_actor.name == actor_name
89+
90+
actor_client = apify_client.actor(created_actor.id)
91+
92+
try:
93+
# Update actor (only title and description - updating defaultRunOptions requires build to be set)
94+
new_title = 'Updated Test Actor'
95+
new_description = 'Updated description'
96+
updated_actor = actor_client.update(
97+
title=new_title,
98+
description=new_description,
99+
)
100+
assert updated_actor is not None
101+
assert updated_actor.title == new_title
102+
assert updated_actor.description == new_description
103+
104+
# Verify update persisted
105+
retrieved_actor = actor_client.get()
106+
assert retrieved_actor is not None
107+
assert retrieved_actor.title == new_title
108+
109+
finally:
110+
# Cleanup - delete actor
111+
actor_client.delete()
112+
113+
# Verify deletion
114+
deleted_actor = actor_client.get()
115+
assert deleted_actor is None

tests/integration/test_actor_async.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING
44

5+
from .utils import get_random_resource_name
6+
57
if TYPE_CHECKING:
68
from apify_client import ApifyClientAsync
79

@@ -55,3 +57,59 @@ async def test_list_actors_sorting(apify_client_async: ApifyClientAsync) -> None
5557
assert actors_page is not None
5658
assert actors_page.items is not None
5759
assert isinstance(actors_page.items, list)
60+
61+
62+
async def test_actor_create_update_delete(apify_client_async: ApifyClientAsync) -> None:
63+
"""Test creating, updating, and deleting an actor."""
64+
actor_name = get_random_resource_name('actor')
65+
66+
# Create actor
67+
created_actor = await apify_client_async.actors().create(
68+
name=actor_name,
69+
title='Test Actor',
70+
description='Test actor for integration tests',
71+
versions=[
72+
{
73+
'versionNumber': '0.1',
74+
'sourceType': 'SOURCE_FILES',
75+
'buildTag': 'latest',
76+
'sourceFiles': [
77+
{
78+
'name': 'main.js',
79+
'format': 'TEXT',
80+
'content': 'console.log("Hello")',
81+
}
82+
],
83+
}
84+
],
85+
)
86+
assert created_actor is not None
87+
assert created_actor.id is not None
88+
assert created_actor.name == actor_name
89+
90+
actor_client = apify_client_async.actor(created_actor.id)
91+
92+
try:
93+
# Update actor (only title and description - updating defaultRunOptions requires build to be set)
94+
new_title = 'Updated Test Actor'
95+
new_description = 'Updated description'
96+
updated_actor = await actor_client.update(
97+
title=new_title,
98+
description=new_description,
99+
)
100+
assert updated_actor is not None
101+
assert updated_actor.title == new_title
102+
assert updated_actor.description == new_description
103+
104+
# Verify update persisted
105+
retrieved_actor = await actor_client.get()
106+
assert retrieved_actor is not None
107+
assert retrieved_actor.title == new_title
108+
109+
finally:
110+
# Cleanup - delete actor
111+
await actor_client.delete()
112+
113+
# Verify deletion
114+
deleted_actor = await actor_client.get()
115+
assert deleted_actor is None

tests/integration/test_build.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING
44

5+
from .utils import get_random_resource_name
6+
57
if TYPE_CHECKING:
68
from apify_client import ApifyClient
79

@@ -108,3 +110,92 @@ def test_build_wait_for_finish(apify_client: ApifyClient) -> None:
108110

109111
assert build is not None
110112
assert build.id == completed_build.id
113+
114+
115+
def test_build_delete_and_abort(apify_client: ApifyClient) -> None:
116+
"""Test deleting and aborting a build on our own actor."""
117+
actor_name = get_random_resource_name('actor')
118+
119+
# Create actor with two versions
120+
created_actor = apify_client.actors().create(
121+
name=actor_name,
122+
title='Test Actor for Build Delete',
123+
versions=[
124+
{
125+
'versionNumber': '0.1',
126+
'sourceType': 'SOURCE_FILES',
127+
'buildTag': 'beta',
128+
'sourceFiles': [
129+
{
130+
'name': 'main.js',
131+
'format': 'TEXT',
132+
'content': 'console.log("Hello v0.1")',
133+
}
134+
],
135+
},
136+
{
137+
'versionNumber': '0.2',
138+
'sourceType': 'SOURCE_FILES',
139+
'buildTag': 'latest',
140+
'sourceFiles': [
141+
{
142+
'name': 'main.js',
143+
'format': 'TEXT',
144+
'content': 'console.log("Hello v0.2")',
145+
}
146+
],
147+
},
148+
],
149+
)
150+
assert created_actor is not None
151+
actor_client = apify_client.actor(created_actor.id)
152+
153+
try:
154+
# Build both versions - we need 2 builds because we can't delete the default build
155+
first_build = actor_client.build(version_number='0.1')
156+
assert first_build is not None
157+
first_build_client = apify_client.build(first_build.id)
158+
first_build_client.wait_for_finish()
159+
160+
second_build = actor_client.build(version_number='0.2')
161+
assert second_build is not None
162+
second_build_client = apify_client.build(second_build.id)
163+
164+
# Wait for the second build to finish
165+
finished_build = second_build_client.wait_for_finish()
166+
assert finished_build is not None
167+
assert finished_build.status.value in ('SUCCEEDED', 'FAILED')
168+
169+
# Test abort on already finished build (should return the build in its current state)
170+
aborted_build = second_build_client.abort()
171+
assert aborted_build is not None
172+
assert aborted_build.status.value in ('SUCCEEDED', 'FAILED', 'ABORTED')
173+
174+
# Delete the first build (not the default/latest)
175+
first_build_client.delete()
176+
177+
# Verify the build is deleted
178+
deleted_build = first_build_client.get()
179+
assert deleted_build is None
180+
181+
finally:
182+
# Cleanup - delete actor
183+
actor_client.delete()
184+
185+
186+
def test_build_get_open_api_definition(apify_client: ApifyClient) -> None:
187+
"""Test getting OpenAPI definition for a build."""
188+
# Get builds for hello-world actor
189+
actor = apify_client.actor(HELLO_WORLD_ACTOR)
190+
builds_page = actor.builds().list(limit=1)
191+
assert builds_page.items
192+
build_id = builds_page.items[0].id
193+
194+
# Get the OpenAPI definition
195+
build_client = apify_client.build(build_id)
196+
openapi_def = build_client.get_open_api_definition()
197+
198+
# OpenAPI definition should be a dict with standard OpenAPI fields
199+
# Note: May be None if the actor doesn't have an OpenAPI definition
200+
if openapi_def is not None:
201+
assert isinstance(openapi_def, dict)

tests/integration/test_build_async.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
from .utils import get_random_resource_name
8+
79
if TYPE_CHECKING:
810
from apify_client import ApifyClientAsync
911

@@ -113,3 +115,92 @@ async def test_build_wait_for_finish(apify_client_async: ApifyClientAsync) -> No
113115

114116
assert build is not None
115117
assert build.id == completed_build.id
118+
119+
120+
async def test_build_delete_and_abort(apify_client_async: ApifyClientAsync) -> None:
121+
"""Test deleting and aborting a build on our own actor."""
122+
actor_name = get_random_resource_name('actor')
123+
124+
# Create actor with two versions
125+
created_actor = await apify_client_async.actors().create(
126+
name=actor_name,
127+
title='Test Actor for Build Delete',
128+
versions=[
129+
{
130+
'versionNumber': '0.1',
131+
'sourceType': 'SOURCE_FILES',
132+
'buildTag': 'beta',
133+
'sourceFiles': [
134+
{
135+
'name': 'main.js',
136+
'format': 'TEXT',
137+
'content': 'console.log("Hello v0.1")',
138+
}
139+
],
140+
},
141+
{
142+
'versionNumber': '0.2',
143+
'sourceType': 'SOURCE_FILES',
144+
'buildTag': 'latest',
145+
'sourceFiles': [
146+
{
147+
'name': 'main.js',
148+
'format': 'TEXT',
149+
'content': 'console.log("Hello v0.2")',
150+
}
151+
],
152+
},
153+
],
154+
)
155+
assert created_actor is not None
156+
actor_client = apify_client_async.actor(created_actor.id)
157+
158+
try:
159+
# Build both versions - we need 2 builds because we can't delete the default build
160+
first_build = await actor_client.build(version_number='0.1')
161+
assert first_build is not None
162+
first_build_client = apify_client_async.build(first_build.id)
163+
await first_build_client.wait_for_finish()
164+
165+
second_build = await actor_client.build(version_number='0.2')
166+
assert second_build is not None
167+
second_build_client = apify_client_async.build(second_build.id)
168+
169+
# Wait for the second build to finish
170+
finished_build = await second_build_client.wait_for_finish()
171+
assert finished_build is not None
172+
assert finished_build.status.value in ('SUCCEEDED', 'FAILED')
173+
174+
# Test abort on already finished build (should return the build in its current state)
175+
aborted_build = await second_build_client.abort()
176+
assert aborted_build is not None
177+
assert aborted_build.status.value in ('SUCCEEDED', 'FAILED', 'ABORTED')
178+
179+
# Delete the first build (not the default/latest)
180+
await first_build_client.delete()
181+
182+
# Verify the build is deleted
183+
deleted_build = await first_build_client.get()
184+
assert deleted_build is None
185+
186+
finally:
187+
# Cleanup - delete actor
188+
await actor_client.delete()
189+
190+
191+
async def test_build_get_open_api_definition(apify_client_async: ApifyClientAsync) -> None:
192+
"""Test getting OpenAPI definition for a build."""
193+
# Get builds for hello-world actor
194+
actor = apify_client_async.actor(HELLO_WORLD_ACTOR)
195+
builds_page = await actor.builds().list(limit=1)
196+
assert builds_page.items
197+
build_id = builds_page.items[0].id
198+
199+
# Get the OpenAPI definition
200+
build_client = apify_client_async.build(build_id)
201+
openapi_def = await build_client.get_open_api_definition()
202+
203+
# OpenAPI definition should be a dict with standard OpenAPI fields
204+
# Note: May be None if the actor doesn't have an OpenAPI definition
205+
if openapi_def is not None:
206+
assert isinstance(openapi_def, dict)

0 commit comments

Comments
 (0)