Refactor user endpoint tests to include pagination and response structure validation

- Updated tests for listing users to validate pagination and response format.
- Changed mock return values to include total count and pagination details.
- Refactored user creation mocks for clarity and consistency.
- Enhanced assertions to check for presence of pagination fields in responses.
- Adjusted test cases for user retrieval and updates to ensure proper handling of user data.
- Improved readability by restructuring mock definitions and assertions across various test files.
This commit is contained in:
JSC
2025-08-17 12:36:52 +02:00
parent e6f796a3c9
commit 6b55ff0e81
35 changed files with 863 additions and 503 deletions

View File

@@ -409,7 +409,9 @@ class TestCreditService:
@pytest.mark.asyncio
async def test_recharge_user_credits_success(
self, credit_service, sample_user,
self,
credit_service,
sample_user,
) -> None:
"""Test successful credit recharge for a user."""
mock_session = credit_service.db_session_factory()

View File

@@ -43,7 +43,9 @@ class TestDashboardService:
"total_duration": 75000,
"total_size": 1024000,
}
mock_sound_repository.get_soundboard_statistics = AsyncMock(return_value=mock_stats)
mock_sound_repository.get_soundboard_statistics = AsyncMock(
return_value=mock_stats,
)
result = await dashboard_service.get_soundboard_statistics()

View File

