Add ruff config to exclude alembic E402, SQLAlchemy F821, and pre-existing E501 line-length issues. Auto-fix 69 unused imports and 2 f-strings without placeholders. Manually fix 8 unused variables. Apply ruff format to 127 files. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""Tests for config change NATS subscriber."""
|
|
|
|
import pytest
|
|
from unittest.mock import AsyncMock, patch
|
|
from uuid import uuid4
|
|
|
|
from app.services.config_change_subscriber import handle_config_changed
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_triggers_backup_on_config_change():
|
|
"""Config change event should trigger a backup."""
|
|
event = {
|
|
"device_id": str(uuid4()),
|
|
"tenant_id": str(uuid4()),
|
|
"old_timestamp": "2026-03-07 11:00:00",
|
|
"new_timestamp": "2026-03-07 12:00:00",
|
|
}
|
|
|
|
with (
|
|
patch(
|
|
"app.services.config_change_subscriber.backup_service.run_backup",
|
|
new_callable=AsyncMock,
|
|
) as mock_backup,
|
|
patch(
|
|
"app.services.config_change_subscriber._last_backup_within_dedup_window",
|
|
new_callable=AsyncMock,
|
|
return_value=False,
|
|
),
|
|
):
|
|
await handle_config_changed(event)
|
|
|
|
mock_backup.assert_called_once()
|
|
assert mock_backup.call_args[1]["trigger_type"] == "config-change"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_skips_backup_within_dedup_window():
|
|
"""Should skip backup if last backup was < 5 minutes ago."""
|
|
event = {
|
|
"device_id": str(uuid4()),
|
|
"tenant_id": str(uuid4()),
|
|
"old_timestamp": "2026-03-07 11:00:00",
|
|
"new_timestamp": "2026-03-07 12:00:00",
|
|
}
|
|
|
|
with (
|
|
patch(
|
|
"app.services.config_change_subscriber.backup_service.run_backup",
|
|
new_callable=AsyncMock,
|
|
) as mock_backup,
|
|
patch(
|
|
"app.services.config_change_subscriber._last_backup_within_dedup_window",
|
|
new_callable=AsyncMock,
|
|
return_value=True,
|
|
),
|
|
):
|
|
await handle_config_changed(event)
|
|
|
|
mock_backup.assert_not_called()
|