From e856773ebf4316e50cc108fb513d6df637cfc1b6 Mon Sep 17 00:00:00 2001 From: StableLlama Date: Fri, 30 May 2025 01:02:01 +0200 Subject: [PATCH] Add remaining tests --- src/basic_data_handling/list_nodes.py | 4 +- tests/test_dict_nodes.py | 100 ++++++++++++++++---- tests/test_set_nodes.py | 127 ++++++++++++++++++++++---- 3 files changed, 191 insertions(+), 40 deletions(-) diff --git a/src/basic_data_handling/list_nodes.py b/src/basic_data_handling/list_nodes.py index b8de2d6..49fe20f 100644 --- a/src/basic_data_handling/list_nodes.py +++ b/src/basic_data_handling/list_nodes.py @@ -790,11 +790,11 @@ NODE_DISPLAY_NAME_MAPPINGS = { "Basic data handling: ListContains": "contains", "Basic data handling: ListCount": "count", "Basic data handling: ListExtend": "extend", - "Basic data handling: ListFirst": "first element", + "Basic data handling: ListFirst": "first", "Basic data handling: ListGetItem": "get item", "Basic data handling: ListIndex": "index", "Basic data handling: ListInsert": "insert", - "Basic data handling: ListLast": "last element", + "Basic data handling: ListLast": "last", "Basic data handling: ListLength": "length", "Basic data handling: ListMax": "max", "Basic data handling: ListMin": "min", diff --git a/tests/test_dict_nodes.py b/tests/test_dict_nodes.py index 39fd979..7b21d50 100644 --- a/tests/test_dict_nodes.py +++ b/tests/test_dict_nodes.py @@ -1,27 +1,32 @@ #import pytest from src.basic_data_handling.dict_nodes import ( - DictCreate, - DictGet, - DictSet, - DictKeys, - DictValues, - DictItems, + DictCompare, DictContainsKey, - DictFromKeys, - DictPop, - DictPopItem, - DictSetDefault, - DictUpdate, - DictLength, - DictMerge, - DictGetKeysValues, - DictRemove, - DictFilterByKeys, + DictCreate, + DictCreateFromBoolean, + DictCreateFromFloat, + DictCreateFromInt, + DictCreateFromLists, + DictCreateFromString, DictExcludeKeys, + DictFilterByKeys, + DictFromKeys, + DictGet, + DictGetKeysValues, DictGetMultiple, DictInvert, - DictCreateFromLists, - DictCompare, + DictItems, + DictKeys, + DictLength, + DictMerge, + DictPop, + DictPopItem, + DictPopRandom, + DictRemove, + DictSet, + DictSetDefault, + DictUpdate, + DictValues, ) def test_dict_create(): @@ -47,6 +52,65 @@ def test_dict_set(): # Test with empty dict assert node.set({}, "key", "value") == ({"key": "value"},) +def test_dict_create_from_boolean(): + node = DictCreateFromBoolean() + # Test with dynamic inputs + result = node.create(key_0="key1", value_0=True, key_1="key2", value_1=False) + assert result == ({"key1": True, "key2": False},) + # Test with empty inputs + assert node.create() == ({},) + + +def test_dict_create_from_float(): + node = DictCreateFromFloat() + # Test with dynamic inputs + result = node.create(key_0="key1", value_0=1.5, key_1="key2", value_1=2.5) + assert result == ({"key1": 1.5, "key2": 2.5},) + # Test with empty inputs + assert node.create() == ({},) + + +def test_dict_create_from_int(): + node = DictCreateFromInt() + # Test with dynamic inputs + result = node.create(key_0="key1", value_0=1, key_1="key2", value_1=2) + assert result == ({"key1": 1, "key2": 2},) + # Test with empty inputs + assert node.create() == ({},) + + +def test_dict_create_from_string(): + node = DictCreateFromString() + # Test with dynamic inputs + result = node.create(key_0="key1", value_0="value1", key_1="key2", value_1="value2") + assert result == ({"key1": "value1", "key2": "value2"},) + # Test with empty inputs + assert node.create() == ({},) + + +def test_dict_pop_random(): + node = DictPopRandom() + # Test with non-empty dictionary + my_dict = {"key1": "value1", "key2": "value2"} + result_dict, key, value, success = node.pop_random(my_dict) + + # Check that operation was successful + assert success is True + # Check that one item was removed + assert len(result_dict) == len(my_dict) - 1 + # Check that removed key is not in result dict + assert key not in result_dict + # Check that the original key-value pair matches + assert my_dict[key] == value + + # Test with empty dictionary + empty_result_dict, empty_key, empty_value, empty_success = node.pop_random({}) + assert empty_result_dict == {} + assert empty_key == "" + assert empty_value is None + assert empty_success is False + + def test_dict_keys(): node = DictKeys() diff --git a/tests/test_set_nodes.py b/tests/test_set_nodes.py index aeb3fb4..83a91d9 100644 --- a/tests/test_set_nodes.py +++ b/tests/test_set_nodes.py @@ -1,25 +1,26 @@ #import pytest from src.basic_data_handling.set_nodes import ( SetAdd, - SetRemove, - SetDiscard, - SetPop, - SetUnion, - SetIntersection, - SetDifference, - SetSymmetricDifference, - SetIsSubset, - SetIsSuperset, - SetIsDisjoint, SetContains, - SetLength, - SetToList, SetCreate, - SetToDataList, + SetCreateFromBoolean, + SetCreateFromFloat, SetCreateFromInt, SetCreateFromString, - SetCreateFromFloat, - SetCreateFromBoolean + SetDifference, + SetDiscard, + SetIntersection, + SetIsDisjoint, + SetIsSubset, + SetIsSuperset, + SetLength, + SetPop, + SetPopRandom, + SetRemove, + SetSymmetricDifference, + SetToDataList, + SetToList, + SetUnion, ) def test_set_create(): @@ -28,6 +29,8 @@ def test_set_create(): assert node.create_set(item_0=1, item_1=2, item_2=3) == ({1, 2, 3},) assert node.create_set(item_0="a", item_1="b") == ({"a", "b"},) assert node.create_set() == (set(),) # Empty set with no arguments + # Mixed types + assert node.create_set(item_0=1, item_1="b", item_2=True) == ({1, "b", True},) def test_set_create_from_int(): @@ -35,15 +38,21 @@ def test_set_create_from_int(): assert node.create_set(item_0=1, item_1=2, item_2=3) == ({1, 2, 3},) assert node.create_set(item_0=5) == ({5},) # Single item set assert node.create_set(item_0=1, item_1=1) == ({1},) # Duplicate items become single item + assert node.create_set() == (set(),) # Empty set with no arguments def test_set_create_from_string(): node = SetCreateFromString() - # Note: Mocking the string function behavior as it's not defined in the file - # This simulates what would happen assuming string() acts like str() - node.create_set = lambda **kwargs: (set([str(value) for value in kwargs.values()]),) - assert node.create_set(item_0="apple", item_1="banana") == ({"apple", "banana"},) - assert node.create_set(item_0="apple", item_1="apple") == ({"apple"},) # Duplicate strings + result = node.create_set(item_0="apple", item_1="banana") + assert isinstance(result[0], set) + assert result[0] == {"apple", "banana"} + + # Duplicate strings + result = node.create_set(item_0="apple", item_1="apple") + assert result[0] == {"apple"} + + # Empty set + assert node.create_set() == (set(),) def test_set_create_from_float(): @@ -51,30 +60,40 @@ def test_set_create_from_float(): assert node.create_set(item_0=1.5, item_1=2.5) == ({1.5, 2.5},) assert node.create_set(item_0=3.14) == ({3.14},) # Single item set assert node.create_set(item_0=1.0, item_1=1.0) == ({1.0},) # Duplicate items + assert node.create_set() == (set(),) # Empty set with no arguments def test_set_create_from_boolean(): node = SetCreateFromBoolean() assert node.create_set(item_0=True, item_1=False) == ({True, False},) assert node.create_set(item_0=True, item_1=True) == ({True},) # Duplicate booleans + assert node.create_set() == (set(),) # Empty set with no arguments + # Test conversion from non-boolean values + assert node.create_set(item_0=1, item_1=0) == ({True, False},) def test_set_add(): node = SetAdd() assert node.add({1, 2}, 3) == ({1, 2, 3},) assert node.add({1, 2}, 1) == ({1, 2},) # Adding an existing item + assert node.add(set(), "first") == ({"first"},) # Adding to empty set + assert node.add({1, 2}, "string") == ({1, 2, "string"},) # Adding different type def test_set_remove(): node = SetRemove() assert node.remove({1, 2, 3}, 2) == ({1, 3}, True) # Successful removal assert node.remove({1, 2, 3}, 4) == ({1, 2, 3}, False) # Item not in set + assert node.remove({1}, 1) == (set(), True) # Removing the only element + assert node.remove(set(), 1) == (set(), False) # Removing from empty set def test_set_discard(): node = SetDiscard() assert node.discard({1, 2, 3}, 2) == ({1, 3},) # Successful removal assert node.discard({1, 2, 3}, 4) == ({1, 2, 3},) # No error for missing item + assert node.discard({1}, 1) == (set(),) # Discarding the only element + assert node.discard(set(), 1) == (set(),) # Discarding from empty set def test_set_pop(): @@ -83,16 +102,40 @@ def test_set_pop(): result_set, removed_item = node.pop(input_set) assert result_set != input_set # Arbitrary item removed assert removed_item in input_set # Removed item was part of original set + assert len(result_set) == len(input_set) - 1 # One item was removed + assert removed_item not in result_set # Removed item is not in result set empty_set = set() assert node.pop(empty_set) == (set(), None) # Handle empty set +def test_set_pop_random(): + node = SetPopRandom() + # Test with single item - must remove that item + single_item_set = {42} + result_set, removed_item = node.pop_random_element(single_item_set) + assert result_set == set() and removed_item == 42 + + # Test with multiple items - can't predict which one will be popped + # but we can check the result set size and that the popped item was from the original set + original_set = {1, 2, 3, 4} + result_set, removed_item = node.pop_random_element(original_set) + assert len(result_set) == len(original_set) - 1 + assert removed_item in original_set + assert removed_item not in result_set + + # Test with empty set + empty_set = set() + assert node.pop_random_element(empty_set) == (set(), None) + + def test_set_union(): node = SetUnion() assert node.union({1, 2}, {3, 4}) == ({1, 2, 3, 4},) assert node.union({1}, {2}, {3}, {4}) == ({1, 2, 3, 4},) assert node.union({1, 2}, set()) == ({1, 2},) # Union with empty set + assert node.union(set(), set()) == (set(),) # Union of empty sets + assert node.union({1, 2}, {2, 3}) == ({1, 2, 3},) # Overlapping sets def test_set_intersection(): @@ -100,18 +143,25 @@ def test_set_intersection(): assert node.intersection({1, 2, 3}, {2, 3, 4}) == ({2, 3},) assert node.intersection({1, 2, 3}, {4, 5}) == (set(),) # No common elements assert node.intersection({1, 2, 3}, {2, 3}, {3, 4}) == ({3},) # Multiple sets + assert node.intersection({1, 2, 3}, {1, 2, 3}) == ({1, 2, 3},) # Identical sets + assert node.intersection(set(), {1, 2, 3}) == (set(),) # Empty set intersection def test_set_difference(): node = SetDifference() assert node.difference({1, 2, 3}, {2, 3, 4}) == ({1},) assert node.difference({1, 2, 3}, {4, 5}) == ({1, 2, 3},) # Nothing to remove + assert node.difference({1, 2, 3}, {1, 2, 3}) == (set(),) # Identical sets + assert node.difference(set(), {1, 2, 3}) == (set(),) # Empty set difference + assert node.difference({1, 2, 3}, set()) == ({1, 2, 3},) # Difference with empty set def test_set_symmetric_difference(): node = SetSymmetricDifference() assert node.symmetric_difference({1, 2, 3}, {3, 4, 5}) == ({1, 2, 4, 5},) assert node.symmetric_difference({1, 2, 3}, {1, 2, 3}) == (set(),) # No unique elements + assert node.symmetric_difference(set(), {1, 2, 3}) == ({1, 2, 3},) # Empty set symmetric difference + assert node.symmetric_difference({1, 2, 3}, set()) == ({1, 2, 3},) # Symmetric difference with empty set def test_set_is_subset(): @@ -119,6 +169,8 @@ def test_set_is_subset(): assert node.is_subset({1, 2}, {1, 2, 3}) == (True,) assert node.is_subset({1, 4}, {1, 2, 3}) == (False,) assert node.is_subset(set(), {1, 2, 3}) == (True,) # Empty set is subset of all sets + assert node.is_subset({1, 2}, {1, 2}) == (True,) # Set is subset of itself + assert node.is_subset({1, 2, 3}, {1, 2}) == (False,) # Superset is not a subset def test_set_is_superset(): @@ -126,32 +178,51 @@ def test_set_is_superset(): assert node.is_superset({1, 2, 3}, {1, 2}) == (True,) assert node.is_superset({1, 2}, {1, 2, 3}) == (False,) assert node.is_superset(set(), set()) == (True,) # Empty set is a superset of itself + assert node.is_superset({1, 2}, {1, 2}) == (True,) # Set is superset of itself + assert node.is_superset({1, 2}, set()) == (True,) # Any set is superset of empty set def test_set_is_disjoint(): node = SetIsDisjoint() assert node.is_disjoint({1, 2}, {3, 4}) == (True,) # No common elements assert node.is_disjoint({1, 2}, {2, 3}) == (False,) # Common element + assert node.is_disjoint(set(), {1, 2, 3}) == (True,) # Empty set is disjoint with any set + assert node.is_disjoint({1, 2}, set()) == (True,) # Empty set is disjoint with any set + assert node.is_disjoint(set(), set()) == (True,) # Empty sets are disjoint def test_set_contains(): node = SetContains() assert node.contains({1, 2, 3}, 2) == (True,) assert node.contains({1, 2, 3}, 4) == (False,) + assert node.contains(set(), 1) == (False,) # Empty set contains nothing + assert node.contains({1, "string", True}, "string") == (True,) # Mixed type set + assert node.contains({1, "string", True}, False) == (False,) # Boolean check def test_set_length(): node = SetLength() assert node.length({1, 2, 3}) == (3,) assert node.length(set()) == (0,) # Empty set + assert node.length({1, 1, 1, 1}) == (1,) # Set with duplicate values (only counts unique) + assert node.length({12, "string", True, 3.14}) == (4,) # Mixed types def test_set_to_list(): node = SetToList() result = node.convert({1, 2, 3}) assert isinstance(result, tuple) + assert isinstance(result[0], list) assert sorted(result[0]) == [1, 2, 3] # Validate conversion to list + # Empty set + result = node.convert(set()) + assert result[0] == [] + + # Mixed types + result = node.convert({1, "string", True}) + assert set(result[0]) == {1, "string", True} # Can't check order, just content + def test_set_to_data_list(): node = SetToDataList() @@ -159,3 +230,19 @@ def test_set_to_data_list(): assert isinstance(result, tuple) assert isinstance(result[0], list) assert sorted(result[0]) == [1, 2, 3] # Validate conversion to data list + + # Empty set + result = node.convert(set()) + assert result[0] == [] + + # Mixed types + result = node.convert({1, "string", True}) + assert set(result[0]) == {1, "string", True} # Can't check order, just content + + # Empty set + result = node.convert(set()) + assert result[0] == [] + + # Mixed types + result = node.convert({1, "string", True}) + assert set(result[0]) == {1, "string", True} # Can't check order, just content