test_json_data_node.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import datetime
  12. import json
  13. import os
  14. import pathlib
  15. import re
  16. import uuid
  17. from dataclasses import dataclass
  18. from enum import Enum
  19. from time import sleep
  20. import freezegun
  21. import numpy as np
  22. import pandas as pd
  23. import pytest
  24. from taipy import Scope
  25. from taipy.common.config import Config
  26. from taipy.common.config.exceptions.exceptions import InvalidConfigurationId
  27. from taipy.core.common._utils import _normalize_path
  28. from taipy.core.data._data_manager import _DataManager
  29. from taipy.core.data._data_manager_factory import _DataManagerFactory
  30. from taipy.core.data.data_node_id import DataNodeId
  31. from taipy.core.data.json import JSONDataNode
  32. from taipy.core.data.operator import JoinOperator, Operator
  33. from taipy.core.exceptions.exceptions import NoData
  34. from taipy.core.reason import NoFileToDownload, NotAFile
  35. @pytest.fixture(scope="function", autouse=True)
  36. def cleanup():
  37. yield
  38. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/temp.json")
  39. if os.path.isfile(path):
  40. os.remove(path)
  41. class MyCustomObject:
  42. def __init__(self, id, integer, text):
  43. self.id = id
  44. self.integer = integer
  45. self.text = text
  46. class MyCustomObject2:
  47. def __init__(self, id, boolean, text):
  48. self.id = id
  49. self.boolean = boolean
  50. self.text = text
  51. class MyEnum(Enum):
  52. A = 1
  53. B = 2
  54. C = 3
  55. @dataclass
  56. class CustomDataclass:
  57. integer: int
  58. string: str
  59. class MyCustomEncoder(json.JSONEncoder):
  60. def default(self, o):
  61. if isinstance(o, MyCustomObject):
  62. return {"__type__": "MyCustomObject", "id": o.id, "integer": o.integer, "text": o.text}
  63. return super().default(self, o)
  64. class MyCustomDecoder(json.JSONDecoder):
  65. def __init__(self, *args, **kwargs):
  66. super().__init__(*args, **kwargs, object_hook=self.object_hook)
  67. def object_hook(self, o):
  68. if o.get("__type__") == "MyCustomObject":
  69. return MyCustomObject(o["id"], o["integer"], o["text"])
  70. else:
  71. return o
  72. class TestJSONDataNode:
  73. def test_create(self):
  74. path = "data/node/path"
  75. json_dn_config = Config.configure_json_data_node(id="foo_bar", default_path=path, name="super name")
  76. dn_1 = _DataManagerFactory._build_manager()._create(json_dn_config, None, None)
  77. assert isinstance(dn_1, JSONDataNode)
  78. assert dn_1.storage_type() == "json"
  79. assert dn_1.config_id == "foo_bar"
  80. assert dn_1.name == "super name"
  81. assert dn_1.scope == Scope.SCENARIO
  82. assert dn_1.id is not None
  83. assert dn_1.owner_id is None
  84. assert dn_1.last_edit_date is None
  85. assert dn_1.job_ids == []
  86. assert not dn_1.is_ready_for_reading
  87. assert dn_1.path == path
  88. json_dn_config_2 = Config.configure_json_data_node(id="foo", default_path=path, encoding="utf-16")
  89. dn_2 = _DataManagerFactory._build_manager()._create(json_dn_config_2, None, None)
  90. assert isinstance(dn_2, JSONDataNode)
  91. assert dn_2.storage_type() == "json"
  92. assert dn_2.properties["encoding"] == "utf-16"
  93. json_dn_config_3 = Config.configure_json_data_node(
  94. id="foo", default_path=path, encoder=MyCustomEncoder, decoder=MyCustomDecoder
  95. )
  96. dn_3 = _DataManagerFactory._build_manager()._create(json_dn_config_3, None, None)
  97. assert isinstance(dn_3, JSONDataNode)
  98. assert dn_3.storage_type() == "json"
  99. assert dn_3.properties["encoder"] == MyCustomEncoder
  100. assert dn_3.encoder == MyCustomEncoder
  101. assert dn_3.properties["decoder"] == MyCustomDecoder
  102. assert dn_3.decoder == MyCustomDecoder
  103. with pytest.raises(InvalidConfigurationId):
  104. _ = JSONDataNode(
  105. "foo bar", Scope.SCENARIO, properties={"default_path": path, "has_header": False, "name": "super name"}
  106. )
  107. def test_get_user_properties(self, json_file):
  108. dn_1 = JSONDataNode("dn_1", Scope.SCENARIO, properties={"path": json_file})
  109. assert dn_1._get_user_properties() == {}
  110. dn_2 = JSONDataNode(
  111. "dn_2",
  112. Scope.SCENARIO,
  113. properties={
  114. "default_data": "foo",
  115. "default_path": json_file,
  116. "encoder": MyCustomEncoder,
  117. "decoder": MyCustomDecoder,
  118. "foo": "bar",
  119. },
  120. )
  121. # default_data, default_path, path, encoder, decoder are filtered out
  122. assert dn_2._get_user_properties() == {"foo": "bar"}
  123. def test_new_json_data_node_with_existing_file_is_ready_for_reading(self):
  124. not_ready_dn_cfg = Config.configure_data_node(
  125. "not_ready_data_node_config_id", "json", default_path="NOT_EXISTING.json"
  126. )
  127. not_ready_dn = _DataManager._bulk_get_or_create([not_ready_dn_cfg])[not_ready_dn_cfg]
  128. assert not not_ready_dn.is_ready_for_reading
  129. assert not_ready_dn.path == "NOT_EXISTING.json"
  130. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_list.json")
  131. ready_dn_cfg = Config.configure_data_node("ready_data_node_config_id", "json", default_path=path)
  132. ready_dn = _DataManager._bulk_get_or_create([ready_dn_cfg])[ready_dn_cfg]
  133. assert ready_dn.is_ready_for_reading
  134. def test_read_non_existing_json(self):
  135. not_existing_json = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": "WRONG.json"})
  136. assert not_existing_json.read() is None
  137. with pytest.raises(NoData):
  138. _DataManagerFactory._build_manager()._read(not_existing_json)
  139. with pytest.raises(NoData):
  140. not_existing_json.read_or_raise()
  141. def test_read(self):
  142. path_1 = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_list.json")
  143. dn_1 = JSONDataNode("bar", Scope.SCENARIO, properties={"default_path": path_1})
  144. data_1 = dn_1.read()
  145. assert isinstance(data_1, list)
  146. assert len(data_1) == 4
  147. path_2 = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_dict.json")
  148. dn_2 = JSONDataNode("bar", Scope.SCENARIO, properties={"default_path": path_2})
  149. data_2 = dn_2.read()
  150. assert isinstance(data_2, dict)
  151. assert data_2["id"] == "1"
  152. path_3 = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_int.json")
  153. dn_3 = JSONDataNode("bar", Scope.SCENARIO, properties={"default_path": path_3})
  154. data_3 = dn_3.read()
  155. assert isinstance(data_3, int)
  156. assert data_3 == 1
  157. path_4 = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_null.json")
  158. dn_4 = JSONDataNode("bar", Scope.SCENARIO, properties={"default_path": path_4})
  159. data_4 = dn_4.read()
  160. assert data_4 is None
  161. def test_read_invalid_json(self):
  162. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/invalid.json.txt")
  163. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": path})
  164. with pytest.raises(ValueError):
  165. dn.read()
  166. def test_append_to_list(self, json_file):
  167. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  168. _DataManagerFactory._build_manager()._repository._save(json_dn)
  169. original_data = json_dn.read()
  170. # Append a dictionary
  171. append_data_1 = {"a": 1, "b": 2, "c": 3}
  172. json_dn.append(append_data_1)
  173. assert json_dn.read() == original_data + [append_data_1]
  174. # Append a list of dictionaries
  175. append_data_data_2 = [{"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}]
  176. json_dn.append(append_data_data_2)
  177. assert json_dn.read() == original_data + [append_data_1] + append_data_data_2
  178. def test_append_to_a_dictionary(self, json_file):
  179. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  180. _DataManagerFactory._build_manager()._repository._save(json_dn)
  181. original_data = {"a": 1, "b": 2, "c": 3}
  182. json_dn.write(original_data)
  183. # Append another dictionary
  184. append_data_1 = {"d": 1, "e": 2, "f": 3}
  185. json_dn.append(append_data_1)
  186. assert json_dn.read() == {**original_data, **append_data_1}
  187. # Append an overlap dictionary
  188. append_data_data_2 = {"a": 10, "b": 20, "g": 30}
  189. json_dn.append(append_data_data_2)
  190. assert json_dn.read() == {**original_data, **append_data_1, **append_data_data_2}
  191. def test_write(self, json_file):
  192. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  193. _DataManagerFactory._build_manager()._repository._save(json_dn)
  194. data = {"a": 1, "b": 2, "c": 3}
  195. json_dn.write(data)
  196. assert np.array_equal(json_dn.read(), data)
  197. def test_write_with_different_encoding(self, json_file):
  198. data = {"≥a": 1, "b": 2}
  199. utf8_dn = JSONDataNode("utf8_dn", Scope.SCENARIO, properties={"default_path": json_file})
  200. _DataManagerFactory._build_manager()._repository._save(utf8_dn)
  201. utf16_dn = JSONDataNode(
  202. "utf16_dn", Scope.SCENARIO, properties={"default_path": json_file, "encoding": "utf-16"}
  203. )
  204. _DataManagerFactory._build_manager()._repository._save(utf16_dn)
  205. # If a file is written with utf-8 encoding, it can only be read with utf-8, not utf-16 encoding
  206. utf8_dn.write(data)
  207. assert np.array_equal(utf8_dn.read(), data)
  208. with pytest.raises(UnicodeError):
  209. utf16_dn.read()
  210. # If a file is written with utf-16 encoding, it can only be read with utf-16, not utf-8 encoding
  211. utf16_dn.write(data)
  212. assert np.array_equal(utf16_dn.read(), data)
  213. with pytest.raises(UnicodeError):
  214. utf8_dn.read()
  215. def test_write_non_serializable(self, json_file):
  216. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  217. data = {"a": 1, "b": json_dn}
  218. with pytest.raises(TypeError):
  219. json_dn.write(data)
  220. def test_write_date(self, json_file):
  221. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  222. _DataManagerFactory._build_manager()._repository._save(json_dn)
  223. now = datetime.datetime.now()
  224. data = {"date": now}
  225. json_dn.write(data)
  226. read_data = json_dn.read()
  227. assert read_data["date"] == now
  228. def test_write_enum(self, json_file):
  229. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  230. _DataManagerFactory._build_manager()._repository._save(json_dn)
  231. data = [MyEnum.A, MyEnum.B, MyEnum.C]
  232. json_dn.write(data)
  233. read_data = json_dn.read()
  234. assert read_data == [MyEnum.A, MyEnum.B, MyEnum.C]
  235. def test_write_dataclass(self, json_file):
  236. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  237. _DataManagerFactory._build_manager()._repository._save(json_dn)
  238. json_dn.write(CustomDataclass(integer=1, string="foo"))
  239. read_data = json_dn.read()
  240. assert read_data.integer == 1
  241. assert read_data.string == "foo"
  242. def test_write_custom_encoder(self, json_file):
  243. json_dn = JSONDataNode(
  244. "foo", Scope.SCENARIO, properties={"default_path": json_file, "encoder": MyCustomEncoder}
  245. )
  246. _DataManagerFactory._build_manager()._repository._save(json_dn)
  247. data = [MyCustomObject("1", 1, "abc"), 100]
  248. json_dn.write(data)
  249. read_data = json_dn.read()
  250. assert read_data[0]["__type__"] == "MyCustomObject"
  251. assert read_data[0]["id"] == "1"
  252. assert read_data[0]["integer"] == 1
  253. assert read_data[0]["text"] == "abc"
  254. assert read_data[1] == 100
  255. def test_read_write_custom_encoder_decoder(self, json_file):
  256. json_dn = JSONDataNode(
  257. "foo",
  258. Scope.SCENARIO,
  259. properties={"default_path": json_file, "encoder": MyCustomEncoder, "decoder": MyCustomDecoder},
  260. )
  261. _DataManagerFactory._build_manager()._repository._save(json_dn)
  262. data = [MyCustomObject("1", 1, "abc"), 100]
  263. json_dn.write(data)
  264. read_data = json_dn.read()
  265. assert isinstance(read_data[0], MyCustomObject)
  266. assert read_data[0].id == "1"
  267. assert read_data[0].integer == 1
  268. assert read_data[0].text == "abc"
  269. assert read_data[1] == 100
  270. def test_filter(self, json_file):
  271. json_dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": json_file})
  272. _DataManagerFactory._build_manager()._repository._save(json_dn)
  273. json_dn.write(
  274. [
  275. {"foo": 1, "bar": 1},
  276. {"foo": 1, "bar": 2},
  277. {"foo": 1},
  278. {"foo": 2, "bar": 2},
  279. {"bar": 2},
  280. {"KWARGS_KEY": "KWARGS_VALUE"},
  281. ]
  282. )
  283. assert len(json_dn.filter(("foo", 1, Operator.EQUAL))) == 3
  284. assert len(json_dn.filter(("foo", 1, Operator.NOT_EQUAL))) == 3
  285. assert len(json_dn.filter(("bar", 2, Operator.EQUAL))) == 3
  286. assert len(json_dn.filter([("bar", 1, Operator.EQUAL), ("bar", 2, Operator.EQUAL)], JoinOperator.OR)) == 4
  287. assert json_dn[0] == {"foo": 1, "bar": 1}
  288. assert json_dn[2] == {"foo": 1}
  289. assert json_dn[:2] == [{"foo": 1, "bar": 1}, {"foo": 1, "bar": 2}]
  290. @pytest.mark.parametrize(
  291. ["properties", "exists"],
  292. [
  293. ({"default_data": {"foo": "bar"}}, True),
  294. ({}, False),
  295. ],
  296. )
  297. def test_create_with_default_data(self, properties, exists):
  298. dn = JSONDataNode("foo", Scope.SCENARIO, DataNodeId(f"dn_id_{uuid.uuid4()}"), properties=properties)
  299. assert dn.path == f"{Config.core.storage_folder}jsons/{dn.id}.json"
  300. assert os.path.exists(dn.path) is exists
  301. def test_set_path(self):
  302. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": "foo.json"})
  303. _DataManagerFactory._build_manager()._repository._save(dn)
  304. assert dn.path == "foo.json"
  305. dn.path = "bar.json"
  306. assert dn.path == "bar.json"
  307. def test_read_write_after_modify_path(self):
  308. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_dict.json")
  309. new_path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/temp.json")
  310. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"default_path": path})
  311. _DataManagerFactory._build_manager()._repository._save(dn)
  312. read_data = dn.read()
  313. assert read_data is not None
  314. dn.path = new_path
  315. with pytest.raises(FileNotFoundError):
  316. dn.read()
  317. dn.write({"other": "stuff"})
  318. assert dn.read() == {"other": "stuff"}
  319. def test_get_system_modified_date_instead_of_last_edit_date(self, tmpdir_factory):
  320. temp_file_path = str(tmpdir_factory.mktemp("data").join("temp.json"))
  321. pd.DataFrame([]).to_json(temp_file_path)
  322. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": temp_file_path})
  323. _DataManagerFactory._build_manager()._repository._save(dn)
  324. dn.write([1, 2, 3])
  325. previous_edit_date = dn.last_edit_date
  326. sleep(0.1)
  327. pd.DataFrame([4, 5, 6]).to_json(temp_file_path)
  328. new_edit_date = datetime.datetime.fromtimestamp(os.path.getmtime(temp_file_path))
  329. assert previous_edit_date < dn.last_edit_date
  330. assert new_edit_date == dn.last_edit_date
  331. sleep(0.1)
  332. dn.write([1, 2, 3])
  333. assert new_edit_date < dn.last_edit_date
  334. os.unlink(temp_file_path)
  335. def test_migrate_to_new_path(self, tmp_path):
  336. _base_path = os.path.join(tmp_path, ".data")
  337. path = os.path.join(_base_path, "test.json")
  338. # create a file on old path
  339. os.mkdir(_base_path)
  340. with open(path, "w"):
  341. pass
  342. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": path})
  343. assert ".data" not in dn.path
  344. assert os.path.exists(dn.path)
  345. def test_is_downloadable(self):
  346. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_dict.json")
  347. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": path})
  348. reasons = dn.is_downloadable()
  349. assert reasons
  350. assert reasons.reasons == ""
  351. def test_is_not_downloadable_no_file(self):
  352. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/wrong_path.json")
  353. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": path})
  354. reasons = dn.is_downloadable()
  355. assert not reasons
  356. assert len(reasons._reasons) == 1
  357. assert str(NoFileToDownload(_normalize_path(path), dn.id)) in reasons.reasons
  358. def is_not_downloadable_not_a_file(self):
  359. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json")
  360. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": path})
  361. reasons = dn.is_downloadable()
  362. assert not reasons
  363. assert len(reasons._reasons) == 1
  364. assert str(NotAFile(_normalize_path(path), dn.id)) in reasons.reasons
  365. def test_get_download_path(self):
  366. path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data_sample/json/example_dict.json")
  367. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": path})
  368. assert re.split(r"[\\/]", dn._get_downloadable_path()) == re.split(r"[\\/]", path)
  369. def test_get_download_path_with_not_existed_file(self):
  370. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": "NOT_EXISTED.json"})
  371. assert dn._get_downloadable_path() == ""
  372. def test_upload(self, json_file, tmpdir_factory):
  373. old_json_path = tmpdir_factory.mktemp("data").join("df.json").strpath
  374. old_data = [{"a": 0, "b": 1, "c": 2}, {"a": 3, "b": 4, "c": 5}]
  375. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": old_json_path})
  376. _DataManagerFactory._build_manager()._repository._save(dn)
  377. dn.write(old_data)
  378. old_last_edit_date = dn.last_edit_date
  379. with open(json_file, "r") as f:
  380. upload_content = json.load(f)
  381. with freezegun.freeze_time(old_last_edit_date + datetime.timedelta(seconds=1)):
  382. dn._upload(json_file)
  383. assert dn.read() == upload_content # The content of the dn should change to the uploaded content
  384. assert dn.last_edit_date > old_last_edit_date
  385. assert dn.path == _normalize_path(old_json_path) # The path of the dn should not change
  386. def test_upload_with_upload_check(self, json_file, tmpdir_factory):
  387. old_json_path = tmpdir_factory.mktemp("data").join("df.json").strpath
  388. old_data = [{"a": 0, "b": 1, "c": 2}, {"a": 3, "b": 4, "c": 5}]
  389. dn = JSONDataNode("foo", Scope.SCENARIO, properties={"path": old_json_path})
  390. _DataManagerFactory._build_manager()._repository._save(dn)
  391. dn.write(old_data)
  392. old_last_edit_date = dn.last_edit_date
  393. def check_data_keys(upload_path, upload_data):
  394. all_column_is_abc = all(data.keys() == {"a", "b", "c"} for data in upload_data)
  395. return upload_path.endswith(".json") and all_column_is_abc
  396. not_exists_json_path = tmpdir_factory.mktemp("data").join("not_exists.json").strpath
  397. reasons = dn._upload(not_exists_json_path, upload_checker=check_data_keys)
  398. assert bool(reasons) is False
  399. assert (
  400. str(list(reasons._reasons[dn.id])[0]) == "The uploaded file 'not_exists.json' can not be read,"
  401. f" therefore is not a valid data file for data node '{dn.id}'"
  402. )
  403. not_json_path = tmpdir_factory.mktemp("data").join("wrong_format_df.not_json").strpath
  404. with open(not_json_path, "w") as f:
  405. json.dump([{"a": 1, "b": 2, "d": 3}, {"a": 4, "b": 5, "d": 6}], f)
  406. # The upload should fail when the file is not a json
  407. reasons = dn._upload(not_json_path, upload_checker=check_data_keys)
  408. assert bool(reasons) is False
  409. assert (
  410. str(list(reasons._reasons[dn.id])[0])
  411. == f"The uploaded file 'wrong_format_df.not_json' has invalid data for data node '{dn.id}'"
  412. )
  413. wrong_format_json_path = tmpdir_factory.mktemp("data").join("wrong_format_df.json").strpath
  414. with open(wrong_format_json_path, "w") as f:
  415. json.dump([{"a": 1, "b": 2, "d": 3}, {"a": 4, "b": 5, "d": 6}], f)
  416. # The upload should fail when check_data_keys() return False
  417. reasons = dn._upload(wrong_format_json_path, upload_checker=check_data_keys)
  418. assert bool(reasons) is False
  419. assert (
  420. str(list(reasons._reasons[dn.id])[0])
  421. == f"The uploaded file 'wrong_format_df.json' has invalid data for data node '{dn.id}'"
  422. )
  423. assert dn.read() == old_data # The content of the dn should not change when upload fails
  424. assert dn.last_edit_date == old_last_edit_date # The last edit date should not change when upload fails
  425. assert dn.path == _normalize_path(old_json_path) # The path of the dn should not change
  426. # The upload should succeed when check_data_keys() return True
  427. assert dn._upload(json_file, upload_checker=check_data_keys)