Files
CustomBingo/tests/test_spreadsheet_reader.py

91 lines
2.8 KiB
Python

import pytest
import pandas as pd
from io import StringIO
from custom_bingo.spreadsheet_reader import read_spreadsheet
import tempfile
import os
def test_read_csv_file():
"""Test reading a CSV file with B, I, N, G, O columns."""
# Create a temporary CSV file
csv_content = """B,I,N,G,O
Apple,Book,Car,Door,Elephant
Banana,Clock,Desk,Engine,Fish"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
temp_file = f.name
try:
result = read_spreadsheet(temp_file)
assert 'B' in result
assert 'I' in result
assert 'N' in result
assert 'G' in result
assert 'O' in result
assert result['B'] == ['Apple', 'Banana']
assert result['I'] == ['Book', 'Clock']
assert result['N'] == ['Car', 'Desk']
assert result['G'] == ['Door', 'Engine']
assert result['O'] == ['Elephant', 'Fish']
finally:
os.remove(temp_file)
def test_read_csv_file_wrong_number_of_columns():
"""Test that reading a CSV file with wrong number of columns raises an error."""
csv_content = """B,I,N,G
Apple,Book,Car,Door
Banana,Clock,Desk,Engine"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
temp_file = f.name
try:
with pytest.raises(ValueError, match="Spreadsheet must have exactly 5 columns"):
read_spreadsheet(temp_file)
finally:
os.remove(temp_file)
def test_read_csv_file_with_completely_empty_column():
"""Test reading a CSV file with a completely empty column raises an error."""
csv_content = """B,I,N,G,O
Apple,Book,,Door,Elephant
Banana,Clock,,Engine,Fish"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
temp_file = f.name
try:
with pytest.raises(ValueError, match="Column N is empty"):
read_spreadsheet(temp_file)
finally:
os.remove(temp_file)
def test_read_csv_file_with_different_column_names():
"""Test reading a CSV file with different column names still works."""
csv_content = """Col1,Col2,Col3,Col4,Col5
Apple,Book,Car,Door,Elephant
Banana,Clock,Desk,Engine,Fish"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
temp_file = f.name
try:
result = read_spreadsheet(temp_file)
# Column names should be reassigned to B, I, N, G, O
assert 'B' in result
assert 'I' in result
assert 'N' in result
assert 'G' in result
assert 'O' in result
assert result['B'] == ['Apple', 'Banana']
assert result['I'] == ['Book', 'Clock']
finally:
os.remove(temp_file)