from time import sleep import pytest from dapd_api_scraper.utils import async_cache, async_retry, chunks, deep_chunks def test_chunks(): array = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunked_array = [[1, 2], [3, 4], [5, 6], [7, 8], [9]] result = list(chunks(array, chunk_size=2)) assert result == chunked_array def test_deep_chunks(): array = [[1, 2, 3], [4, 5, 6, 7], [8], [9]] chunked_array = [[1, 2], [3], [4, 5], [6, 7], [8], [9]] result = list(deep_chunks(array, chunk_size=2)) assert result == chunked_array @pytest.mark.asyncio async def test_async_retry_success(): @async_retry() async def test_function(): return True result = await test_function() assert result is True @pytest.mark.asyncio async def test_async_retry_success_with_retry(): calls_count = 0 @async_retry(delay=1, max_retries=5) async def test_function(): nonlocal calls_count calls_count += 1 if calls_count >= 5: return True raise Exception("Test") assert await test_function() is True @pytest.mark.asyncio async def test_async_retry_fail(): calls_count = 0 @async_retry(delay=1, max_retries=5) async def test_function(): nonlocal calls_count calls_count += 1 raise Exception("Test") with pytest.raises(Exception): await test_function() assert calls_count == 6 @pytest.mark.asyncio async def test_async_cache(): calls_count = 0 @async_cache(timeout=5) async def test_function(*args, **kwargs): nonlocal calls_count calls_count += 1 return args, kwargs result = await test_function("test") result_2 = await test_function("test") assert result == (("test",), {}) assert result_2 == (("test",), {}) assert calls_count == 1 result_3 = await test_function("test_2") assert result_3 == (("test_2",), {}) assert calls_count == 2 result = await test_function("test") sleep(1) result_2 = await test_function("test") assert result == (("test",), {}) assert result_2 == (("test",), {}) assert calls_count == 2 sleep(6) result_3 = await test_function("test") assert result_3 == (("test",), {}) assert calls_count == 3