|
23 | 23 | import pytest
|
24 | 24 |
|
25 | 25 |
|
26 |
| -class TestIsAtHome: |
27 |
| - async def test_is_at_home_local(self) -> None: |
28 |
| - async with Actor as actor: |
29 |
| - is_at_home = actor.is_at_home() |
30 |
| - assert is_at_home is False |
31 |
| - |
32 |
| - async def test_is_at_home_on_apify(self, monkeypatch: pytest.MonkeyPatch) -> None: |
33 |
| - print('setenv') |
34 |
| - monkeypatch.setenv(ApifyEnvVars.IS_AT_HOME, 'true') |
35 |
| - async with Actor as actor: |
36 |
| - is_at_home = actor.is_at_home() |
37 |
| - assert is_at_home is True |
38 |
| - |
39 |
| - |
40 |
| -class TestGetEnv: |
41 |
| - async def test_get_env_use_env_vars(self, monkeypatch: pytest.MonkeyPatch) -> None: |
42 |
| - ignored_env_vars = { |
43 |
| - ApifyEnvVars.INPUT_KEY, |
44 |
| - ApifyEnvVars.MEMORY_MBYTES, |
45 |
| - ApifyEnvVars.STARTED_AT, |
46 |
| - ApifyEnvVars.TIMEOUT_AT, |
47 |
| - ApifyEnvVars.DEFAULT_DATASET_ID, |
48 |
| - ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID, |
49 |
| - ApifyEnvVars.DEFAULT_REQUEST_QUEUE_ID, |
50 |
| - ApifyEnvVars.SDK_LATEST_VERSION, |
51 |
| - ApifyEnvVars.LOG_FORMAT, |
52 |
| - ApifyEnvVars.LOG_LEVEL, |
53 |
| - } |
54 |
| - |
55 |
| - legacy_env_vars = { |
56 |
| - ApifyEnvVars.ACT_ID: ActorEnvVars.ID, |
57 |
| - ApifyEnvVars.ACT_RUN_ID: ActorEnvVars.RUN_ID, |
58 |
| - ApifyEnvVars.ACTOR_ID: ActorEnvVars.ID, |
59 |
| - ApifyEnvVars.ACTOR_BUILD_ID: ActorEnvVars.BUILD_ID, |
60 |
| - ApifyEnvVars.ACTOR_BUILD_NUMBER: ActorEnvVars.BUILD_NUMBER, |
61 |
| - ApifyEnvVars.ACTOR_RUN_ID: ActorEnvVars.RUN_ID, |
62 |
| - ApifyEnvVars.ACTOR_TASK_ID: ActorEnvVars.TASK_ID, |
63 |
| - ApifyEnvVars.CONTAINER_URL: ActorEnvVars.WEB_SERVER_URL, |
64 |
| - ApifyEnvVars.CONTAINER_PORT: ActorEnvVars.WEB_SERVER_PORT, |
65 |
| - } |
66 |
| - |
67 |
| - # Set up random env vars |
68 |
| - expected_get_env: dict[str, Any] = {} |
69 |
| - expected_get_env[ApifyEnvVars.LOG_LEVEL.name.lower()] = 'INFO' |
70 |
| - |
71 |
| - for int_env_var in INTEGER_ENV_VARS: |
72 |
| - if int_env_var in ignored_env_vars: |
73 |
| - continue |
74 |
| - |
75 |
| - int_get_env_var = int_env_var.name.lower() |
76 |
| - expected_get_env[int_get_env_var] = random.randint(1, 99999) |
77 |
| - monkeypatch.setenv(int_env_var, f'{expected_get_env[int_get_env_var]}') |
78 |
| - |
79 |
| - for float_env_var in FLOAT_ENV_VARS: |
80 |
| - if float_env_var in ignored_env_vars: |
81 |
| - continue |
82 |
| - |
83 |
| - float_get_env_var = float_env_var.name.lower() |
84 |
| - expected_get_env[float_get_env_var] = random.random() |
85 |
| - monkeypatch.setenv(float_env_var, f'{expected_get_env[float_get_env_var]}') |
86 |
| - |
87 |
| - for bool_env_var in BOOL_ENV_VARS: |
88 |
| - if bool_env_var in ignored_env_vars: |
89 |
| - continue |
90 |
| - |
91 |
| - bool_get_env_var = bool_env_var.name.lower() |
92 |
| - expected_get_env[bool_get_env_var] = random.choice([True, False]) |
93 |
| - monkeypatch.setenv(bool_env_var, f'{"true" if expected_get_env[bool_get_env_var] else "false"}') |
94 |
| - |
95 |
| - for datetime_env_var in DATETIME_ENV_VARS: |
96 |
| - if datetime_env_var in ignored_env_vars: |
97 |
| - continue |
98 |
| - |
99 |
| - datetime_get_env_var = datetime_env_var.name.lower() |
100 |
| - expected_get_env[datetime_get_env_var] = datetime.now(TzInfo(0)) # type: ignore |
101 |
| - monkeypatch.setenv( |
102 |
| - datetime_env_var, |
103 |
| - expected_get_env[datetime_get_env_var].strftime('%Y-%m-%dT%H:%M:%S.%fZ'), |
104 |
| - ) |
105 |
| - |
106 |
| - for string_env_var in STRING_ENV_VARS: |
107 |
| - if string_env_var in ignored_env_vars: |
108 |
| - continue |
109 |
| - |
110 |
| - string_get_env_var = string_env_var.name.lower() |
111 |
| - expected_get_env[string_get_env_var] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) |
112 |
| - monkeypatch.setenv(string_env_var, expected_get_env[string_get_env_var]) |
113 |
| - |
114 |
| - # We need this override so that the actor doesn't fail when connecting to the platform events websocket |
115 |
| - monkeypatch.delenv(ActorEnvVars.EVENTS_WEBSOCKET_URL) |
116 |
| - monkeypatch.delenv(ApifyEnvVars.ACTOR_EVENTS_WS_URL) |
117 |
| - expected_get_env[ActorEnvVars.EVENTS_WEBSOCKET_URL.name.lower()] = None |
118 |
| - expected_get_env[ApifyEnvVars.ACTOR_EVENTS_WS_URL.name.lower()] = None |
119 |
| - |
120 |
| - # Adjust expectations for timedelta fields |
121 |
| - for env_name, env_value in expected_get_env.items(): |
122 |
| - if env_name.endswith('_millis'): |
123 |
| - expected_get_env[env_name] = timedelta(milliseconds=env_value) |
124 |
| - |
125 |
| - # Convert dedicated_cpus to float |
126 |
| - expected_get_env[ApifyEnvVars.DEDICATED_CPUS.name.lower()] = float( |
127 |
| - expected_get_env[ApifyEnvVars.DEDICATED_CPUS.name.lower()] |
| 26 | +async def test_is_at_home_local() -> None: |
| 27 | + async with Actor as actor: |
| 28 | + is_at_home = actor.is_at_home() |
| 29 | + assert is_at_home is False |
| 30 | + |
| 31 | + |
| 32 | +async def test_is_at_home_on_apify(monkeypatch: pytest.MonkeyPatch) -> None: |
| 33 | + print('setenv') |
| 34 | + monkeypatch.setenv(ApifyEnvVars.IS_AT_HOME, 'true') |
| 35 | + async with Actor as actor: |
| 36 | + is_at_home = actor.is_at_home() |
| 37 | + assert is_at_home is True |
| 38 | + |
| 39 | + |
| 40 | +async def test_get_env_use_env_vars(monkeypatch: pytest.MonkeyPatch) -> None: |
| 41 | + ignored_env_vars = { |
| 42 | + ApifyEnvVars.INPUT_KEY, |
| 43 | + ApifyEnvVars.MEMORY_MBYTES, |
| 44 | + ApifyEnvVars.STARTED_AT, |
| 45 | + ApifyEnvVars.TIMEOUT_AT, |
| 46 | + ApifyEnvVars.DEFAULT_DATASET_ID, |
| 47 | + ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID, |
| 48 | + ApifyEnvVars.DEFAULT_REQUEST_QUEUE_ID, |
| 49 | + ApifyEnvVars.SDK_LATEST_VERSION, |
| 50 | + ApifyEnvVars.LOG_FORMAT, |
| 51 | + ApifyEnvVars.LOG_LEVEL, |
| 52 | + ActorEnvVars.STANDBY_PORT, |
| 53 | + } |
| 54 | + |
| 55 | + legacy_env_vars = { |
| 56 | + ApifyEnvVars.ACT_ID: ActorEnvVars.ID, |
| 57 | + ApifyEnvVars.ACT_RUN_ID: ActorEnvVars.RUN_ID, |
| 58 | + ApifyEnvVars.ACTOR_ID: ActorEnvVars.ID, |
| 59 | + ApifyEnvVars.ACTOR_BUILD_ID: ActorEnvVars.BUILD_ID, |
| 60 | + ApifyEnvVars.ACTOR_BUILD_NUMBER: ActorEnvVars.BUILD_NUMBER, |
| 61 | + ApifyEnvVars.ACTOR_RUN_ID: ActorEnvVars.RUN_ID, |
| 62 | + ApifyEnvVars.ACTOR_TASK_ID: ActorEnvVars.TASK_ID, |
| 63 | + ApifyEnvVars.CONTAINER_URL: ActorEnvVars.WEB_SERVER_URL, |
| 64 | + ApifyEnvVars.CONTAINER_PORT: ActorEnvVars.WEB_SERVER_PORT, |
| 65 | + } |
| 66 | + |
| 67 | + # Set up random env vars |
| 68 | + expected_get_env: dict[str, Any] = {} |
| 69 | + expected_get_env[ApifyEnvVars.LOG_LEVEL.name.lower()] = 'INFO' |
| 70 | + |
| 71 | + for int_env_var in INTEGER_ENV_VARS: |
| 72 | + if int_env_var in ignored_env_vars: |
| 73 | + continue |
| 74 | + |
| 75 | + int_get_env_var = int_env_var.name.lower() |
| 76 | + expected_get_env[int_get_env_var] = random.randint(1, 99999) |
| 77 | + monkeypatch.setenv(int_env_var, f'{expected_get_env[int_get_env_var]}') |
| 78 | + |
| 79 | + for float_env_var in FLOAT_ENV_VARS: |
| 80 | + if float_env_var in ignored_env_vars: |
| 81 | + continue |
| 82 | + |
| 83 | + float_get_env_var = float_env_var.name.lower() |
| 84 | + expected_get_env[float_get_env_var] = random.random() |
| 85 | + monkeypatch.setenv(float_env_var, f'{expected_get_env[float_get_env_var]}') |
| 86 | + |
| 87 | + for bool_env_var in BOOL_ENV_VARS: |
| 88 | + if bool_env_var in ignored_env_vars: |
| 89 | + continue |
| 90 | + |
| 91 | + bool_get_env_var = bool_env_var.name.lower() |
| 92 | + expected_get_env[bool_get_env_var] = random.choice([True, False]) |
| 93 | + monkeypatch.setenv(bool_env_var, f'{"true" if expected_get_env[bool_get_env_var] else "false"}') |
| 94 | + |
| 95 | + for datetime_env_var in DATETIME_ENV_VARS: |
| 96 | + if datetime_env_var in ignored_env_vars: |
| 97 | + continue |
| 98 | + |
| 99 | + datetime_get_env_var = datetime_env_var.name.lower() |
| 100 | + expected_get_env[datetime_get_env_var] = datetime.now(TzInfo(0)) # type: ignore |
| 101 | + monkeypatch.setenv( |
| 102 | + datetime_env_var, |
| 103 | + expected_get_env[datetime_get_env_var].strftime('%Y-%m-%dT%H:%M:%S.%fZ'), |
128 | 104 | )
|
129 | 105 |
|
130 |
| - # Update expectations for legacy configuration |
131 |
| - for old_name, new_name in legacy_env_vars.items(): |
132 |
| - expected_get_env[old_name.name.lower()] = expected_get_env[new_name.name.lower()] |
| 106 | + for string_env_var in STRING_ENV_VARS: |
| 107 | + if string_env_var in ignored_env_vars: |
| 108 | + continue |
133 | 109 |
|
134 |
| - await Actor.init() |
135 |
| - assert Actor.get_env() == expected_get_env |
| 110 | + string_get_env_var = string_env_var.name.lower() |
| 111 | + expected_get_env[string_get_env_var] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) |
| 112 | + monkeypatch.setenv(string_env_var, expected_get_env[string_get_env_var]) |
136 | 113 |
|
137 |
| - await Actor.exit() |
| 114 | + # We need this override so that the actor doesn't fail when connecting to the platform events websocket |
| 115 | + monkeypatch.delenv(ActorEnvVars.EVENTS_WEBSOCKET_URL) |
| 116 | + monkeypatch.delenv(ApifyEnvVars.ACTOR_EVENTS_WS_URL) |
| 117 | + expected_get_env[ActorEnvVars.EVENTS_WEBSOCKET_URL.name.lower()] = None |
| 118 | + expected_get_env[ApifyEnvVars.ACTOR_EVENTS_WS_URL.name.lower()] = None |
| 119 | + |
| 120 | + # Adjust expectations for timedelta fields |
| 121 | + for env_name, env_value in expected_get_env.items(): |
| 122 | + if env_name.endswith('_millis'): |
| 123 | + expected_get_env[env_name] = timedelta(milliseconds=env_value) |
| 124 | + |
| 125 | + # Convert dedicated_cpus to float |
| 126 | + expected_get_env[ApifyEnvVars.DEDICATED_CPUS.name.lower()] = float( |
| 127 | + expected_get_env[ApifyEnvVars.DEDICATED_CPUS.name.lower()] |
| 128 | + ) |
| 129 | + |
| 130 | + # Update expectations for legacy configuration |
| 131 | + for old_name, new_name in legacy_env_vars.items(): |
| 132 | + expected_get_env[old_name.name.lower()] = expected_get_env[new_name.name.lower()] |
| 133 | + |
| 134 | + await Actor.init() |
| 135 | + assert Actor.get_env() == expected_get_env |
| 136 | + |
| 137 | + await Actor.exit() |
0 commit comments