-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored 'on_invalid_data' to '_convert_unsupported_data()'
Adapted tests accordingly
- Loading branch information
1 parent
5b8bf15
commit f7ab246
Showing
6 changed files
with
208 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from unittest.mock import MagicMock | ||
from taipy.gui import Gui | ||
from taipy.gui.data.data_accessor import ( | ||
_DataAccessor, | ||
_DataAccessors, | ||
_InvalidDataAccessor, | ||
) | ||
from taipy.gui.data.data_format import _DataFormat | ||
from taipy.gui.utils.types import _TaipyData | ||
from unittest.mock import Mock | ||
import typing as t | ||
|
||
|
||
class MyDataAccessor(_DataAccessor): | ||
@staticmethod | ||
def get_supported_classes() -> t.List[t.Type]: | ||
return [int] | ||
|
||
def get_data( | ||
self, | ||
var_name: str, | ||
value: t.Any, | ||
payload: t.Dict[str, t.Any], | ||
data_format: _DataFormat, | ||
) -> t.Dict[str, t.Any]: | ||
return {"value": 2*int(value)} | ||
|
||
def get_col_types(self, var_name: str, value: t.Any) -> t.Dict[str, str]: # type: ignore | ||
pass | ||
|
||
def to_pandas(self, value: t.Any) -> t.Union[t.List[t.Any], t.Any]: | ||
pass | ||
|
||
def on_edit(self, value: t.Any, payload: t.Dict[str, t.Any]) -> t.Optional[t.Any]: | ||
pass | ||
|
||
def on_delete(self, value: t.Any, payload: t.Dict[str, t.Any]) -> t.Optional[t.Any]: | ||
pass | ||
|
||
def on_add( | ||
self, | ||
value: t.Any, | ||
payload: t.Dict[str, t.Any], | ||
new_row: t.Optional[t.List[t.Any]] = None, | ||
) -> t.Optional[t.Any]: | ||
pass | ||
|
||
def to_csv(self, var_name: str, value: t.Any) -> t.Optional[str]: | ||
pass | ||
|
||
def mock_taipy_data(value): | ||
"""Helper to mock _TaipyData objects.""" | ||
mock_data = Mock(spec=_TaipyData) | ||
mock_data.get.return_value = value | ||
return mock_data | ||
|
||
|
||
def test_custom_accessor(gui: Gui): | ||
"""Test if get_data() uses the correct accessor.""" | ||
data_accessors = _DataAccessors(gui) | ||
data = mock_taipy_data(123) | ||
|
||
# Testing when accessor is not registered | ||
data_accessor = data_accessors._DataAccessors__get_instance(mock_taipy_data) # type: ignore | ||
assert isinstance(data_accessor, _InvalidDataAccessor), f"Expected _InvalidDataAccessor but got {type(data_accessor)}" | ||
result = data_accessors.get_data("var_name", data, {}) | ||
assert result == {} | ||
|
||
# Testing when accessor is not registered | ||
data_accessors._register(MyDataAccessor) | ||
|
||
result = data_accessors.get_data("var_name", data, {}) | ||
assert isinstance(result, dict) | ||
assert result["value"] == 246 | ||
|
||
data_accessors._unregister(MyDataAccessor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import logging | ||
|
||
import pytest | ||
|
||
from taipy.gui import Gui | ||
|
||
|
||
def test_handle_invalid_data_no_callback(): | ||
result = Gui._convert_unsupported_data("invalid_data") | ||
assert result is None | ||
|
||
|
||
|
||
def test_unsupported_data_converter_returns_none(): | ||
def convert(value): | ||
return None # Simulates a failed transformation | ||
|
||
Gui.set_unsupported_data_converter(convert) | ||
|
||
result = Gui._convert_unsupported_data("invalid_data") | ||
assert result is None | ||
|
||
# Reset converter | ||
Gui.set_unsupported_data_converter(None) | ||
|
||
|
||
def test_unsupported_data_converter_applied(): | ||
def convert(value): | ||
return "converted" # Successful transformation | ||
|
||
Gui.set_unsupported_data_converter(convert) | ||
|
||
result = Gui._convert_unsupported_data("raw data") | ||
assert result == "converted" | ||
|
||
# Reset converter | ||
Gui.set_unsupported_data_converter(None) | ||
|
||
|
||
def test_unsupported_data_converter_raises_exception(capfd, monkeypatch): | ||
def convert(value): | ||
raise ValueError("Conversion failure") # Simulate an error | ||
|
||
def mock_warn(message: str): | ||
logging.warning(message) # Ensure the warning goes to stderr. | ||
# Patch the _warn function inside the taipy.gui._warnings module. | ||
monkeypatch.setattr("taipy.gui._warnings._warn", mock_warn) | ||
|
||
Gui.set_unsupported_data_converter(convert) | ||
|
||
result = Gui._convert_unsupported_data("raw data") | ||
|
||
out, _ = capfd.readouterr() | ||
|
||
assert result is None # Should return None on exception | ||
assert "Error transforming data: Transformation error" | ||
|
||
# Reset converter | ||
Gui.set_unsupported_data_converter(None) | ||
|
||
|
||
@pytest.mark.parametrize("input_data", [None, 123, [], {}, set()]) | ||
def test_unsupported_data_converter_with_various_inputs(input_data): | ||
def convert(value): | ||
return "converted" # Always returns valid data | ||
|
||
Gui.set_unsupported_data_converter(convert) | ||
|
||
result = Gui._convert_unsupported_data(input_data) | ||
assert result == "converted" # Transformed correctly for all inputs | ||
|
||
# Reset converter | ||
Gui.set_unsupported_data_converter(None) |
Oops, something went wrong.