1+ from pathlib import Path
2+
13import pytest
24
5+ from crawlee import service_locator
36from crawlee .configuration import Configuration as CrawleeConfiguration
47from crawlee .crawlers import BasicCrawler
58from crawlee .errors import ServiceConflictError
69
710from apify import Actor
811from apify import Configuration as ApifyConfiguration
9- from apify ._configuration import service_locator
1012
1113
1214@pytest .mark .parametrize (
@@ -105,9 +107,9 @@ async def test_crawler_implicit_configuration() -> None:
105107 """Test that crawler and Actor use implicit service_locator based configuration unless explicit configuration
106108 was passed to them."""
107109 async with Actor ():
108- crawler_1 = BasicCrawler ()
110+ crawler = BasicCrawler ()
109111
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 ()
111113
112114
113115async def test_crawlers_own_configuration () -> None :
@@ -132,30 +134,54 @@ async def test_crawler_global_configuration() -> None:
132134 service_locator .set_configuration (config_global )
133135
134136 async with Actor ():
135- crawler_1 = BasicCrawler ()
137+ crawler = BasicCrawler ()
136138
137139 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
139141
140142
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 :
142144 """Test that retrieving storage depends on used configuration."""
145+ dir_1 = tmp_path / 'dir_1'
146+ dir_2 = tmp_path / 'dir_2'
143147 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 )
145151
146152 async with Actor (configuration = config_actor ):
147153 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 ()
150156
151157 assert actor_kvs is not crawler_kvs
152158
153159
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+
154174async def test_storage_retrieved_is_same_with_same_config () -> None :
155175 """Test that retrieving storage is same if same configuration is used."""
156176 async with Actor ():
157177 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 ()
160180
161181 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