Skip to content

Commit

Permalink
Merge pull request #97 from pedohorse/fix-rename-attirb-input-validation
Browse files Browse the repository at this point in the history
Fix rename attirb input validation
  • Loading branch information
pedohorse authored Sep 1, 2024
2 parents 9de6425 + 0530ff0 commit 977921f
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/lifeblood/core_nodes/rename_attrib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lifeblood.node_plugin_base import BaseNode
from lifeblood.nodethings import ProcessingResult
from lifeblood.nodethings import ProcessingResult, ProcessingError
from lifeblood.taskspawn import TaskSpawn
from lifeblood.exceptions import NodeNotReadyToProcess
from lifeblood.enums import NodeParameterType
Expand Down Expand Up @@ -29,28 +29,44 @@ def __init__(self, name: str):
super(RenameAttributes, self).__init__(name)
ui = self.get_ui()
with ui.initializing_interface_lock():
ui.add_parameter('ignore errors', 'Ignore non-existing attribute errors', NodeParameterType.BOOL, False)
with ui.multigroup_parameter_block('num'):
with ui.parameters_on_same_line_block():
ui.add_parameter('oldname', '<from / to>', NodeParameterType.STRING, 'from')
ui.add_parameter('newname', None, NodeParameterType.STRING, 'to')

def process_task(self, context) -> ProcessingResult:
res = ProcessingResult()
do_ignore_errors = context.param_value('ignore errors')
attrs = dict(context.task_attributes())
for i in range(context.param_value('num')):
attr_oldname = context.param_value(f'oldname_{i}')
attr_oldname: str = context.param_value(f'oldname_{i}').strip()
if not attr_oldname:
if do_ignore_errors:
continue
else:
raise ProcessingError(f'from-attribute name must not be empty')

if attr_oldname not in attrs:
continue
if do_ignore_errors:
continue
else:
raise ProcessingError(f'attribute "{attr_oldname}" does not exist')

attr_newname: str = context.param_value(f'newname_{i}').strip()
if not attr_newname:
if do_ignore_errors:
continue
else:
raise ProcessingError(f'to-attribute name must not be empty')

attr_newname = context.param_value(f'newname_{i}')
if attr_newname == attr_oldname:
continue

res.set_attribute(attr_newname, attrs[attr_oldname])
res.remove_attribute(attr_oldname)
attrs[attr_newname] = attrs[attr_oldname]
del attrs[attr_oldname]
attrs.pop(attr_oldname)

return res

Expand Down
170 changes: 170 additions & 0 deletions tests/nodes/test_rename_attrib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import random
from asyncio import Event
from lifeblood.scheduler import Scheduler
from lifeblood.worker import Worker
from lifeblood.nodethings import ProcessingError
from lifeblood_testing_common.nodes_common import TestCaseBase, PseudoContext

from typing import List


class TestWedge(TestCaseBase):
async def test_noop(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')

res = context.process_task(node, task)
self.assertEqual({}, res.attributes_to_set)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_same(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('num', 1)
node.set_param_value('oldname_0', 'foo')
node.set_param_value('newname_0', 'foo')

res = context.process_task(node, task)
self.assertEqual({}, res.attributes_to_set)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_basic(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
for pref, suff in (('', ''), (' ', ''), (' ', ''), ('', ' '), ('', ' '), (' ', ' '), (' ', ' '), (' ', ' ')):
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('num', 1)
node.set_param_value('oldname_0', f'{pref}foo{suff}')
node.set_param_value('newname_0', f'{pref}bla{suff}')

res = context.process_task(node, task)
self.assertEqual({
'bla': 123,
'foo': None,
}, res.attributes_to_set)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_chain(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):

task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('ignore errors', False)
node.set_param_value('num', 2)
node.set_param_value('oldname_0', f'foo')
node.set_param_value('newname_0', f'middle')
node.set_param_value('oldname_1', f'middle')
node.set_param_value('newname_1', f'bla')

res = context.process_task(node, task)
self.assertEqual({
'bla': 123,
'foo': None,
'middle': None,
}, res.attributes_to_set)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_overlap(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('num', 1)
node.set_param_value('oldname_0', 'foo')
node.set_param_value('newname_0', 'bar')

res = context.process_task(node, task)
self.assertEqual({
'bar': 123,
'foo': None,
}, res.attributes_to_set)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_nonexisting_error(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('ignore errors', False)
node.set_param_value('num', 1)
node.set_param_value('oldname_0', 'faaaaa')
node.set_param_value('newname_0', 'bla')

self.assertRaises(ProcessingError, context.process_task, node, task)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_nonexisting_noerror(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('ignore errors', True)
node.set_param_value('num', 1)
node.set_param_value('oldname_0', 'faaaaa')
node.set_param_value('newname_0', 'bla')

res = context.process_task(node, task)
self.assertEqual({}, res.attributes_to_set)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_empty_error(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
for from_attr, to_attr in [('', 'foo'), ('foo', ''), ('', ''), (' ', ' '), ('bar', ' '), (' ', 'bar')]:
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('ignore errors', False)
node.set_param_value('num', 1)
node.set_param_value('oldname_0', from_attr)
node.set_param_value('newname_0', to_attr)

self.assertRaises(ProcessingError, context.process_task, node, task)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_empty_noerror(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
for from_attr, to_attr in [('', 'foo'), ('foo', ''), ('', ''), (' ', ' '), ('bar', ' '), (' ', 'bar')]:
task = context.create_pseudo_task_with_attrs({'foo': 123, 'bar': 'wow'})

node = context.create_node('rename_attrib', 'footest')
node.set_param_value('ignore errors', True)
node.set_param_value('num', 1)
node.set_param_value('oldname_0', from_attr)
node.set_param_value('newname_0', to_attr)

res = context.process_task(node, task)
self.assertEqual({}, res.attributes_to_set) # expect noop

await self._helper_test_node_with_arg_update(
_logic
)

0 comments on commit 977921f

Please sign in to comment.