1
+ from pathlib import Path
2
+
1
3
import pytest
2
4
5
+ from crawlee import service_locator
3
6
from crawlee .configuration import Configuration as CrawleeConfiguration
4
7
from crawlee .crawlers import BasicCrawler
5
8
from crawlee .errors import ServiceConflictError
6
9
7
10
from apify import Actor
8
11
from apify import Configuration as ApifyConfiguration
9
- from apify ._configuration import service_locator
10
12
11
13
12
14
@pytest .mark .parametrize (
@@ -105,9 +107,9 @@ async def test_crawler_implicit_configuration() -> None:
105
107
"""Test that crawler and Actor use implicit service_locator based configuration unless explicit configuration
106
108
was passed to them."""
107
109
async with Actor ():
108
- crawler_1 = BasicCrawler ()
110
+ crawler = BasicCrawler ()
109
111
110
- assert service_locator .get_configuration () is crawler_1 ._service_locator .get_configuration ()
112
+ assert service_locator .get_configuration () is crawler ._service_locator .get_configuration ()
111
113
112
114
113
115
async def test_crawlers_own_configuration () -> None :
@@ -132,30 +134,54 @@ async def test_crawler_global_configuration() -> None:
132
134
service_locator .set_configuration (config_global )
133
135
134
136
async with Actor ():
135
- crawler_1 = BasicCrawler ()
137
+ crawler = BasicCrawler ()
136
138
137
139
assert service_locator .get_configuration () is config_global
138
- assert crawler_1 ._service_locator .get_configuration () is config_global
140
+ assert crawler ._service_locator .get_configuration () is config_global
139
141
140
142
141
- async def test_storage_retrieved_is_different_with_different_config () -> None :
143
+ async def test_storage_retrieved_is_different_with_different_config (tmp_path : Path ) -> None :
142
144
"""Test that retrieving storage depends on used configuration."""
145
+ dir_1 = tmp_path / 'dir_1'
146
+ dir_2 = tmp_path / 'dir_2'
143
147
config_actor = ApifyConfiguration ()
144
- apify_crawler_1 = ApifyConfiguration ()
148
+ config_actor .storage_dir = str (dir_1 )
149
+ apify_crawler = ApifyConfiguration ()
150
+ apify_crawler .storage_dir = str (dir_2 )
145
151
146
152
async with Actor (configuration = config_actor ):
147
153
actor_kvs = await Actor .open_key_value_store ()
148
- crawler_1 = BasicCrawler (configuration = apify_crawler_1 )
149
- crawler_kvs = await crawler_1 .get_key_value_store ()
154
+ crawler = BasicCrawler (configuration = apify_crawler )
155
+ crawler_kvs = await crawler .get_key_value_store ()
150
156
151
157
assert actor_kvs is not crawler_kvs
152
158
153
159
160
+ async def test_storage_retrieved_is_same_with_equivalent_config () -> None :
161
+ """Test that retrieving storage depends on used configuration. If two same configuration(even if they are different
162
+ instances) are used it returns same storage."""
163
+ config_actor = ApifyConfiguration ()
164
+ apify_crawler = ApifyConfiguration ()
165
+
166
+ async with Actor (configuration = config_actor ):
167
+ actor_kvs = await Actor .open_key_value_store ()
168
+ crawler = BasicCrawler (configuration = apify_crawler )
169
+ crawler_kvs = await crawler .get_key_value_store ()
170
+
171
+ assert actor_kvs is crawler_kvs
172
+
173
+
154
174
async def test_storage_retrieved_is_same_with_same_config () -> None :
155
175
"""Test that retrieving storage is same if same configuration is used."""
156
176
async with Actor ():
157
177
actor_kvs = await Actor .open_key_value_store ()
158
- crawler_1 = BasicCrawler ()
159
- crawler_kvs = await crawler_1 .get_key_value_store ()
178
+ crawler = BasicCrawler ()
179
+ crawler_kvs = await crawler .get_key_value_store ()
160
180
161
181
assert actor_kvs is crawler_kvs
182
+
183
+
184
+ async def test_crawler_uses_apify_config () -> None :
185
+ """Test that crawler is using ApifyConfiguration in SDK context."""
186
+ crawler = BasicCrawler ()
187
+ assert isinstance (crawler ._service_locator .get_configuration (), ApifyConfiguration )
0 commit comments