Browse Source

Attempt to make all the tests pass

jrobinAV 1 year ago
parent
commit
9a0489e1db

+ 79 - 74
tests/core/data/test_write_multiple_sheet_excel_data_node.py

@@ -31,7 +31,12 @@ def tmp_excel_file():
 def cleanup(tmp_excel_file):
     yield
     if os.path.exists(tmp_excel_file):
-        os.remove(tmp_excel_file)
+        try:
+            os.remove(tmp_excel_file)
+        except Exception as e:
+            from taipy.logger._taipy_logger import _TaipyLogger
+            logger = _TaipyLogger._get_logger()
+            logger.error(f"Failed to delete {tmp_excel_file}. {e}")
 
 
 @dataclasses.dataclass
@@ -161,39 +166,39 @@ def test_write_with_header_multiple_sheet_numpy_without_sheet_name(tmp_excel_fil
         assert np.array_equal(excel_dn_data[sheet_name], sheet_data[sheet_name])
 
 
-# def test_write_with_header_multiple_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode(
-#         "foo",
-#         Scope.SCENARIO,
-#         properties={"path": tmp_excel_file, "sheet_name": sheet_names, "exposed_type": MyCustomObject},
-#     )
-#     row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
-#     sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
-#
-#     excel_dn.write(sheet_data)
-#     excel_dn_data = excel_dn.read()
-#     assert len(excel_dn_data) == len(sheet_data) == 2
-#     for sheet_name in sheet_data.keys():
-#         assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
-#
-#
-# def test_write_with_header_multiple_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode(
-#         "foo",
-#         Scope.SCENARIO,
-#         properties={"path": tmp_excel_file, "exposed_type": MyCustomObject})
-#
-#     row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
-#     sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
-#
-#     excel_dn.write(sheet_data)
-#     excel_dn_data = excel_dn.read()
-#     assert len(excel_dn_data) == len(sheet_data) == 2
-#     for sheet_name in sheet_data.keys():
-#         assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
-#
+def test_write_with_header_multiple_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode(
+        "foo",
+        Scope.SCENARIO,
+        properties={"path": tmp_excel_file, "sheet_name": sheet_names, "exposed_type": MyCustomObject},
+    )
+    row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
+    sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
+
+    excel_dn.write(sheet_data)
+    excel_dn_data = excel_dn.read()
+    assert len(excel_dn_data) == len(sheet_data) == 2
+    for sheet_name in sheet_data.keys():
+        assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
+
+
+def test_write_with_header_multiple_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode(
+        "foo",
+        Scope.SCENARIO,
+        properties={"path": tmp_excel_file, "exposed_type": MyCustomObject})
+
+    row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
+    sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
+
+    excel_dn.write(sheet_data)
+    excel_dn_data = excel_dn.read()
+    assert len(excel_dn_data) == len(sheet_data) == 2
+    for sheet_name in sheet_data.keys():
+        assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
+
 
 def test_write_without_header_multiple_sheet_pandas_with_sheet_name(tmp_excel_file):
     excel_dn = ExcelDataNode(
@@ -313,46 +318,46 @@ def test_write_without_header_multiple_sheet_numpy_without_sheet_name(tmp_excel_
         assert np.array_equal(excel_dn_data[sheet_name], sheet_data[sheet_name])
 
 
-# def test_write_without_header_multiple_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode(
-#         "foo",
-#         Scope.SCENARIO,
-#         properties={
-#             "path": tmp_excel_file,
-#             "sheet_name": sheet_names,
-#             "exposed_type": MyCustomObject,
-#             "has_header": False,
-#         },
-#     )
-#
-#     row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
-#     sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
-#
-#     excel_dn.write(sheet_data)
-#     excel_dn_data = excel_dn.read()
-#     assert len(excel_dn_data) == len(sheet_data) == 2
-#     for sheet_name in sheet_data.keys():
-#         assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
-#
-#
-# def test_write_without_header_multiple_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode(
-#         "foo",
-#         Scope.SCENARIO,
-#         properties={"path": tmp_excel_file, "exposed_type": MyCustomObject, "has_header": False}
-#     )
-#
-#     row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
-#     sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
-#
-#     excel_dn.write(sheet_data)
-#     excel_dn_data = excel_dn.read()
-#     assert len(excel_dn_data) == len(sheet_data) == 2
-#     for sheet_name in sheet_data.keys():
-#         assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
-#
+def test_write_without_header_multiple_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode(
+        "foo",
+        Scope.SCENARIO,
+        properties={
+            "path": tmp_excel_file,
+            "sheet_name": sheet_names,
+            "exposed_type": MyCustomObject,
+            "has_header": False,
+        },
+    )
+
+    row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
+    sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
+
+    excel_dn.write(sheet_data)
+    excel_dn_data = excel_dn.read()
+    assert len(excel_dn_data) == len(sheet_data) == 2
+    for sheet_name in sheet_data.keys():
+        assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
+
+
+def test_write_without_header_multiple_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode(
+        "foo",
+        Scope.SCENARIO,
+        properties={"path": tmp_excel_file, "exposed_type": MyCustomObject, "has_header": False}
+    )
+
+    row_1 = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    row_2 = [MyCustomObject(0, 4, "hello"), MyCustomObject(1, 5, "abc"), MyCustomObject(2, 6, ".")]
+    sheet_data = {"Sheet1": row_1, "Sheet2": row_2}
+
+    excel_dn.write(sheet_data)
+    excel_dn_data = excel_dn.read()
+    assert len(excel_dn_data) == len(sheet_data) == 2
+    for sheet_name in sheet_data.keys():
+        assert all(actual == expected for actual, expected in zip(excel_dn_data[sheet_name], sheet_data[sheet_name]))
+
 
 @pytest.mark.skip(reason="Not implemented on pandas 1.3.5")
 @pytest.mark.parametrize(

+ 69 - 101
tests/core/data/test_write_single_sheet_excel_data_node.py

@@ -32,7 +32,12 @@ def tmp_excel_file():
 def cleanup(tmp_excel_file):
     yield
     if os.path.exists(tmp_excel_file):
-        os.remove(tmp_excel_file)
+        try:
+            os.remove(tmp_excel_file)
+        except Exception as e:
+            from taipy.logger._taipy_logger import _TaipyLogger
+            logger = _TaipyLogger._get_logger()
+            logger.error(f"Failed to delete {tmp_excel_file}. {e}")
 
 
 @dataclasses.dataclass
@@ -51,43 +56,6 @@ class MyCustomObject:
                 setattr(self, field.name, field.type(value))
 
 
-@contextlib.contextmanager
-def load_worksheet_with_close(filename, *args, **kwargs):
-    '''
-    Open an openpyxl worksheet and automatically close it when finished.
-    '''
-    import tempfile
-
-    import openpyxl
-
-    wb = openpyxl.load_workbook(filename, *args, **kwargs)
-    yield wb
-    # Create path in temporary directory and write workbook there to force
-    # it to close
-    path = os.path.join(tempfile.gettempdir(), os.path.basename(filename))
-    wb.save(path)
-    os.remove(path)
-
-
-def test_fail():
-    path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample", "temp2.xlsx")
-
-    from taipy.logger._taipy_logger import _TaipyLogger
-    logger = _TaipyLogger._get_logger()
-
-    import openpyxl
-    file = openpyxl.load_workbook(path)
-    file.close()
-
-    if os.path.exists(path):
-        logger.warning(f"trying to delete {path}.")
-        try:
-            os.remove(path)
-            logger.warning(f"{path} has been deleted.")
-        except Exception as e:
-            logger.error(f"Failed to delete {path}. {e}")
-
-
 def test_write_with_header_single_sheet_pandas_with_sheet_name(tmp_excel_file):
     excel_dn = ExcelDataNode("foo", Scope.SCENARIO, properties={"path": tmp_excel_file, "sheet_name": "Sheet1"})
 
@@ -279,69 +247,69 @@ def test_write_without_header_single_sheet_numpy_without_sheet_name(tmp_excel_fi
     assert excel_dn.read()["Sheet1"].size == 0
 
 
-# def test_write_with_header_single_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file_2):
-#     excel_dn = ExcelDataNode(
-#         "foo",
-#         Scope.SCENARIO,
-#         properties={"path": tmp_excel_file_2, "sheet_name": "Sheet1", "exposed_type": MyCustomObject},
-#     )
-#     expected_data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#
-#     excel_dn.write(expected_data)
-#     actual_data = excel_dn.read()
-#
-#     assert all(actual == expected for actual, expected in zip(actual_data, expected_data))
-#
-#     excel_dn.write(None)
-#     actual_data = excel_dn.read()
-#     assert actual_data == []
-#
-#
-# def test_write_with_header_single_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode("foo", Scope.SCENARIO,
-#     properties={"path": tmp_excel_file, "exposed_type": MyCustomObject})
-#
-#     data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     excel_dn.write(data)
-#     assert all(actual == expected for actual, expected in zip(excel_dn.read(), data))
-#
-#     excel_dn.write(None)
-#     assert excel_dn.read() == []
-#
-#
-# def test_write_without_header_single_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode(
-#         "foo",
-#         Scope.SCENARIO,
-#         properties={
-#             "path": tmp_excel_file,
-#             "sheet_name": "Sheet1",
-#             "exposed_type": MyCustomObject,
-#             "has_header": False,
-#         },
-#     )
-#
-#     data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     excel_dn.write(data)
-#     assert all(actual == expected for actual, expected in zip(excel_dn.read(), data))
-#
-#     excel_dn.write(None)
-#     assert excel_dn.read() == []
-#
-#
-# def test_write_without_header_single_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
-#     excel_dn = ExcelDataNode(
-#         "foo", Scope.SCENARIO,
-#         properties={"path": tmp_excel_file, "exposed_type": MyCustomObject, "has_header": False}
-#     )
-#
-#     data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
-#     excel_dn.write(data)
-#     assert all(actual == expected for actual, expected in zip(excel_dn.read(), data))
-#
-#     excel_dn.write(None)
-#     assert excel_dn.read() == []
-#
+def test_write_with_header_single_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file_2):
+    excel_dn = ExcelDataNode(
+        "foo",
+        Scope.SCENARIO,
+        properties={"path": tmp_excel_file_2, "sheet_name": "Sheet1", "exposed_type": MyCustomObject},
+    )
+    expected_data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+
+    excel_dn.write(expected_data)
+    actual_data = excel_dn.read()
+
+    assert all(actual == expected for actual, expected in zip(actual_data, expected_data))
+
+    excel_dn.write(None)
+    actual_data = excel_dn.read()
+    assert actual_data == []
+
+
+def test_write_with_header_single_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode("foo", Scope.SCENARIO,
+    properties={"path": tmp_excel_file, "exposed_type": MyCustomObject})
+
+    data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    excel_dn.write(data)
+    assert all(actual == expected for actual, expected in zip(excel_dn.read(), data))
+
+    excel_dn.write(None)
+    assert excel_dn.read() == []
+
+
+def test_write_without_header_single_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode(
+        "foo",
+        Scope.SCENARIO,
+        properties={
+            "path": tmp_excel_file,
+            "sheet_name": "Sheet1",
+            "exposed_type": MyCustomObject,
+            "has_header": False,
+        },
+    )
+
+    data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    excel_dn.write(data)
+    assert all(actual == expected for actual, expected in zip(excel_dn.read(), data))
+
+    excel_dn.write(None)
+    assert excel_dn.read() == []
+
+
+def test_write_without_header_single_sheet_custom_exposed_type_without_sheet_name(tmp_excel_file):
+    excel_dn = ExcelDataNode(
+        "foo", Scope.SCENARIO,
+        properties={"path": tmp_excel_file, "exposed_type": MyCustomObject, "has_header": False}
+    )
+
+    data = [MyCustomObject(0, 1, "hi"), MyCustomObject(1, 2, "world"), MyCustomObject(2, 3, "text")]
+    excel_dn.write(data)
+    assert all(actual == expected for actual, expected in zip(excel_dn.read(), data))
+
+    excel_dn.write(None)
+    assert excel_dn.read() == []
+
 
 def test_raise_write_with_sheet_name_length_mismatch(excel_file_with_sheet_name):
     excel_dn = ExcelDataNode(