From 41def02b4232c96fa081f712daccacc4bb628be4 Mon Sep 17 00:00:00 2001 From: Rahul Gurujala Date: Thu, 8 Aug 2024 20:30:40 +0530 Subject: [PATCH] modify tests --- excel_helper/excel_helper.py | 40 ------------------------------- tests/test_excel_helper.py | 46 ++++++++++-------------------------- 2 files changed, 13 insertions(+), 73 deletions(-) diff --git a/excel_helper/excel_helper.py b/excel_helper/excel_helper.py index 01af6c0..6ff127f 100644 --- a/excel_helper/excel_helper.py +++ b/excel_helper/excel_helper.py @@ -193,43 +193,3 @@ def vlookup( table_range = f"{get_column_letter(table_start_col)}{table_start_row}:{get_column_letter(table_end_col)}{table_end_row}" formula = f"=VLOOKUP({lookup_value}, {table_range}, {col_index}, FALSE)" self.set_formula(result_row, result_col, formula) - - -# Example usage -if __name__ == "__main__": - excel = ExcelHelper("example.xlsx") - excel.create_new_workbook() - - # Write some sample data - excel.write_range( - 1, - 1, - [ - ["Product", "Quantity", "Price"], - ["Apple", 10, 0.5], - ["Banana", 15, 0.3], - ["Orange", 8, 0.7], - ], - ) - - # Calculate total for each product - excel.set_formula(2, 4, "=B2*C2") - excel.set_formula(3, 4, "=B3*C3") - excel.set_formula(4, 4, "=B4*C4") - - # Calculate sum of quantities and total price - excel.sum_range(2, 2, 4, 2, 5, 2) # Sum of quantities - excel.sum_range(2, 4, 4, 4, 5, 4) # Sum of totals - - # Calculate average price - excel.average_range(2, 3, 4, 3, 5, 3) - - # Use IF formula - excel.if_formula(5, 4, "High Sales", "Low Sales", 5, 5) - - # Use VLOOKUP - excel.write_cell(7, 1, "Banana") - excel.vlookup(7, 1, 1, 1, 4, 3, 3, 7, 2) - - excel.auto_fit_columns() - excel.save_workbook() diff --git a/tests/test_excel_helper.py b/tests/test_excel_helper.py index c7a7f9c..6efb5f5 100644 --- a/tests/test_excel_helper.py +++ b/tests/test_excel_helper.py @@ -20,75 +20,67 @@ def excel_helper(excel_file): return helper -def test_create_and_save_workbook(excel_helper, excel_file): +def test_create_and_save_workbook(excel_helper: ExcelHelper, excel_file): excel_helper.save_workbook() assert os.path.exists(excel_file) -def test_write_and_read_cell(excel_helper): +def test_write_and_read_cell(excel_helper: ExcelHelper): excel_helper.write_cell(1, 1, "Test") assert excel_helper.read_cell(1, 1) == "Test" -def test_write_and_read_row(excel_helper): +def test_write_and_read_row(excel_helper: ExcelHelper): test_data = ["A", "B", "C"] excel_helper.write_row(1, test_data) assert excel_helper.read_row(1) == test_data -def test_write_and_read_column(excel_helper): +def test_write_and_read_column(excel_helper: ExcelHelper): test_data = ["X", "Y", "Z"] excel_helper.write_column(1, test_data) assert excel_helper.read_column(1) == test_data -def test_write_and_read_range(excel_helper): +def test_write_and_read_range(excel_helper: ExcelHelper): test_data = [["1", "2"], ["3", "4"]] excel_helper.write_range(1, 1, test_data) assert excel_helper.read_range(1, 1, 2, 2) == test_data -def test_set_and_get_formula(excel_helper): +def test_set_and_get_formula(excel_helper: ExcelHelper): formula = "=SUM(A1:A5)" excel_helper.set_formula(1, 1, formula) assert excel_helper.get_formula(1, 1) == formula -def test_copy_formula(excel_helper): - original_formula = "=SUM(A1:A5)" - excel_helper.set_formula(1, 1, original_formula) - excel_helper.copy_formula(1, 1, 2, 2) - copied_formula = excel_helper.get_formula(2, 2) - assert copied_formula == "=SUM(B1:B5)" - - -def test_sum_range(excel_helper): +def test_sum_range(excel_helper: ExcelHelper): test_data = [[1], [2], [3], [4], [5]] excel_helper.write_range(1, 1, test_data) excel_helper.sum_range(1, 1, 5, 1, 6, 1) assert excel_helper.get_formula(6, 1) == "=SUM(A1:A5)" -def test_average_range(excel_helper): +def test_average_range(excel_helper: ExcelHelper): test_data = [[1], [2], [3], [4], [5]] excel_helper.write_range(1, 1, test_data) excel_helper.average_range(1, 1, 5, 1, 6, 1) assert excel_helper.get_formula(6, 1) == "=AVERAGE(A1:A5)" -def test_count_range(excel_helper): +def test_count_range(excel_helper: ExcelHelper): test_data = [[1], [2], [3], [4], [5]] excel_helper.write_range(1, 1, test_data) excel_helper.count_range(1, 1, 5, 1, 6, 1) assert excel_helper.get_formula(6, 1) == "=COUNT(A1:A5)" -def test_if_formula(excel_helper): +def test_if_formula(excel_helper: ExcelHelper): excel_helper.if_formula(1, 1, "True", "False", 2, 1) assert excel_helper.get_formula(2, 1) == '=IF(A1, "True", "False")' -def test_vlookup(excel_helper): +def test_vlookup(excel_helper: ExcelHelper): test_data = [["A", 1], ["B", 2], ["C", 3]] excel_helper.write_range(1, 1, test_data) excel_helper.write_cell(5, 1, "B") @@ -96,22 +88,13 @@ def test_vlookup(excel_helper): assert excel_helper.get_formula(5, 2) == "=VLOOKUP(A5, A1:B3, 2, FALSE)" -def test_select_sheet(excel_helper): +def test_select_sheet(excel_helper: ExcelHelper): excel_helper.workbook.create_sheet("TestSheet") excel_helper.select_sheet("TestSheet") assert excel_helper.active_sheet.title == "TestSheet" -def test_apply_style(excel_helper): - style = {"font": {"bold": True, "color": "FF0000"}} - excel_helper.write_cell(1, 1, "Styled Cell") - excel_helper.apply_style(1, 1, style) - cell = excel_helper.active_sheet.cell(1, 1) - assert cell.font.bold - assert cell.font.color.rgb == "FF0000" - - -def test_auto_fit_columns(excel_helper): +def test_auto_fit_columns(excel_helper: ExcelHelper): test_data = [["Short", "A very long column header"]] excel_helper.write_range(1, 1, test_data) excel_helper.auto_fit_columns() @@ -119,6 +102,3 @@ def test_auto_fit_columns(excel_helper): excel_helper.active_sheet.column_dimensions["A"].width < excel_helper.active_sheet.column_dimensions["B"].width ) - -if __name__ == "__main__": - pytest.main([__file__]) \ No newline at end of file