1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/c2f2b62f5435/ Changeset: c2f2b62f5435 User: jmchilton Date: 2014-01-13 22:14:33 Summary: Add more state handling tests... ... to tool execution unit tests. Affected #: 1 file diff -r 76979aa9d343e94dfcf22ce1458a145fbc9c7218 -r c2f2b62f5435a328eb0e54cbee00ce2fd3f668fc test/unit/tools/test_execution.py --- a/test/unit/tools/test_execution.py +++ b/test/unit/tools/test_execution.py @@ -7,14 +7,16 @@ import galaxy.model from galaxy.tools import Tool from galaxy.tools import DefaultToolState +from galaxy.tools.parameters import params_to_incoming from galaxy.util import parse_xml from galaxy.util import string_to_object from galaxy.util import object_to_string from galaxy.util.odict import odict from tools_support import UsesApp -EXAMPLE_TOOL_CONTENTS = '''<tool id="test_tool" name="Test Tool"> - <command>echo "$text" < $out1</command> +# Simple tool with just one text parameter and output. +SIMPLE_TOOL_CONTENTS = '''<tool id="test_tool" name="Test Tool"> + <command>echo "$param1" < $out1</command><inputs><param type="text" name="param1" value="" /></inputs> @@ -23,6 +25,21 @@ </outputs></tool>''' +# Tool with a repeat parameter, to test state update. +REPEAT_TOOL_CONTENTS = '''<tool id="test_tool" name="Test Tool"> + <command>echo "$param1" #for $r in $repeat# "$r.param2" #end for# < $out1</command> + <inputs> + <param type="text" name="param1" value="" /> + <repeat name="repeat1" label="Repeat 1"> + <param type="text" name="param2" value="" /> + </repeat> + </inputs> + <outputs> + <output name="out1" format="data" /> + </outputs> +</tool> +''' + class ToolExecutionTestCase( TestCase, UsesApp ): @@ -39,27 +56,82 @@ self.tear_down_app() def test_state_new( self ): - self.__write_tool( EXAMPLE_TOOL_CONTENTS ) - self.__setup_tool( ) - template, template_vars = self.tool.handle_input( - trans=self.trans, - incoming=dict( param1="moo" ) + self.__init_tool( SIMPLE_TOOL_CONTENTS ) + template, template_vars = self.__handle_with_incoming( + param1="moo", # no runtool_btn, just rerenders the form mako with tool # state populated. ) + state = self.__assert_rerenders_tool_without_errors( template, template_vars ) + assert state.inputs[ "param1" ] == "moo" + + def test_execute( self ): + self.__init_tool( SIMPLE_TOOL_CONTENTS ) + template, template_vars = self.__handle_with_incoming( + param1="moo", + runtool_btn="dummy", + ) + assert template == "tool_executed.mako" + + def test_repeat_state_updates( self ): + self.__init_tool( REPEAT_TOOL_CONTENTS ) + + # Fresh state contains no repeat elements + template, template_vars = self.__handle_with_incoming() + state = self.__assert_rerenders_tool_without_errors( template, template_vars ) + assert len( state.inputs[ "repeat1" ] ) == 0 + + # Hitting add button adds repeat element + template, template_vars = self.__handle_with_incoming( + repeat1_add="dummy", + ) + state = self.__assert_rerenders_tool_without_errors( template, template_vars ) + assert len( state.inputs[ "repeat1" ] ) == 1 + + # Hitting add button again adds another repeat element + template, template_vars = self.__handle_with_incoming( state, **{ + "repeat1_add": "dummy", + "repeat1_0|param2": "moo2", + } ) + state = self.__assert_rerenders_tool_without_errors( template, template_vars ) + assert len( state.inputs[ "repeat1" ] ) == 2 + assert state.inputs[ "repeat1" ][ 0 ][ "param2" ] == "moo2" + + # Hitting remove drops a repeat element + template, template_vars = self.__handle_with_incoming( state, repeat1_1_remove="dummy" ) + state = self.__assert_rerenders_tool_without_errors( template, template_vars ) + assert len( state.inputs[ "repeat1" ] ) == 1 + + def __handle_with_incoming( self, previous_state=None, **kwds ): + """ Execute tool.handle_input with incoming specified by kwds + (optionally extending a previous state). + """ + if previous_state: + incoming = self.__to_incoming( previous_state, **kwds) + else: + incoming = kwds + return self.tool.handle_input( + trans=self.trans, + incoming=incoming, + ) + + def __to_incoming( self, state, **kwds ): + new_incoming = {} + params_to_incoming( new_incoming, self.tool.inputs, state.inputs, self.app ) + new_incoming[ "tool_state" ] = self.__state_to_string( state ) + new_incoming.update( kwds ) + return new_incoming + + def __assert_rerenders_tool_without_errors( self, template, template_vars ): assert template == "tool_form.mako" assert not template_vars[ "errors" ] state = template_vars[ "tool_state" ] + return state assert state.inputs[ "param1" ] == "moo" - def test_execute( self ): - self.__write_tool( EXAMPLE_TOOL_CONTENTS ) + def __init_tool( self, tool_contents ): + self.__write_tool( tool_contents ) self.__setup_tool( ) - template, template_vars = self.tool.handle_input( - trans=self.trans, - incoming=dict( param1="moo", runtool_btn="dummy" ) - ) - assert template == "tool_executed.mako" def __setup_tool( self ): tree = parse_xml( self.tool_file ) @@ -80,9 +152,12 @@ tool_state.inputs = inputs return tool_state + def __state_to_string( self, tool_state ): + return object_to_string( tool_state.encode( self.tool, self.app ) ) + def __inputs_to_state_string( self, inputs ): tool_state = self.__inputs_to_state( inputs ) - return object_to_string( tool_state.encode( self.tool, self.app ) ) + return self.__state_to_string( tool_state ) class MockAction( object ): Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.