Skip to content

Commit 7f4dcad

Browse files
authored
Integration tests for API helpers & environment helpers (#44)
This adds integration tests for: - `Actor.is_at_home` - `Actor.get_env` - `Actor.new_client` - `Actor.start` - `Actor.call` - `Actor.call_task` - `Actor.abort` - `Actor.metamorph` - `Actor.reboot` - `Actor.add_webhook` - `Actor.set_status_message`
1 parent 72d92ea commit 7f4dcad

File tree

7 files changed

+438
-10
lines changed

7 files changed

+438
-10
lines changed

docs/docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ Return True when the actor is running on the Apify platform, and False otherwise
352352
Return a dictionary with information parsed from all the APIFY_XXX environment variables.
353353

354354
For a list of all the environment variables,
355-
see the [Actor documentation]([https://docs.apify.com/actor/run#environment-variables](https://docs.apify.com/actor/run#environment-variables)).
355+
see the [Actor documentation]([https://docs.apify.com/actors/development/environment-variables](https://docs.apify.com/actors/development/environment-variables)).
356356
If some variables are not defined or are invalid, the corresponding value in the resulting dictionary will be None.
357357

358358
* **Return type**

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[pytest]
22
asyncio_mode=auto
3+
timeout = 1200

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
'pre-commit ~= 2.20.0',
7878
'pytest ~= 7.2.0',
7979
'pytest-asyncio ~= 0.20.3',
80+
'pytest-only ~= 2.0.0',
8081
'pytest-randomly ~= 3.12.0',
82+
'pytest-timeout ~= 2.1.0',
8183
'pytest-xdist ~= 3.1.0',
8284
'respx ~= 0.20.1',
8385
'sphinx ~= 5.3.0',

src/apify/actor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ def get_env(cls) -> Dict:
698698
"""Return a dictionary with information parsed from all the `APIFY_XXX` environment variables.
699699
700700
For a list of all the environment variables,
701-
see the [Actor documentation](https://docs.apify.com/actor/run#environment-variables).
701+
see the [Actor documentation](https://docs.apify.com/actors/development/environment-variables).
702702
If some variables are not defined or are invalid, the corresponding value in the resulting dictionary will be None.
703703
"""
704704
return cls._get_default_instance().get_env()

tests/integration/README.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ This fixture returns a factory function for creating actors on the Apify Platfor
3030
For the actor source, the fixture takes the files from `tests/integration/actor_source_base`,
3131
builds the Apify SDK wheel from the current codebase,
3232
and adds the actor source you passed to the fixture as an argument.
33+
You have to pass exactly one of the `main_func`, `main_py` and `source_files` arguments.
3334

3435
The created actor will be uploaded to the platform, built there, and after the test finishes, it will be automatically deleted.
3536
If the actor build fails, it will not be deleted, so that you can check why the build failed.
3637

37-
You can create actors straight from a Python function:
38+
### Creating test actor straight from a Python function
39+
40+
You can create actors straight from a Python function.
41+
This is great because you can have the test actor source code checked with the linter.
3842

3943
```python
4044
async def test_something(self, make_actor: ActorFactory) -> None:
@@ -50,8 +54,34 @@ async def test_something(self, make_actor: ActorFactory) -> None:
5054
assert run_result['status'] == 'SUCCEEDED'
5155
```
5256

53-
Or you can pass the `src/main.py` file, if you need something more complex
54-
(e.g. specify more imports or pass some fixed value to the actor source code):
57+
These actors will have the `src/main.py` file set to the `main` function definition,
58+
prepended with `import asyncio` and `from apify import Actor`, for your convenience.
59+
60+
You can also pass extra imports directly to the main function:
61+
62+
```python
63+
async def test_something(self, make_actor: ActorFactory) -> None:
64+
async def main():
65+
import os
66+
from apify.consts import ActorEventType, ApifyEnvVars
67+
async with Actor:
68+
print('The actor is running with ' + os.getenv(ApifyEnvVars.MEMORY_MBYTES) + 'MB of memory')
69+
await Actor.on(ActorEventType.SYSTEM_INFO, lambda event_data: print(event_data))
70+
71+
actor = await make_actor('something', main_func=main)
72+
73+
run_result = await actor.call()
74+
75+
assert run_result is not None
76+
assert run_result['status'] == 'SUCCEEDED'
77+
```
78+
79+
### Creating actor from source files
80+
81+
You can also pass the source files directly if you need something more complex
82+
(e.g. pass some fixed value to the actor source code or use multiple source files).
83+
84+
To pass the source code of the `src/main.py` file directly, use the `main_py` argument to `make_actor`:
5585

5686
```python
5787
async def test_something(self, make_actor: ActorFactory) -> None:
@@ -76,7 +106,8 @@ async def test_something(self, make_actor: ActorFactory) -> None:
76106

77107
```
78108

79-
Or you can pass multiple source files, if you need something really complex:
109+
Or you can pass multiple source files with the `source_files` argument,
110+
if you need something really complex:
80111

81112
```python
82113
async def test_something(self, make_actor: ActorFactory) -> None:
@@ -104,5 +135,3 @@ async def test_something(self, make_actor: ActorFactory) -> None:
104135
assert actor_run is not None
105136
assert actor_run['status'] == 'SUCCEEDED'
106137
```
107-
108-
You have to pass exactly one of the `main_func`, `main_py` and `source_files` arguments.

0 commit comments

Comments
 (0)