def __init__(self, methodName):
     super(UDFTestCase,self).__init__(methodName)
     self.udf_obj=UDFExceptionHandling()
 def __init__(self, methodName):
     super(UDFTestCase, self).__init__(methodName)
     self.udf_obj = UDFExceptionHandling()
class UDFTestCase(ScenarioTestCase, MPPTestCase):
    ''' Testing exception handling in subtransactions '''
    
    def __init__(self, methodName):
        super(UDFTestCase,self).__init__(methodName)
        self.udf_obj=UDFExceptionHandling()

    @classmethod
    def setUpClass(cls):
        super(UDFTestCase, cls).setUpClass()
        '''
        Create UDF to exercise subtransactions upfront. They will be used in the tests .
        '''
        base_dir = os.path.dirname(sys.modules[cls.__module__].__file__)
        udf_name = ['test_excep','test_protocol_allseg','setup_sql_exceptionhandling']
        for uname in udf_name:
            udfsql=''
            udfsql= os.path.join(os.path.dirname(sys.modules[cls.__module__].__file__), "sql")+'/'+uname+'.sql'
            udfans= os.path.join(os.path.dirname(sys.modules[cls.__module__].__file__), "expected")+'/'+uname+'.ans'
            udfout= os.path.join(os.path.dirname(sys.modules[cls.__module__].__file__), "sql")+'/'+uname+'.out'
            tinctest.logger.info( '\n Creating UDF :  %s' % udfsql )
            res=PSQL.run_sql_file(sql_file = udfsql,out_file=udfout)
            init_file=os.path.join( base_dir, "sql",'init_file')
            result = Gpdiff.are_files_equal(udfout, udfans, match_sub =[init_file])
            assert result, 'Gpdiff are not equal' 

            

    def test_UDF_exception(self):
        ''' 
        
        @param debug_dtm_action_segment 
        @param debug_dtm_action_target 
        @param debug_dtm_action_protocol  or debug_dtm_action_sql_command_tag
        @param debug_dtm_action 
        @param debug_dtm_action_nestinglevel
        @description: This tests the Exception Handling of GPDB PL/PgSQL UDF 
                      It exercises ;
                         1. PROTOCOL or SQL type of dtm_action_target
                         2. Various levels of sub-transactions
                         3. dtm_action_protocol(PROTOCOL): subtransaction_begin, subtransaction_rollback or subtransaction_release 
                            or
                            debug_dtm_action_sql_command_tag(SQL) : 'MPPEXEC UPDATE'
                         4. dtm_action: fail_begin_command, fail_end_command or panic_begin_comand
        
        debug_dtm_action: Using this can specify what action to be triggered/simulated and at what point like 
                          error / panic / delay and at start or end command after receiving by the segment.

        debug_dtm_action_segment: Using this can specify segment number to trigger the specified dtm_action.

        debug_dtm_action_target: Allows to set target for specified dtm_action should it be DTM protocol 
                                 command or SQL command from master to segment.

        debug_dtm_action_protocol: Allows to specify sub-type of DTM protocol for which to perform specified 
                                   dtm_action (like prepare, abort_no_prepared, commit_prepared, abort_prepared, 
                                   subtransaction_begin, subtransaction_release, subtransaction_rollback, etc...
        debug_dtm_action_sql_command_tag: If debug_dtm_action_target is sql then this parameter can be used to set the type of sql
                                          that should trigger the exeception. Ex: 'MPPEXEC UPDATE'

        debug_dtm_action_nestinglevel: This allows to optional specify at which specific depth level in transaction 
                                       to take the specified dtm_action. This apples only to target with protocol and not SQL.
        STEPS:
        @data_provider data_types_provider
        '''
        tinctest.logger.info("\n ===============================================")
        tinctest.logger.info("\n Starting New Test: %s%s " % (self.test_data[0][0], self.test_data[0][1] ))
        debug_dtm_action_segment = self.test_data[1][0]
        debug_dtm_action_target = self.test_data[1][1]
        debug_dtm_action_protocol=''
        debug_dtm_action_sql_command_tag=''
        if debug_dtm_action_target == 'protocol':
            debug_dtm_action_protocol = self.test_data[1][2]
        elif debug_dtm_action_target == 'sql':
            debug_dtm_action_sql_command_tag = self.test_data[1][2]
        debug_dtm_action = self.test_data[1][3]
        debug_dtm_action_nestinglevel = self.test_data[1][4]
        
        tinctest.logger.info( '\ndebug_dtm_action_segment: %s' % debug_dtm_action_segment)
        tinctest.logger.info( 'debug_dtm_action_target: %s' % debug_dtm_action_target)
        tinctest.logger.info( 'debug_dtm_action_protocol: %s ' % debug_dtm_action_protocol)
        tinctest.logger.info( 'debug_dtm_action_sql_command_tag: %s ' % debug_dtm_action_sql_command_tag)
        tinctest.logger.info( 'debug_dtm_action: %s ' % debug_dtm_action)
        tinctest.logger.info( 'debug_dtm_action_nestinglevel: %s ' % debug_dtm_action_nestinglevel)
        tinctest.logger.info("\n ===============================================")

        self.udf_obj.reset_protocol_conf()
        if debug_dtm_action_target == 'protocol':
            self.udf_obj.set_protocol_conf(debug_dtm_action_segment, debug_dtm_action_target, debug_dtm_action_protocol,debug_dtm_action,debug_dtm_action_nestinglevel)
            self.udf_obj.run_test(debug_dtm_action_segment, debug_dtm_action_target, debug_dtm_action_protocol,debug_dtm_action,debug_dtm_action_nestinglevel)
        if debug_dtm_action_target == 'sql':
            self.udf_obj.set_sql_conf(debug_dtm_action_segment, debug_dtm_action_target, debug_dtm_action_sql_command_tag,debug_dtm_action,debug_dtm_action_nestinglevel)
            self.udf_obj.run_test(debug_dtm_action_segment, debug_dtm_action_target, debug_dtm_action_sql_command_tag,debug_dtm_action,debug_dtm_action_nestinglevel)
