Skip to content

Commit

Permalink
add even more tests for switch node
Browse files Browse the repository at this point in the history
  • Loading branch information
oldrev committed Oct 11, 2024
1 parent 5544e9e commit 62db940
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 12 deletions.
22 changes: 11 additions & 11 deletions tests/REDNODES-SPECS-DIFF.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,19 @@ This file is automatically generated by the script `test/specs_diff.py` to compa
| :white_check_mark: | ~~switch Node should check if payload is not empty (string)~~ |
| :white_check_mark: | ~~switch Node should check if payload is not empty (undefined)~~ |
| :white_check_mark: | ~~switch Node should check if payload less than equals given value~~ |
| :white_check_mark: | ~~switch Node should handle flow context~~ |
| :white_check_mark: | ~~switch Node should handle global context~~ |
| :white_check_mark: | ~~switch Node should handle persistable flow context~~ |
| :white_check_mark: | ~~switch Node should handle persistable global context~~ |
| :white_check_mark: | ~~switch Node should match if a payload has a required property~~ |
| :white_check_mark: | ~~switch Node should match regex~~ |
| :white_check_mark: | ~~switch Node should match regex with ignore-case flag set true~~ |
| :white_check_mark: | ~~switch Node should not match if a payload does not have a required property~~ |
| :white_check_mark: | ~~switch Node should not match if the key is not a string~~ |
| :white_check_mark: | ~~switch Node should not match if the parent object does not exist - null~~ |
| :white_check_mark: | ~~switch Node should not match if the parent object does not exist - undefined~~ |
| :white_check_mark: | ~~switch Node should not match regex with ignore-case flag set false~~ |
| :white_check_mark: | ~~switch Node should not match regex with ignore-case flag unset~~ |
| :white_check_mark: | ~~switch Node should return nothing when the payload does equal to desired numeric value~~ |
| :white_check_mark: | ~~switch Node should return nothing when the payload does equal to desired string~~ |
| :white_check_mark: | ~~switch Node should return nothing when the payload doesn't contain desired string~~ |
Expand All @@ -237,6 +244,10 @@ This file is automatically generated by the script `test/specs_diff.py` to compa
| :white_check_mark: | ~~switch Node should return nothing when the payload is not greater than desired string~~ |
| :white_check_mark: | ~~switch Node should return nothing when the payload is not greater than desired string~~ |
| :white_check_mark: | ~~switch Node should return nothing when the payload is not less than desired string~~ |
| :white_check_mark: | ~~switch Node should use a nested message property to compare nested message property - matches~~ |
| :white_check_mark: | ~~switch Node should use a nested message property to compare nested message property - no match~~ |
| :white_check_mark: | ~~switch Node should use a nested message property to compare value - matches~~ |
| :white_check_mark: | ~~switch Node should use a nested message property to compare value - no match~~ |
| :x: | **switch Node handles more than one switch statement** |
| :x: | **switch Node sends a message when the "else/otherwise" statement is selected** |
| :x: | **switch Node should be able to use $I in JSONata expression** |
Expand All @@ -252,16 +263,9 @@ This file is automatically generated by the script `test/specs_diff.py` to compa
| :x: | **switch Node should handle empty rule** |
| :x: | **switch Node should handle env var expression** |
| :x: | **switch Node should handle flow and global contexts with JSONata expression** |
| :x: | **switch Node should handle flow context** |
| :x: | **switch Node should handle global context** |
| :x: | **switch Node should handle invalid jsonata expression** |
| :x: | **switch Node should handle persistable flow and global contexts with JSONata expression** |
| :x: | **switch Node should handle persistable flow context** |
| :x: | **switch Node should handle persistable global context** |
| :x: | **switch Node should handle too many pending messages** |
| :x: | **switch Node should match regex with ignore-case flag set true** |
| :x: | **switch Node should not match regex with ignore-case flag set false** |
| :x: | **switch Node should not match regex with ignore-case flag unset** |
| :x: | **switch Node should not repair message sequence for each port** |
| :x: | **switch Node should repair message sequence for each port** |
| :x: | **switch Node should repair message sequence for each port (overlap)** |
Expand All @@ -275,10 +279,6 @@ This file is automatically generated by the script `test/specs_diff.py` to compa
| :x: | **switch Node should take tail of message sequence (no repair)** |
| :x: | **switch Node should take tail of message sequence (repair)** |
| :x: | **switch Node should treat non-existant msg property conditional as undefined** |
| :x: | **switch Node should use a nested message property to compare nested message property - matches** |
| :x: | **switch Node should use a nested message property to compare nested message property - no match** |
| :x: | **switch Node should use a nested message property to compare value - matches** |
| :x: | **switch Node should use a nested message property to compare value - no match** |
| :x: | **switch Node stops after first statement** |
### change
| Status | Spec Test |
Expand Down
157 changes: 156 additions & 1 deletion tests/nodes/function/test_switch_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,162 @@ async def test_it_should_check_if_payload_if_of_type_null(self):
async def test_it_should_check_if_payload_if_of_type_undefined(self):
await _generic_switch_test("istype", "undefined", True, True, None)

