import asyncio import json from dataclasses import dataclass from tempfile import NamedTemporaryFile import pytest from src.backend.connectors.youtube_apis import helpers @pytest.mark.asyncio async def test_batch_worker(): task_count = 10 counter = 0 async def dummy_awaitable(): await asyncio.sleep(0) nonlocal counter counter += 1 batch_worker = helpers.batch_worker() await asyncio.gather( *[ batch_worker(asyncio.create_task(dummy_awaitable())) for _ in range(task_count) ] ) assert counter == task_count def test_to_response_object(): """Test that the response object is correctly created from the provided data.""" @dataclass class SampleObject: first_name: str last_name: str data = {"firstName": "John", "lastName": "Doe"} response = helpers.to_response_object(data, SampleObject) # Test camel case to snake case conversion. assert response.first_name == "John" assert response.last_name == "Doe" class TestLoadCredentials: def test_load_credentials_from_path(self): """Test that credentials are loaded correctly from provided file path.""" mock_content = {"key": "value"} with NamedTemporaryFile() as mock_credentials_file: mock_credentials_file.write(json.dumps(mock_content).encode()) mock_credentials_file.seek(0) result = helpers.load_credentials(mock_credentials_file.name) assert result == mock_content @pytest.mark.parametrize( "service_account_file_path", [ None, # No file provided "non_existent_file", # Non-existent file ], ) def test_load_credentials_use_fallback(self, service_account_file_path): """Test that the fallback credentials are used when no file is provided.""" result = helpers.load_credentials(service_account_file_path) assert ( result == helpers.YT_CMS_FALLBACK_CREDENTIALS ), "Fallback credentials should be used when no valid file is provided."