add create DICT and LIST from JSON string
This commit is contained in:
@@ -11,6 +11,7 @@ from src.basic_data_handling.dict_nodes import (
|
||||
DictCreateFromItemsList,
|
||||
DictCreateFromLists,
|
||||
DictCreateFromString,
|
||||
DictCreateFromJSONString,
|
||||
DictExcludeKeys,
|
||||
DictFilterByKeys,
|
||||
DictFromKeys,
|
||||
@@ -112,6 +113,20 @@ def test_dict_create_from_string():
|
||||
assert node.create() == ({},)
|
||||
|
||||
|
||||
def test_dict_create_from_json_string():
|
||||
node = DictCreateFromJSONString()
|
||||
# Single-line JSON object
|
||||
assert node.create_from_json('{\n "key1": "value1",\n "key2": 2\n}') == ({"key1": "value1", "key2": 2},)
|
||||
# Empty object
|
||||
assert node.create_from_json("{}") == ({},)
|
||||
# JSON array is not a valid object -> error
|
||||
with pytest.raises(ValueError, match="JSON object"):
|
||||
node.create_from_json("[1, 2, 3]")
|
||||
# Malformed JSON -> error
|
||||
with pytest.raises(ValueError):
|
||||
node.create_from_json("{not json}")
|
||||
|
||||
|
||||
def test_dict_create_from_items_datalist():
|
||||
node = DictCreateFromItemsDataList()
|
||||
assert node.create_from_items(item=[("key1", "value1"), ("key2", "value2")]) == (_dict_x2,)
|
||||
|
||||
@@ -10,6 +10,7 @@ from src.basic_data_handling.list_nodes import (
|
||||
ListCreateFromFloat,
|
||||
ListCreateFromInt,
|
||||
ListCreateFromString,
|
||||
ListCreateFromJSONString,
|
||||
ListEnumerate,
|
||||
ListExtend,
|
||||
ListFirst,
|
||||
@@ -257,6 +258,22 @@ def test_list_create_from_string():
|
||||
assert node.create_list() == ([],)
|
||||
|
||||
|
||||
def test_list_create_from_json_string():
|
||||
node = ListCreateFromJSONString()
|
||||
# Multiline JSON array
|
||||
assert node.create_from_json('[\n 1,\n 2,\n 3\n]') == ([1, 2, 3],)
|
||||
# Empty array
|
||||
assert node.create_from_json("[]") == ([],)
|
||||
# Mix of JSON types
|
||||
assert node.create_from_json('["a", 1, true, null]') == (["a", 1, True, None],)
|
||||
# JSON object is not a valid array -> error
|
||||
with pytest.raises(ValueError, match="JSON array"):
|
||||
node.create_from_json('{"a": 1}')
|
||||
# Malformed JSON -> error
|
||||
with pytest.raises(ValueError):
|
||||
node.create_from_json("[1, 2,")
|
||||
|
||||
|
||||
def test_list_to_data_list():
|
||||
node = ListToDataList()
|
||||
input_list = [1, 2, 3]
|
||||
|
||||
Reference in New Issue
Block a user