import pytest import tempfile import os from click.testing import CliRunner from custom_bingo.main import main def test_main_command(): """Test the main CLI command with a temporary CSV file.""" runner = CliRunner() # Create a temporary CSV file csv_content = """B,I,N,G,O Apple,Book,Car,Door,Elephant Banana,Clock,Desk,Engine,Fish Orange,Pencil,Tree,Flower,Guitar Grape,Eraser,River,Grass,Hat Lemon,Notebook,Moon,Leaf,Jacket""" with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as csv_file: csv_file.write(csv_content) csv_path = csv_file.name # Create a temporary output PDF file with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as pdf_file: pdf_path = pdf_file.name # Remove the PDF file so the command can create it os.remove(pdf_path) try: # Run the CLI command result = runner.invoke(main, [ '--input-file', csv_path, '--output-file', pdf_path, '--number-of-cards', '1' ]) # Check that the command completed successfully assert result.exit_code == 0 assert "Successfully generated" in result.output # Check that the output PDF file was created assert os.path.exists(pdf_path) assert os.path.getsize(pdf_path) > 0 finally: # Clean up temporary files if os.path.exists(csv_path): os.remove(csv_path) if os.path.exists(pdf_path): os.remove(pdf_path) def test_main_command_with_custom_title(): """Test the main CLI command with custom title.""" runner = CliRunner() # Create a temporary CSV file csv_content = """B,I,N,G,O Apple,Book,Car,Door,Elephant Banana,Clock,Desk,Engine,Fish Orange,Pencil,Tree,Flower,Guitar Grape,Eraser,River,Grass,Hat Lemon,Notebook,Moon,Leaf,Jacket""" with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as csv_file: csv_file.write(csv_content) csv_path = csv_file.name # Create a temporary output PDF file with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as pdf_file: pdf_path = pdf_file.name # Remove the PDF file so the command can create it os.remove(pdf_path) try: # Run the CLI command with custom title result = runner.invoke(main, [ '--input-file', csv_path, '--output-file', pdf_path, '--number-of-cards', '1', '--title', 'Custom Title' ]) # Check that the command completed successfully assert result.exit_code == 0 assert "Successfully generated" in result.output # Check that the output PDF file was created assert os.path.exists(pdf_path) assert os.path.getsize(pdf_path) > 0 finally: # Clean up temporary files if os.path.exists(csv_path): os.remove(csv_path) if os.path.exists(pdf_path): os.remove(pdf_path) def test_main_command_with_sub_headers(): """Test the main CLI command with sub-headers.""" runner = CliRunner() # Create a temporary CSV file csv_content = """B,I,N,G,O Apple,Book,Car,Door,Elephant Banana,Clock,Desk,Engine,Fish Orange,Pencil,Tree,Flower,Guitar Grape,Eraser,River,Grass,Hat Lemon,Notebook,Moon,Leaf,Jacket""" with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as csv_file: csv_file.write(csv_content) csv_path = csv_file.name # Create a temporary output PDF file with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as pdf_file: pdf_path = pdf_file.name # Remove the PDF file so the command can create it os.remove(pdf_path) try: # Run the CLI command with sub-headers result = runner.invoke(main, [ '--input-file', csv_path, '--output-file', pdf_path, '--number-of-cards', '1', '--sub-headers', 'Fruits,Objects,Nouns,Actions,Accessories' ]) # Check that the command completed successfully assert result.exit_code == 0 assert "Successfully generated" in result.output # Check that the output PDF file was created assert os.path.exists(pdf_path) assert os.path.getsize(pdf_path) > 0 finally: # Clean up temporary files if os.path.exists(csv_path): os.remove(csv_path) if os.path.exists(pdf_path): os.remove(pdf_path) def test_main_command_invalid_file(): """Test the main CLI command with an invalid input file.""" runner = CliRunner() # Use a non-existent file result = runner.invoke(main, [ '--input-file', 'nonexistent.csv', '--output-file', 'output.pdf', '--number-of-cards', '1' ]) # Should exit with error code assert result.exit_code != 0