Skip to content

Commit

Permalink
improving test performance + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-st-john committed May 22, 2024
1 parent 1c93524 commit 38909f0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
2 changes: 2 additions & 0 deletions coverage_sticker.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set coverage_output=coverage report -m
coverage-badge -o test_coverage.svg -f
43 changes: 27 additions & 16 deletions src/yedextended/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,19 +1067,32 @@ def wrapper_func(self, *args, **kwargs):
excel_data = kwargs.get("excel_data", None)
type = kwargs.get("type", None)
self.excel_type_verification(type)
if excel_data:
if isinstance(excel_data, io.BytesIO):
in_mem_file = excel_data
elif isinstance(excel_data, str):
with open(excel_data, "rb") as f:
in_mem_file = io.BytesIO(f.read())
else:
if not os.path.isfile(self.TEMP_EXCEL_SHEET):
self.create_excel_template(type)
sleep(1)

if save:
self.create_excel_template(type)
sleep(0.5)
with open(self.TEMP_EXCEL_SHEET, "rb") as f:
in_mem_file = io.BytesIO(f.read())

else:
# In case we are given a file path or in-memory file, for excel to graph
if excel_data:
if isinstance(excel_data, io.BytesIO):
in_mem_file = excel_data
elif isinstance(excel_data, str):
with open(excel_data, "rb") as f:
in_mem_file = io.BytesIO(f.read())
# Primary use case - from template for graph to excel
else:
with open(self.TEMP_EXCEL_SHEET, "rb") as f:
in_mem_file = io.BytesIO(f.read())

# if not os.path.isfile(self.TEMP_EXCEL_SHEET):
# self.create_excel_template(type)
# sleep(1)
# with open(self.TEMP_EXCEL_SHEET, "rb") as f:
# in_mem_file = io.BytesIO(f.read())

if not in_mem_file:
raise RuntimeWarning("No excel data found to open.")
self.excel_wb = pyxl.load_workbook(in_mem_file)
Expand All @@ -1101,10 +1114,6 @@ def graph_to_excel_conversion(self, type=None, graph=None) -> None:
if graph:
self.graph = graph

self.excel_type_verification(type)

self.create_excel_template(type)

self.original_stats = self.graph.gather_graph_stats()
row = 2

Expand Down Expand Up @@ -1462,7 +1471,9 @@ def bulk_data_management(self, type=None, graph=None, excel_data=None):
"""Process of converting to excel for manual operations, allows user to modify and then convert back to graph."""
self.graph_to_excel_conversion(type=type, graph=graph)
self.give_user_chance_to_modify()
self.excel_to_graph_conversion(excel_data)
self.excel_to_graph_conversion(type=type, excel_data=excel_data)

return self


class Graph:
Expand Down Expand Up @@ -1948,7 +1959,7 @@ def process_node(parent, input_node):

def manage_graph_data_in_excel(self, type=None):
"""Port graph data into Excel in several formats for easy and bulk creation and management. Then ports back into python graph structure."""
ExcelManager().bulk_data_management(graph=self, type=type)
return ExcelManager().bulk_data_management(graph=self, type=type)

def gather_graph_stats(self) -> GraphStats:
"""Creating current Graph Stats for the current graph"""
Expand Down
46 changes: 34 additions & 12 deletions tests/test_yedextended.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ def test_round_trip():
graph.add_node("c")
assert graph.stringify_graph() != graph_after.stringify_graph()

if os.path.exists(FILE):
os.remove(FILE)


def test_custom_property_assignment():
graph1 = yed.Graph()
Expand Down Expand Up @@ -443,9 +446,17 @@ def test_persist_graph():
file2_handle = open(file2, "r")
file1_contents = file1_handle.read()
file2_contents = file2_handle.read()
file1_handle.close()
file2_handle.close()
assert file1_contents != file2_contents
assert file2_contents.count("\n") > file1_contents.count("\n")

# Ensure cleaned up
if os.path.exists(file1):
os.remove(file1)
if os.path.exists(file2):
os.remove(file2)


def test_from_existing_graph():
assert isinstance(yed.Graph().from_existing_graph("examples\\yed_created_edges.graphml"), yed.Graph)
Expand Down Expand Up @@ -627,7 +638,6 @@ def test_init():
assert os.path.isfile(excel.TEMP_EXCEL_SHEET) is True, "Expected template created"


@pytest.mark.skip(reason="Under construction")
def test_graph_to_excel_conversion_obj():
def get_filtered_sheet_values(sheet):
data = []
Expand Down Expand Up @@ -657,6 +667,9 @@ def get_filtered_sheet_values(sheet):
reference_data = get_filtered_sheet_values(reference)
assert current_data == reference_data

if os.path.exists(excel1.TEMP_EXCEL_SHEET):
os.remove(excel1.TEMP_EXCEL_SHEET)


def test_graph_to_excel_conversion_rel():
def get_filtered_sheet_values(sheet):
Expand Down Expand Up @@ -687,20 +700,13 @@ def get_filtered_sheet_values(sheet):
reference_data = get_filtered_sheet_values(reference)
assert current_data == reference_data


@pytest.mark.skip(reason="Requires refactor of name vs id handling.")
def test_excel_to_graph(): # FIXME:
graph1 = yed.Graph().from_existing_graph("examples\\yed_created_edges.graphml")
# test conversion to excel
excel1 = yed.ExcelManager()
data = "examples\\yed_test_to_excel2.xlsx"
excel1.excel_to_graph_conversion(type="obj_and_hierarchy", excel_data=data)
excel1.excel_to_graph_conversion(type="relations", excel_data=data)
assert graph1.stringify_graph() == excel1.graph.stringify_graph()
if os.path.exists(excel1.TEMP_EXCEL_SHEET):
os.remove(excel1.TEMP_EXCEL_SHEET)


def test_bulk_data_management(): # FIXME:
graph1 = yed.Graph()

# first level nodes
graph1.add_node("a")
graph1.add_node("b")
Expand All @@ -714,12 +720,28 @@ def test_bulk_data_management(): # FIXME:
group1_1 = group1.add_group("group1_1")
group1_1.add_node("e")

# shallow copy the stringified graph
before_string = graph1.stringify_graph()[:]

graph1.manage_graph_data_in_excel()
# doing nothing in excel should lead to the same graph being built back up (when talking about simple obj model)
excel_obj = graph1.manage_graph_data_in_excel()

after_string = graph1.stringify_graph()

assert before_string is not None
assert after_string is not None
assert before_string == after_string

if os.path.exists(excel_obj.TEMP_EXCEL_SHEET):
os.remove(excel_obj.TEMP_EXCEL_SHEET)


@pytest.mark.skip(reason="Requires refactor of name vs id handling.")
def test_excel_to_graph(): # FIXME:
graph1 = yed.Graph().from_existing_graph("examples\\yed_created_edges.graphml")
# test conversion to excel
excel1 = yed.ExcelManager()
data = "examples\\yed_test_to_excel2.xlsx"
excel1.excel_to_graph_conversion(type="obj_and_hierarchy", excel_data=data)
excel1.excel_to_graph_conversion(type="relations", excel_data=data)
assert graph1.stringify_graph() == excel1.graph.stringify_graph()

0 comments on commit 38909f0

Please sign in to comment.