"""Notifications related tests.""" import pytest from src.errors.notifications import NotificationError from src.logic.notifications import send_streams_updated_notification from src.models.store import Store from src.models.store_state import StoreState from tests.unit import data def test_send_streams_updated_notification_success(mocker): """Test send_streams_updated_notification success.""" store_state = StoreState( store=Store.SPOTIFY, last_available_date=data.DATE_OBJ, updated_at=data.TIME_OBJ, ) mock = mocker.patch('src.logic.notifications.send_request') send_streams_updated_notification(store_state) mock.assert_called_once_with( 'POST', '/activity/streams_updated', json={ 'store_id': Store.SPOTIFY, 'timestamp': data.TIME_OBJ.strftime('%Y-%m-%d %H:%M:%S'), 'available_date': data.DATE_OBJ.strftime('%Y-%m-%d'), }, ) def test_send_streams_updated_notification_failure(mocker): """Test that send_streams_updated_notification raises NotificationError on failure.""" store_state = StoreState( store=Store.SPOTIFY, last_available_date=data.DATE_OBJ, updated_at=data.TIME_OBJ, ) error_message = 'API is down' mocker.patch('src.logic.notifications.send_request', side_effect=Exception(error_message)) with pytest.raises(NotificationError) as excinfo: send_streams_updated_notification(store_state) assert str(excinfo.value) == 'Failed to send "streams_updated" notification' assert isinstance(excinfo.value.__cause__, Exception) assert str(excinfo.value.__cause__) == error_message