Skip to content
Snippets Groups Projects
Select Git revision
  • 7a0c3b17729eac2b4753ef44158f9c90c2a7d0d1
  • master default protected
2 results

test_excel_operation.py

Blame
  • test_excel_operation.py 1.58 KiB
    import pytest
    import openpyxl
    from utils.excel_operations import load_excel, save_excel, create_output_workbook
    import os
    
    @pytest.fixture
    def sample_workbook(tmp_path):
        """Create a sample workbook for testing."""
        file_path = tmp_path / "test.xlsx"
        workbook = openpyxl.Workbook()
        sheet = workbook.active
        sheet.append(["Name", "Age", "City"])
        sheet.append(["John Doe", 30, "New York"])
        workbook.save(file_path)
        return file_path
    
    def test_load_excel(sample_workbook):
        """Test loading an existing Excel file."""
        workbook = load_excel(sample_workbook)
        sheet = workbook.active
    
        assert sheet.cell(row=1, column=1).value == "Name"
        assert sheet.cell(row=2, column=1).value == "John Doe"
    
    def test_save_excel(tmp_path):
        """Test saving an Excel file."""
        workbook = openpyxl.Workbook()
        sheet = workbook.active
        sheet.append(["Product", "Price"])
        sheet.append(["Laptop", 1200])
    
        file_path = tmp_path / "output.xlsx"
        save_excel(workbook, file_path)
    
        # Verify the file is created and data is saved correctly
        loaded_workbook = openpyxl.load_workbook(file_path)
        loaded_sheet = loaded_workbook.active
    
        assert loaded_sheet.cell(row=1, column=1).value == "Product"
        assert loaded_sheet.cell(row=2, column=2).value == 1200
    
    def test_create_output_workbook():
        """Test creating a new workbook with headers."""
        headers = ["ID", "Name", "Department"]
        workbook = create_output_workbook(headers)
        sheet = workbook.active
    
        assert sheet.cell(row=1, column=1).value == "ID"
        assert sheet.cell(row=1, column=3).value == "Department"