import pytest import requests from unittest.mock import patch from src.movie_showtimes import get_movie_showtimes, display_showtimes from io import StringIO from rich.console import Console @pytest.fixture def mock_successful_response(): """Mock a successful API response.""" return { "showtimes": [ { "day": "Today", "movies": [ { "name": "Dune", "showing": [ {"time": ["12:30pm", "4:00pm"], "type": "Standard"}, {"time": ["1:30pm", "5:30pm"], "type": "IMAX"} ] } ] } ] } @pytest.fixture def mock_failed_response(): """Mock a failed API response.""" return {} @patch("requests.get") def test_get_movie_showtimes_success(mock_get, mock_successful_response): """Test fetching movie showtimes successfully from SerpApi.""" mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = mock_successful_response showtimes = get_movie_showtimes() assert len(showtimes) == 1 assert showtimes[0]["day"] == "Today" assert showtimes[0]["movies"][0]["name"] == "Dune" assert showtimes[0]["movies"][0]["showing"][0]["type"] == "Standard" @patch("requests.get") def test_get_movie_showtimes_failure(mock_get, mock_failed_response): """Test handling of API failure (non-200 response).""" mock_get.return_value.status_code = 500 mock_get.return_value.json.return_value = mock_failed_response with pytest.raises(ValueError, match="Could not fetch movie showtimes from SerpApi."): get_movie_showtimes() @patch("sys.stdout", new_callable=StringIO) def test_display_showtimes_output(mock_stdout, mock_successful_response): """Test console output formatting of display_showtimes.""" console = Console(file=mock_stdout) display_showtimes(mock_successful_response["showtimes"]) output = mock_stdout.getvalue() assert "Today" in output assert "Dune" in output assert "Standard" in output assert "12:30pm, 4:00pm" in output assert "IMAX" in output assert "1:30pm, 5:30pm" in output