Procházet zdrojové kódy

fixed inconsistent excel dn read between exposed types

Toan Quach před 1 rokem
rodič
revize
711adf9554

+ 2 - 2
taipy/core/data/excel.py

@@ -244,8 +244,8 @@ class ExcelDataNode(DataNode, _AbstractFileDataNode, _AbstractTabularDataNode):
         finally:
             excel_file.close()
 
-        if len(provided_sheet_names) == 1:
-            return work_books[provided_sheet_names[0]]
+        if len(user_provided_sheet_names) == 1:
+            return work_books[user_provided_sheet_names[0]]
 
         return work_books
 

+ 2 - 2
tests/core/data/test_read_excel_data_node.py

@@ -74,7 +74,7 @@ def test_read_empty_excel_with_header():
         Scope.SCENARIO,
         properties={"path": empty_excel_path, "exposed_type": MyCustomObject},
     )
-    assert len(empty_excel.read()) == 0
+    assert len(empty_excel.read()) == 1
 
 
 def test_raise_no_data_without_header():
@@ -93,7 +93,7 @@ def test_read_empty_excel_without_header():
         Scope.SCENARIO,
         properties={"path": empty_excel_path, "exposed_type": MyCustomObject, "has_header": False},
     )
-    assert len(empty_excel.read()) == 0
+    assert len(empty_excel.read()) == 1
 
 
 def test_read_multi_sheet_with_header_no_data():

+ 15 - 8
tests/core/data/test_write_single_sheet_excel_data_node.py

@@ -35,6 +35,7 @@ def cleanup(tmp_excel_file):
             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}")
 
@@ -265,15 +266,18 @@ def test_write_with_header_single_sheet_custom_exposed_type_with_sheet_name(tmp_
 
 
 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})
+    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_data = excel_dn.read()
+    assert isinstance(excel_data, dict) and "Sheet1" in excel_data.keys()
+    assert all(actual == expected for actual, expected in zip(excel_data["Sheet1"], data))
 
     excel_dn.write(None)
-    assert excel_dn.read() == []
+    excel_data = excel_dn.read()
+    assert isinstance(excel_data, dict) and "Sheet1" in excel_data.keys()
+    assert excel_data["Sheet1"] == []
 
 
 def test_write_without_header_single_sheet_custom_exposed_type_with_sheet_name(tmp_excel_file):
@@ -298,16 +302,19 @@ def test_write_without_header_single_sheet_custom_exposed_type_with_sheet_name(t
 
 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}
+        "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_data = excel_dn.read()
+    assert isinstance(excel_data, dict) and "Sheet1" in excel_data.keys()
+    assert all(actual == expected for actual, expected in zip(excel_data["Sheet1"], data))
 
     excel_dn.write(None)
-    assert excel_dn.read() == []
+    excel_data = excel_dn.read()
+    assert isinstance(excel_data, dict) and "Sheet1" in excel_data.keys()
+    assert excel_data["Sheet1"] == []
 
 
 def test_raise_write_with_sheet_name_length_mismatch(excel_file_with_sheet_name):