Skip to content

Commit

Permalink
Integration tests for API helpers & environment helpers (#44)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
fnesveda authored Feb 2, 2023
1 parent 72d92ea commit 7f4dcad
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ Return True when the actor is running on the Apify platform, and False otherwise
Return a dictionary with information parsed from all the APIFY_XXX environment variables.

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

* **Return type**
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[pytest]
asyncio_mode=auto
timeout = 1200
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
'pre-commit ~= 2.20.0',
'pytest ~= 7.2.0',
'pytest-asyncio ~= 0.20.3',
'pytest-only ~= 2.0.0',
'pytest-randomly ~= 3.12.0',
'pytest-timeout ~= 2.1.0',
'pytest-xdist ~= 3.1.0',
'respx ~= 0.20.1',
'sphinx ~= 5.3.0',
Expand Down
2 changes: 1 addition & 1 deletion src/apify/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ def get_env(cls) -> Dict:
"""Return a dictionary with information parsed from all the `APIFY_XXX` environment variables.
For a list of all the environment variables,
see the [Actor documentation](https://docs.apify.com/actor/run#environment-variables).
see the [Actor documentation](https://docs.apify.com/actors/development/environment-variables).
If some variables are not defined or are invalid, the corresponding value in the resulting dictionary will be None.
"""
return cls._get_default_instance().get_env()
Expand Down
41 changes: 35 additions & 6 deletions tests/integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ This fixture returns a factory function for creating actors on the Apify Platfor
For the actor source, the fixture takes the files from `tests/integration/actor_source_base`,
builds the Apify SDK wheel from the current codebase,
and adds the actor source you passed to the fixture as an argument.
You have to pass exactly one of the `main_func`, `main_py` and `source_files` arguments.

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

You can create actors straight from a Python function:
### Creating test actor straight from a Python function

You can create actors straight from a Python function.
This is great because you can have the test actor source code checked with the linter.

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

Or you can pass the `src/main.py` file, if you need something more complex
(e.g. specify more imports or pass some fixed value to the actor source code):
These actors will have the `src/main.py` file set to the `main` function definition,
prepended with `import asyncio` and `from apify import Actor`, for your convenience.

You can also pass extra imports directly to the main function:

```python
async def test_something(self, make_actor: ActorFactory) -> None:
async def main():
import os
from apify.consts import ActorEventType, ApifyEnvVars
async with Actor:
print('The actor is running with ' + os.getenv(ApifyEnvVars.MEMORY_MBYTES) + 'MB of memory')
await Actor.on(ActorEventType.SYSTEM_INFO, lambda event_data: print(event_data))

actor = await make_actor('something', main_func=main)

run_result = await actor.call()

assert run_result is not None
assert run_result['status'] == 'SUCCEEDED'
```

### Creating actor from source files

You can also pass the source files directly if you need something more complex
(e.g. pass some fixed value to the actor source code or use multiple source files).

To pass the source code of the `src/main.py` file directly, use the `main_py` argument to `make_actor`:

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

```

Or you can pass multiple source files, if you need something really complex:
Or you can pass multiple source files with the `source_files` argument,
if you need something really complex:

```python
async def test_something(self, make_actor: ActorFactory) -> None:
Expand Down Expand Up @@ -104,5 +135,3 @@ async def test_something(self, make_actor: ActorFactory) -> None:
assert actor_run is not None
assert actor_run['status'] == 'SUCCEEDED'
```

You have to pass exactly one of the `main_func`, `main_py` and `source_files` arguments.
Loading

0 comments on commit 7f4dcad

Please sign in to comment.