"""Tests for highwatermark logic.""" from unittest.mock import patch import pytest from oto import response as oto_response from analytics.logic import highwatermark HIGHWATERMARK = {"highwatermark": "2021-08-08"} @pytest.fixture def expected_payload_by_streams(): """Return expected payload by streams.""" return HIGHWATERMARK @pytest.fixture def expected_payload_by_videos(): """Return expected payload by videos.""" return HIGHWATERMARK @pytest.fixture def expected_payload_by_downloads(): """Return expected payload by downloads.""" return HIGHWATERMARK @pytest.fixture def mock_get_downloads_date_success(): """Mock successful get_downloads_max_available_date model response.""" with patch( "analytics.utils.data_availability.get_downloads_max_available_date" ) as get_downloads_max_available_date: get_downloads_max_available_date.return_value = "2021-08-08" yield get_downloads_max_available_date @pytest.fixture def mock_get_downloads_date_failure(): """Mock failure get_downloads_max_available_date model response.""" with patch( "analytics.utils.data_availability.get_downloads_max_available_date" ) as get_downloads_max_available_date: get_downloads_max_available_date.return_value = None yield get_downloads_max_available_date @pytest.fixture def mock_get_streams_date_success(): """Mock successful get_max_available_date model response.""" with patch( "analytics.utils.data_availability.get_max_available_date" ) as get_max_available_date: get_max_available_date.return_value = "2021-08-08" yield get_max_available_date @pytest.fixture def mock_get_streams_date_failure(): """Mock failure get_max_available_date model response.""" with patch( "analytics.utils.data_availability.get_max_available_date" ) as get_streams_max_available_date: get_streams_max_available_date.return_value = None yield get_streams_max_available_date @pytest.fixture def mock_get_videos_date_success(): """Mock successful get_videos_max_available_date model response.""" with patch( "analytics.utils.data_availability.get_videos_max_available_date" ) as get_videos_max_available_date: get_videos_max_available_date.return_value = "2021-08-08" yield get_videos_max_available_date @pytest.fixture def mock_get_videos_date_failure(): """Mock failure get_videos_max_available_date model response.""" with patch( "analytics.utils.data_availability.get_videos_max_available_date" ) as get_videos_max_available_date: get_videos_max_available_date.return_value = None yield get_videos_max_available_date class TestHighwatermarkWithStreams: """Test get_highwatermark with streams.""" @pytest.fixture def response(self, mock_get_streams_date_success): """Run get_highwatermark.""" return highwatermark.get_highwatermark("streams") def test_succeeds(self, response, expected_payload_by_streams): """Test successful response.""" assert response.status == 200 assert isinstance(response, oto_response.Response) assert response.message == expected_payload_by_streams def test_fails_when_streams_model_fails(self, mock_get_streams_date_failure): """Test failure response when streams model fails.""" response = highwatermark.get_highwatermark("streams") assert response.status == 200 assert response.message == {"highwatermark": None} class TestHighwatermarkWithVideos: """Test get_highwatermark with videos.""" @pytest.fixture def response( self, mock_get_videos_date_success, request_context_for_label, mock_permissions_for_label, ): """Run get_highwatermark.""" return highwatermark.get_highwatermark("videos") def test_succeeds(self, response, expected_payload_by_videos): """Test successful response.""" assert response.status == 200 assert isinstance(response, oto_response.Response) assert response.message == expected_payload_by_videos def test_fails_when_videos_model_fails(self, mock_get_videos_date_failure): """Test failure response when videos model fails.""" response = highwatermark.get_highwatermark("videos") assert response.status == 200 assert response.message == {"highwatermark": None} class TestHighwatermarkWithDownloads: """Test get_highwatermark with downloads.""" @pytest.fixture def response( self, mock_get_downloads_date_success, request_context_for_label, mock_permissions_for_label, ): """Run get_highwatermark.""" return highwatermark.get_highwatermark("downloads") def test_succeeds(self, response, expected_payload_by_downloads): """Test successful response.""" assert response.status == 200 assert isinstance(response, oto_response.Response) assert response.message == expected_payload_by_downloads def test_fails_when_downloads_model_fails(self, mock_get_downloads_date_failure): """Test failure response when downloads model fails.""" response = highwatermark.get_highwatermark("downloads") assert response.status == 200 assert response.message == {"highwatermark": None}