class UDFTestCase(ScenarioTestCase, MPPTestCase):
    ''' Testing exception handling in subtransactions '''
    def __init__(self, methodName):
        super(UDFTestCase, self).__init__(methodName)
        self.udf_obj = UDFExceptionHandling()

    @classmethod
    def setUpClass(cls):
        super(UDFTestCase, cls).setUpClass()
        '''
        Create UDF to exercise subtransactions upfront. They will be used in the tests .
        '''
        base_dir = os.path.dirname(sys.modules[cls.__module__].__file__)
        udf_name = [
            'test_excep', 'test_protocol_allseg', 'setup_sql_exceptionhandling'
        ]
        for uname in udf_name:
            udfsql = ''
            udfsql = os.path.join(
                os.path.dirname(sys.modules[cls.__module__].__file__),
                "sql") + '/' + uname + '.sql'
            udfans = os.path.join(
                os.path.dirname(sys.modules[cls.__module__].__file__),
                "expected") + '/' + uname + '.ans'
            udfout = os.path.join(
                os.path.dirname(sys.modules[cls.__module__].__file__),
                "sql") + '/' + uname + '.out'
            tinctest.logger.info('\n Creating UDF :  %s' % udfsql)
            res = PSQL.run_sql_file(sql_file=udfsql, out_file=udfout)
            init_file = os.path.join(base_dir, "sql", 'init_file')
            result = Gpdiff.are_files_equal(udfout,
                                            udfans,
                                            match_sub=[init_file])
            assert result, 'Gpdiff are not equal'

    def test_UDF_exception(self):
        ''' 
        
        @param debug_dtm_action_segment 
        @param debug_dtm_action_target 
        @param debug_dtm_action_protocol  or debug_dtm_action_sql_command_tag
        @param debug_dtm_action 
        @param debug_dtm_action_nestinglevel
        @description: This tests the Exception Handling of GPDB PL/PgSQL UDF 
                      It exercises ;
                         1. PROTOCOL or SQL type of dtm_action_target
                         2. Various levels of sub-transactions
                         3. dtm_action_protocol(PROTOCOL): subtransaction_begin, subtransaction_rollback or subtransaction_release 
                            or
                            debug_dtm_action_sql_command_tag(SQL) : 'MPPEXEC UPDATE'
                         4. dtm_action: fail_begin_command, fail_end_command or panic_begin_comand
        
        debug_dtm_action: Using this can specify what action to be triggered/simulated and at what point like 
                          error / panic / delay and at start or end command after receiving by the segment.

        debug_dtm_action_segment: Using this can specify segment number to trigger the specified dtm_action.

        debug_dtm_action_target: Allows to set target for specified dtm_action should it be DTM protocol 
                                 command or SQL command from master to segment.

        debug_dtm_action_protocol: Allows to specify sub-type of DTM protocol for which to perform specified 
                                   dtm_action (like prepare, abort_no_prepared, commit_prepared, abort_prepared, 
                                   subtransaction_begin, subtransaction_release, subtransaction_rollback, etc...
        debug_dtm_action_sql_command_tag: If debug_dtm_action_target is sql then this parameter can be used to set the type of sql
                                          that should trigger the exeception. Ex: 'MPPEXEC UPDATE'

        debug_dtm_action_nestinglevel: This allows to optional specify at which specific depth level in transaction 
                                       to take the specified dtm_action. This apples only to target with protocol and not SQL.
        STEPS:
        @data_provider data_types_provider
        '''
        tinctest.logger.info(
            "\n ===============================================")
        tinctest.logger.info("\n Starting New Test: %s%s " %
                             (self.test_data[0][0], self.test_data[0][1]))
        debug_dtm_action_segment = self.test_data[1][0]
        debug_dtm_action_target = self.test_data[1][1]
        debug_dtm_action_protocol = ''
        debug_dtm_action_sql_command_tag = ''
        if debug_dtm_action_target == 'protocol':
            debug_dtm_action_protocol = self.test_data[1][2]
        elif debug_dtm_action_target == 'sql':
            debug_dtm_action_sql_command_tag = self.test_data[1][2]
        debug_dtm_action = self.test_data[1][3]
        debug_dtm_action_nestinglevel = self.test_data[1][4]

        tinctest.logger.info('\ndebug_dtm_action_segment: %s' %
                             debug_dtm_action_segment)
        tinctest.logger.info('debug_dtm_action_target: %s' %
                             debug_dtm_action_target)
        tinctest.logger.info('debug_dtm_action_protocol: %s ' %
                             debug_dtm_action_protocol)
        tinctest.logger.info('debug_dtm_action_sql_command_tag: %s ' %
                             debug_dtm_action_sql_command_tag)
        tinctest.logger.info('debug_dtm_action: %s ' % debug_dtm_action)
        tinctest.logger.info('debug_dtm_action_nestinglevel: %s ' %
                             debug_dtm_action_nestinglevel)
        tinctest.logger.info(
            "\n ===============================================")

        self.udf_obj.reset_protocol_conf()
        if debug_dtm_action_target == 'protocol':
            self.udf_obj.set_protocol_conf(debug_dtm_action_segment,
                                           debug_dtm_action_target,
                                           debug_dtm_action_protocol,
                                           debug_dtm_action,
                                           debug_dtm_action_nestinglevel)
            self.udf_obj.run_test(debug_dtm_action_segment,
                                  debug_dtm_action_target,
                                  debug_dtm_action_protocol, debug_dtm_action,
                                  debug_dtm_action_nestinglevel)
        if debug_dtm_action_target == 'sql':
            self.udf_obj.set_sql_conf(debug_dtm_action_segment,
                                      debug_dtm_action_target,
                                      debug_dtm_action_sql_command_tag,
                                      debug_dtm_action,
                                      debug_dtm_action_nestinglevel)
            self.udf_obj.run_test(debug_dtm_action_segment,
                                  debug_dtm_action_target,
                                  debug_dtm_action_sql_command_tag,
                                  debug_dtm_action,
                                  debug_dtm_action_nestinglevel)