@@ -99,6 +99,11 @@ class TestExtractionService:
url = "https://www.youtube.com/watch?v=test123"
user_id = 1
# Mock user for user_name retrieval
mock_user = Mock()
mock_user.name = "Test User"
extraction_service.user_repo.get_by_id = AsyncMock(return_value=mock_user)
# Mock repository call - no service detection happens during creation
mock_extraction = Extraction(
id=1,
@@ -120,6 +125,7 @@ class TestExtractionService:
assert result["service_id"] is None # Not detected during creation
assert result["title"] is None # Not detected during creation
assert result["status"] == "pending"
assert result["user_name"] == "Test User"
@pytest.mark.asyncio
async def test_create_extraction_basic(self, extraction_service) -> None:
@@ -127,6 +133,11 @@ class TestExtractionService:
url = "https://www.youtube.com/watch?v=test123"
user_id = 1
# Mock user for user_name retrieval
mock_user = Mock()
mock_user.name = "Test User"
extraction_service.user_repo.get_by_id = AsyncMock(return_value=mock_user)
# Mock repository call - creation always succeeds now
mock_extraction = Extraction(
id=2,
@@ -146,6 +157,7 @@ class TestExtractionService:
assert result["id"] == 2
assert result["url"] == url
assert result["status"] == "pending"
assert result["user_name"] == "Test User"
@pytest.mark.asyncio
async def test_create_extraction_any_url(self, extraction_service) -> None:
@@ -153,6 +165,11 @@ class TestExtractionService:
url = "https://invalid.url"
user_id = 1
# Mock user for user_name retrieval
mock_user = Mock()
mock_user.name = "Test User"
extraction_service.user_repo.get_by_id = AsyncMock(return_value=mock_user)
# Mock repository call - even invalid URLs are accepted during creation
mock_extraction = Extraction(
id=3,
@@ -172,6 +189,7 @@ class TestExtractionService:
assert result["id"] == 3
assert result["url"] == url
assert result["status"] == "pending"
assert result["user_name"] == "Test User"
@pytest.mark.asyncio
async def test_process_extraction_with_service_detection(
@@ -408,9 +426,16 @@ class TestExtractionService:
sound_id=42,
)
# Mock user for user_name retrieval
mock_user = Mock()
mock_user.name = "Test User"
extraction_service.extraction_repo.get_by_id = AsyncMock(
return_value=extraction,
)
extraction_service.user_repo.get_by_id = AsyncMock(
return_value=mock_user,
)
result = await extraction_service.get_extraction_by_id(1)
@@ -421,6 +446,7 @@ class TestExtractionService:
assert result["title"] == "Test Video"
assert result["status"] == "completed"
assert result["sound_id"] == 42
assert result["user_name"] == "Test User"
@pytest.mark.asyncio
async def test_get_extraction_by_id_not_found(self, extraction_service) -> None:
@@ -434,52 +460,70 @@ class TestExtractionService:
@pytest.mark.asyncio
async def test_get_user_extractions(self, extraction_service) -> None:
"""Test getting user extractions."""
from app.models.user import User
user = User(id=1, name="Test User", email="test@example.com")
extractions = [
Extraction(
id=1,
service="youtube",
service_id="test123",
url="https://www.youtube.com/watch?v=test123",
user_id=1,
title="Test Video 1",
status="completed",
sound_id=42,
(
Extraction(
id=1,
service="youtube",
service_id="test123",
url="https://www.youtube.com/watch?v=test123",
user_id=1,
title="Test Video 1",
status="completed",
sound_id=42,
),
user,
),
Extraction(
id=2,
service="youtube",
service_id="test456",
url="https://www.youtube.com/watch?v=test456",
user_id=1,
title="Test Video 2",
status="pending",
(
Extraction(
id=2,
service="youtube",
service_id="test456",
url="https://www.youtube.com/watch?v=test456",
user_id=1,
title="Test Video 2",
status="pending",
),
user,
),
]
extraction_service.extraction_repo.get_by_user = AsyncMock(
return_value=extractions,
extraction_service.extraction_repo.get_user_extractions_filtered = AsyncMock(
return_value=(extractions, 2),
)
result = await extraction_service.get_user_extractions(1)
assert len(result) == 2
assert result[0]["id"] == 1
assert result[0]["title"] == "Test Video 1"
assert result[1]["id"] == 2
assert result[1]["title"] == "Test Video 2"
assert result["total"] == 2
assert len(result["extractions"]) == 2
assert result["extractions"][0]["id"] == 1
assert result["extractions"][0]["title"] == "Test Video 1"
assert result["extractions"][0]["user_name"] == "Test User"
assert result["extractions"][1]["id"] == 2
assert result["extractions"][1]["title"] == "Test Video 2"
assert result["extractions"][1]["user_name"] == "Test User"
@pytest.mark.asyncio
async def test_get_pending_extractions(self, extraction_service) -> None:
"""Test getting pending extractions."""
from app.models.user import User
user = User(id=1, name="Test User", email="test@example.com")
pending_extractions = [
Extraction(
id=1,
service="youtube",
service_id="test123",
url="https://www.youtube.com/watch?v=test123",
user_id=1,
title="Pending Video",
status="pending",
(
Extraction(
id=1,
service="youtube",
service_id="test123",
url="https://www.youtube.com/watch?v=test123",
user_id=1,
title="Pending Video",
status="pending",
),
user,
),
]
@@ -492,3 +536,4 @@ class TestExtractionService:
assert len(result) == 1
assert result[0]["id"] == 1
assert result[0]["status"] == "pending"
assert result[0]["user_name"] == "Test User"

View File

@@ -25,9 +25,10 @@ class TestSchedulerService:
@pytest.mark.asyncio
async def test_start_scheduler(self, scheduler_service) -> None:
"""Test starting the scheduler service."""
with patch.object(scheduler_service.scheduler, "add_job") as mock_add_job, \
patch.object(scheduler_service.scheduler, "start") as mock_start:
with (
patch.object(scheduler_service.scheduler, "add_job") as mock_add_job,
patch.object(scheduler_service.scheduler, "start") as mock_start,
):
await scheduler_service.start()
# Verify job was added
@@ -61,7 +62,9 @@ class TestSchedulerService:
"total_credits_added": 500,
}
with patch.object(scheduler_service.credit_service, "recharge_all_users_credits") as mock_recharge:
with patch.object(
scheduler_service.credit_service, "recharge_all_users_credits",
) as mock_recharge:
mock_recharge.return_value = mock_stats
await scheduler_service._daily_credit_recharge()
@@ -71,7 +74,9 @@ class TestSchedulerService:
@pytest.mark.asyncio
async def test_daily_credit_recharge_failure(self, scheduler_service) -> None:
"""Test daily credit recharge task with failure."""
with patch.object(scheduler_service.credit_service, "recharge_all_users_credits") as mock_recharge:
with patch.object(
scheduler_service.credit_service, "recharge_all_users_credits",
) as mock_recharge:
mock_recharge.side_effect = Exception("Database error")
# Should not raise exception, just log it