# TODO Add more
@pytest.mark.asyncio
@pytest.mark.it('should handle flow context')
async def test_it_should_handle_flow_context(self):
flows = [
{"id": "100", "type": "tab"}, # flow 1
{"id": "1", "type": "change", "z": "100", "rules": [
{"t": "set", "p": "foo", "pt": "flow", "to": "flowValue", "tot": "str"},
{"t": "set", "p": "bar", "pt": "flow", "to": "flowValue", "tot": "str"},
], "reg": False, "name": "changeNode", "wires": [["2"]]},
{"id": "2", "z": "100", "type": "switch", "property": "foo", "propertyType": "flow",
"rules": [{"t": "eq", "v": "bar", "vt": "flow"}],
"checkall": "true", "outputs": 1, "wires": [["3"]]},
{"id": "3", "z": "100", "type": "test-once"}
]
injections = [
{"nid": "1", "msg": {"payload": "value"}},
]
msgs = await run_flow_with_msgs_ntimes(flows, injections, 1)
assert msgs[0]["payload"] == "value"

@pytest.mark.asyncio
@pytest.mark.it('should handle persistable flow context')
async def test_it_should_handle_persistable_flow_context(self):
flows = [
{"id": "100", "type": "tab"}, # flow 1
{"id": "1", "type": "change", "z": "100", "rules": [
{"t": "set", "p": "#:(memory1)::foo", "pt": "flow", "to": "flowValue", "tot": "str"},
{"t": "set", "p": "#:(memory1)::bar", "pt": "flow", "to": "flowValue", "tot": "str"},
], "reg": False, "name": "changeNode", "wires": [["2"]]},
{"id": "2", "z": "100", "type": "switch", "property": "#:(memory1)::foo", "propertyType": "flow",
"rules": [{"t": "eq", "v": "#:(memory1)::bar", "vt": "flow"}],
"checkall": "true", "outputs": 1, "wires": [["3"]]},
{"id": "3", "z": "100", "type": "test-once"}
]
injections = [
{"nid": "1", "msg": {"payload": "value"}},
]
msgs = await run_flow_with_msgs_ntimes(flows, injections, 1)
assert msgs[0]["payload"] == "value"

@pytest.mark.asyncio
@pytest.mark.it('should handle global context')
async def test_it_should_handle_global_context(self):
flows = [
{"id": "100", "type": "tab"}, # flow 1
{"id": "1", "type": "change", "z": "100", "rules": [
{"t": "set", "p": "foo", "pt": "global", "to": "globalValue", "tot": "str"},
{"t": "set", "p": "bar", "pt": "global", "to": "globalValue", "tot": "str"},
], "reg": False, "name": "changeNode", "wires": [["2"]]},
{"id": "2", "z": "100", "type": "switch", "property": "foo", "propertyType": "global",
"rules": [{"t": "eq", "v": "bar", "vt": "global"}],
"checkall": "true", "outputs": 1, "wires": [["3"]]},
{"id": "3", "z": "100", "type": "test-once"}
]
injections = [
{"nid": "1", "msg": {"payload": "value"}},
]
msgs = await run_flow_with_msgs_ntimes(flows, injections, 1)
assert msgs[0]["payload"] == "value"

