Repository: Avaiga/taipy
File: tests/core/data/test_data_manager.py (Python)

Code before:
from taipy.core.data.in_memory import InMemoryDataNode
from taipy.core.data.pickle import PickleDataNode
from taipy.core.exceptions.exceptions import InvalidDataNodeType, ModelNotFound
from tests.core.utils.named_temporary_file import NamedTemporaryFile


def file_exists(file_path: str) -> bool:
    return os.path.exists(file_path)


class TestDataManager:
    def test_create_data_node_and_modify_properties_does_not_modify_config(self):
        dn_config = Config.configure_data_node(id="name", foo="bar")
        dn = _DataManager._create_and_set(dn_config, None, None)
        assert dn_config.properties.get("foo") == "bar"
        assert dn_config.properties.get("baz") is None

        dn.properties["baz"] = "qux"
        _DataManager._set(dn)
        assert dn_config.properties.get("foo") == "bar"
        assert dn_config.properties.get("baz") is None
        assert dn.properties.get("foo") == "bar"
        assert dn.properties.get("baz") == "qux"

    def test_can_create(self):
        dn_config = Config.configure_in_memory_data_node("dn", 10, scope=Scope.SCENARIO)
        global_dn_config = Config.configure_data_node(
            id="global_dn", storage_type="in_memory", scope=Scope.GLOBAL, data=10
        )

        reasons = _DataManager._can_create(global_dn_config)
        assert bool(reasons) is True
        assert reasons._reasons == {}

        reasons = _DataManager._can_create(dn_config)
        assert bool(reasons) is False
        assert reasons._reasons == {dn_config.id: {'Data node config "dn" does not have GLOBAL scope'}}

        reasons = _DataManager._can_create(1)
        assert bool(reasons) is False
        assert reasons._reasons == {"1": {'Object "1" is not a valid config to be created'}}

    def test_create_data_node_with_name_provided(self):
        dn_config = Config.configure_data_node(id="dn", foo="bar", name="acb")
        dn = _DataManager._create_and_set(dn_config, None, None)
        assert dn.name == "acb"

    def test_create_and_get_csv_data_node(self):
        # Test we can instantiate a CsvDataNode from DataNodeConfig with :
        # - a csv type
        # - a default scenario scope

Review comment (suggestion):
```suggestion
        dn_config = Config.configure_data_node("dn", 10, scope=Scope.SCENARIO)
```

Code after:
from taipy.core.data.in_memory import InMemoryDataNode
from taipy.core.data.pickle import PickleDataNode
from taipy.core.exceptions.exceptions import InvalidDataNodeType, ModelNotFound
from tests.core.utils.named_temporary_file import NamedTemporaryFile


def file_exists(file_path: str) -> bool:
    return os.path.exists(file_path)


class TestDataManager:
    def test_create_data_node_and_modify_properties_does_not_modify_config(self):
        dn_config = Config.configure_data_node(id="name", foo="bar")
        dn = _DataManager._create_and_set(dn_config, None, None)
        assert dn_config.properties.get("foo") == "bar"
        assert dn_config.properties.get("baz") is None

        dn.properties["baz"] = "qux"
        _DataManager._set(dn)
        assert dn_config.properties.get("foo") == "bar"
        assert dn_config.properties.get("baz") is None
        assert dn.properties.get("foo") == "bar"
        assert dn.properties.get("baz") == "qux"

    def test_can_create(self):
        dn_config = Config.configure_data_node("dn", 10, scope=Scope.SCENARIO)
        global_dn_config = Config.configure_data_node(
            id="global_dn", storage_type="in_memory", scope=Scope.GLOBAL, data=10
        )

        reasons = _DataManager._can_create()
        assert bool(reasons) is True
        assert reasons._reasons == {}

        reasons = _DataManager._can_create(global_dn_config)
        assert bool(reasons) is True
        assert reasons._reasons == {}

        reasons = _DataManager._can_create(dn_config)
        assert bool(reasons) is False
        assert reasons._reasons == {dn_config.id: {'Data node config "dn" does not have GLOBAL scope'}}

        reasons = _DataManager._can_create(1)
        assert bool(reasons) is False
        assert reasons._reasons == {"1": {'Object "1" must be a valid DataNodeConfig'}}

    def test_create_data_node_with_name_provided(self):
        dn_config = Config.configure_data_node(id="dn", foo="bar", name="acb")
        dn = _DataManager._create_and_set(dn_config, None, None)
        assert dn.name == "acb"