"""Spotify parsing logic test.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from requests import HTTPError from src.spotify_parser import ParsingError from src import spotify_parser @patch('src.spotify_parser.requests') def test_successful_parsing(requests_mock, mock_artist_page, mock_artist_id): """Test parsing the number of monthly listeners successfully.""" expected_listeners_count = 854361 requests_mock.get.return_value = MagicMock(text=mock_artist_page) result = spotify_parser.get_spotify_monthly_listeners(mock_artist_id) assert result == expected_listeners_count @patch('src.spotify_parser.requests') def test_parsing_html_success_no_about_block( requests_mock, mock_artist_id, mock_broken_artist_page_no_about_block): """Test parsing the incorrect HTML page.""" expected_listeners_count = 854361 requests_mock.get.return_value = MagicMock(text=mock_broken_artist_page_no_about_block) result = spotify_parser.get_spotify_monthly_listeners(mock_artist_id) assert result == expected_listeners_count @pytest.mark.parametrize('error', [(HTTPError()), (ParsingError())]) @patch('src.spotify_parser.requests') def test_parsing_failure(requests_mock, mock_artist_id, error): """Test receiving an http error from Spotify or parsing failure.""" requests_mock.get.return_value.raise_for_status = MagicMock( side_effect=error) with pytest.raises(type(error)): spotify_parser.get_spotify_monthly_listeners(mock_artist_id) @patch('src.spotify_parser.requests') def test_parsing_html_failure( requests_mock, mock_artist_id, mock_totally_broken_artist_page): """Test parsing the incorrect HTML page.""" requests_mock.get.return_value = MagicMock(text=mock_totally_broken_artist_page) with pytest.raises(ParsingError): spotify_parser.get_spotify_monthly_listeners(mock_artist_id)