@pytest.mark.asyncio
@pytest.mark.it('should handle persistable global context')
async def test_it_should_handle_persistable_global_context(self):
flows = [
{"id": "100", "type": "tab"}, # flow 1
{"id": "1", "type": "change", "z": "100", "rules": [
{"t": "set", "p": "#:(memory1)::foo", "pt": "global", "to": "globalValue", "tot": "str"},
{"t": "set", "p": "#:(memory1)::bar", "pt": "global", "to": "globalValue", "tot": "str"},
], "reg": False, "name": "changeNode", "wires": [["2"]]},
{"id": "2", "z": "100", "type": "switch", "property": "#:(memory1)::foo", "propertyType": "global",
"rules": [{"t": "eq", "v": "#:(memory1)::bar", "vt": "global"}],
"checkall": "true", "outputs": 1, "wires": [["3"]]},
{"id": "3", "z": "100", "type": "test-once"}
]
injections = [
{"nid": "1", "msg": {"payload": "value"}},
]
msgs = await run_flow_with_msgs_ntimes(flows, injections, 1)
assert msgs[0]["payload"] == "value"

@pytest.mark.asyncio
@pytest.mark.it('should use a nested message property to compare value - matches')
async def test_it_should_use_a_nested_message_property_to_compare_value_matches(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload[msg.topic]", "rules": [
{"t": "eq", "v": "bar"}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_message_switch_test(flow, True, {"topic": "foo", "payload": {"foo": "bar"}})

@pytest.mark.asyncio
@pytest.mark.it('should use a nested message property to compare value - no match')
async def test_it_should_use_a_nested_message_property_to_compare_value_no_match(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload[msg.topic]", "rules": [
{"t": "eq", "v": "bar"}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_message_switch_test(flow, False, {"topic": "foo", "payload": {"foo": "none"}})

@pytest.mark.asyncio
@pytest.mark.it('should use a nested message property to compare nested message property - matches')
async def test_it_should_use_a_nested_message_property_to_compare_nested_message_property_matches(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload[msg.topic]", "rules": [
{"t": "eq", "v": "payload[msg.topic2]", "vt": "msg"}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_message_switch_test(flow, True, {"topic": "foo", "topic2": "foo2", "payload": {"foo": "bar", "foo2": "bar"}})

@pytest.mark.asyncio
@pytest.mark.it('should use a nested message property to compare nested message property - no match')
async def test_it_should_use_a_nested_message_property_to_compare_nested_message_property_no_match(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload[msg.topic]", "rules": [
{"t": "eq", "v": "payload[msg.topic2]", "vt": "msg"}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_message_switch_test(flow, False, {"topic": "foo", "topic2": "foo2", "payload": {"foo": "bar", "foo2": "none"}})

@pytest.mark.asyncio
@pytest.mark.it('should match regex with ignore-case flag set true')
async def test_it_should_match_regex_with_ignore_case_flag_set_true(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload", "rules": [
{"t": "regex", "v": "onetwothree", "case": True}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_switch_test(flow, True, "oneTWOthree")

@pytest.mark.asyncio
@pytest.mark.it('should not match regex with ignore-case flag unset')
async def test_it_should_not_match_regex_with_ignore_case_flag_unset(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload", "rules": [
{"t": "regex", "v": "onetwothree"}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_switch_test(flow, False, "oneTWOthree")

@pytest.mark.asyncio
@pytest.mark.it('should not match regex with ignore-case flag set false')
async def test_it_should_not_match_regex_with_ignore_case_flag_set_false(self):
flow = [
{"id": "100", "type": "tab"},
{"id": "1", "z": "100", "type": "switch", "name": "switchNode", "property": "payload", "rules": [
{"t": "regex", "v": "onetwothree", "case": False}], "checkall": True, "outputs": 1, "wires": [["2"]]},
{"id": "2", "z": "100", "type": "test-once"}
]
await _custom_flow_switch_test(flow, False, "oneTWOthree")

@pytest.mark.asyncio
@pytest.mark.it("should return nothing when the payload doesn't match regex")
Expand Down

0 comments on commit 62db940

Please sign in to comment.