Skip to content

Commit 5a60822

Browse files
committed
Update unit tests
1 parent 6ab9aa0 commit 5a60822

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

tests/unit/actor/test_actor_env_helpers.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import random
44
import string
55
from datetime import datetime, timedelta
6+
from decimal import Decimal
67
from typing import TYPE_CHECKING, Any
78

89
from pydantic_core import TzInfo
@@ -30,15 +31,7 @@ async def test_actor_is_not_at_home_when_local() -> None:
3031
assert is_at_home is False
3132

3233

33-
async def test_actor_is_at_home_on_apify(monkeypatch: pytest.MonkeyPatch) -> None:
34-
print('setenv')
35-
monkeypatch.setenv(ApifyEnvVars.IS_AT_HOME, 'true')
36-
async with Actor as actor:
37-
is_at_home = actor.is_at_home()
38-
assert is_at_home is True
39-
40-
41-
async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch) -> None:
34+
async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch) -> None: # noqa: PLR0912
4235
ignored_env_vars = {
4336
ApifyEnvVars.INPUT_KEY,
4437
ApifyEnvVars.MEMORY_MBYTES,
@@ -82,7 +75,10 @@ async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch)
8275
continue
8376

8477
float_get_env_var = float_env_var.name.lower()
85-
expected_get_env[float_get_env_var] = random.random()
78+
if float_env_var == ActorEnvVars.MAX_TOTAL_CHARGE_USD:
79+
expected_get_env[float_get_env_var] = Decimal(random.random())
80+
else:
81+
expected_get_env[float_get_env_var] = random.random()
8682
monkeypatch.setenv(float_env_var, f'{expected_get_env[float_get_env_var]}')
8783

8884
for bool_env_var in BOOL_ENV_VARS:
@@ -93,6 +89,9 @@ async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch)
9389
expected_get_env[bool_get_env_var] = random.choice([True, False])
9490
monkeypatch.setenv(bool_env_var, f'{"true" if expected_get_env[bool_get_env_var] else "false"}')
9591

92+
expected_get_env[ApifyEnvVars.IS_AT_HOME.name.lower()] = False
93+
monkeypatch.setenv(ApifyEnvVars.IS_AT_HOME, 'false')
94+
9695
for datetime_env_var in DATETIME_ENV_VARS:
9796
if datetime_env_var in ignored_env_vars:
9897
continue

tests/unit/actor/test_actor_log.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414
import pytest
1515

1616

17-
async def test_actor_logs_messages_correctly(
18-
caplog: pytest.LogCaptureFixture,
19-
monkeypatch: pytest.MonkeyPatch,
20-
) -> None:
17+
async def test_actor_logs_messages_correctly(caplog: pytest.LogCaptureFixture) -> None:
2118
caplog.set_level(logging.DEBUG, logger='apify')
22-
monkeypatch.setenv('APIFY_IS_AT_HOME', '1')
2319

2420
with contextlib.suppress(RuntimeError):
2521
async with Actor(configure_logging=False):
@@ -43,7 +39,7 @@ async def test_actor_logs_messages_correctly(
4339
# Test that exception in Actor.main is logged with the traceback
4440
raise RuntimeError('Dummy RuntimeError')
4541

46-
assert len(caplog.records) == 12
42+
assert len(caplog.records) == 13
4743

4844
assert caplog.records[0].levelno == logging.INFO
4945
assert caplog.records[0].message == 'Initializing Actor...'
@@ -56,39 +52,42 @@ async def test_actor_logs_messages_correctly(
5652
assert getattr(caplog.records[1], 'os', None) == sys.platform
5753

5854
assert caplog.records[2].levelno == logging.DEBUG
59-
assert caplog.records[2].message.startswith('APIFY_ACTOR_EVENTS_WS_URL env var not set')
55+
assert caplog.records[2].message == 'Event manager initialized'
6056

6157
assert caplog.records[3].levelno == logging.DEBUG
62-
assert caplog.records[3].message == 'Debug message'
58+
assert caplog.records[3].message == 'Charging manager initialized'
6359

64-
assert caplog.records[4].levelno == logging.INFO
65-
assert caplog.records[4].message == 'Info message'
60+
assert caplog.records[4].levelno == logging.DEBUG
61+
assert caplog.records[4].message == 'Debug message'
6662

67-
assert caplog.records[5].levelno == logging.WARNING
68-
assert caplog.records[5].message == 'Warning message'
63+
assert caplog.records[5].levelno == logging.INFO
64+
assert caplog.records[5].message == 'Info message'
6965

70-
assert caplog.records[6].levelno == logging.ERROR
71-
assert caplog.records[6].message == 'Error message'
66+
assert caplog.records[6].levelno == logging.WARNING
67+
assert caplog.records[6].message == 'Warning message'
7268

7369
assert caplog.records[7].levelno == logging.ERROR
74-
assert caplog.records[7].message == 'Exception message'
75-
assert caplog.records[7].exc_info is not None
76-
assert caplog.records[7].exc_info[0] is ValueError
77-
assert isinstance(caplog.records[7].exc_info[1], ValueError)
78-
assert str(caplog.records[7].exc_info[1]) == 'Dummy ValueError'
79-
80-
assert caplog.records[8].levelno == logging.INFO
81-
assert caplog.records[8].message == 'Multi\nline\nlog\nmessage'
82-
83-
assert caplog.records[9].levelno == logging.ERROR
84-
assert caplog.records[9].message == 'Actor failed with an exception'
85-
assert caplog.records[9].exc_info is not None
86-
assert caplog.records[9].exc_info[0] is RuntimeError
87-
assert isinstance(caplog.records[9].exc_info[1], RuntimeError)
88-
assert str(caplog.records[9].exc_info[1]) == 'Dummy RuntimeError'
89-
90-
assert caplog.records[10].levelno == logging.INFO
91-
assert caplog.records[10].message == 'Exiting Actor'
92-
93-
assert caplog.records[11].levelno == logging.DEBUG
94-
assert caplog.records[11].message == 'Not calling sys.exit(91) because Actor is running in an unit test'
70+
assert caplog.records[7].message == 'Error message'
71+
72+
assert caplog.records[8].levelno == logging.ERROR
73+
assert caplog.records[8].message == 'Exception message'
74+
assert caplog.records[8].exc_info is not None
75+
assert caplog.records[8].exc_info[0] is ValueError
76+
assert isinstance(caplog.records[8].exc_info[1], ValueError)
77+
assert str(caplog.records[8].exc_info[1]) == 'Dummy ValueError'
78+
79+
assert caplog.records[9].levelno == logging.INFO
80+
assert caplog.records[9].message == 'Multi\nline\nlog\nmessage'
81+
82+
assert caplog.records[10].levelno == logging.ERROR
83+
assert caplog.records[10].message == 'Actor failed with an exception'
84+
assert caplog.records[10].exc_info is not None
85+
assert caplog.records[10].exc_info[0] is RuntimeError
86+
assert isinstance(caplog.records[10].exc_info[1], RuntimeError)
87+
assert str(caplog.records[10].exc_info[1]) == 'Dummy RuntimeError'
88+
89+
assert caplog.records[11].levelno == logging.INFO
90+
assert caplog.records[11].message == 'Exiting Actor'
91+
92+
assert caplog.records[12].levelno == logging.DEBUG
93+
assert caplog.records[12].message == 'Not calling sys.exit(91) because Actor is running in an unit test'

0 commit comments

Comments
 (0)