Skip to content

Commit

Permalink
Testing get_instance method of data_accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ujj1225 committed Oct 15, 2024
1 parent 2903c5e commit ce77584
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/gui/gui_specific/test_get_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
from taipy.gui.data.data_accessor import _DataAccessors, _InvalidDataAccessor
from taipy.gui import Gui


def test__get_instance_with_valid_data(gui: Gui):
"""Test if __get_instance returns the correct accessor for valid data."""
data_accessors = _DataAccessors(gui)

# Simulate valid data being passed
data_accessors._DataAccessors__access_4_type = {int: "valid_accessor"} # type: ignore

result = data_accessors._DataAccessors__get_instance(123) # type: ignore
assert result == "valid_accessor" # Valid accessor should be returned


def test__get_instance_with_invalid_data(gui: Gui):
"""Test if __get_instance handles invalid data correctly."""
data_accessors = _DataAccessors(gui)

# No valid data accessor, should return None
gui.handle_invalid_data = lambda x: None # type: ignore
result = data_accessors._DataAccessors__get_instance("invalid_data") # type: ignore

assert isinstance(result, _InvalidDataAccessor) # Should return InvalidDataAccessor


def test__get_instance_transformation_successful(gui: Gui):
"""Test if __get_instance transforms invalid data correctly when callback is set."""
data_accessors = _DataAccessors(gui)

# Mock invalid data transformation to return valid data
gui.handle_invalid_data = lambda x: 123 # type: ignore # Transformation success
data_accessors._DataAccessors__access_4_type = {int: "valid_accessor"} # type: ignore

result = data_accessors._DataAccessors__get_instance("invalid_data") # type: ignore
assert (
result == "valid_accessor"
) # Transformed data should now return the valid accessor

0 comments on commit ce77584

Please sign in to comment.