Skip to content

Commit

Permalink
modify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Gurujala committed Aug 8, 2024
1 parent 0bb0756 commit 41def02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 73 deletions.
40 changes: 0 additions & 40 deletions excel_helper/excel_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
46 changes: 13 additions & 33 deletions tests/test_excel_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,85 @@ 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")
excel_helper.vlookup(5, 1, 1, 1, 3, 2, 2, 5, 2)
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()
assert (
excel_helper.active_sheet.column_dimensions["A"].width
< excel_helper.active_sheet.column_dimensions["B"].width
)

if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 41def02

Please sign in to comment.