From 4aab91eab824c82179abc3ae9b99bd01d422a0b2 Mon Sep 17 00:00:00 2001 From: Jacek Date: Tue, 17 Oct 2017 23:05:37 +0100 Subject: [PATCH] Do not open new transaction when getting called. With this change all calls to `ut.run` are in autonomous transaction. This way, utPLSQL does not create a new transaction if a transaction was not opened in the calling session. --- source/api/ut.pkb | 40 ++++++------- source/api/ut_runner.pkb | 20 ++++--- test/ut_runner/test_ut_runner.pkb | 99 +++++++++++++++++++++---------- test/ut_runner/test_ut_runner.pks | 9 ++- 4 files changed, 106 insertions(+), 62 deletions(-) diff --git a/source/api/ut.pkb b/source/api/ut.pkb index 3ff89c9d6..95220a5a4 100644 --- a/source/api/ut.pkb +++ b/source/api/ut.pkb @@ -259,55 +259,55 @@ create or replace package body ut is end; procedure run( - a_reporter ut_reporter_base := null, a_color_console boolean := false, + a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false, a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null, a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null ) is + l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter()); begin - ut.run( - ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console, - a_coverage_schemes, a_source_file_mappings, a_test_file_mappings, a_include_objects, a_exclude_objects + run_autonomous( + a_paths, l_reporter, ut_utils.boolean_to_int(a_color_console), a_coverage_schemes, a_source_file_mappings, a_test_file_mappings, + a_include_objects, a_exclude_objects ); + ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id); end; procedure run( - a_reporter ut_reporter_base := null, a_color_console boolean := false, + a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false, a_coverage_schemes ut_varchar2_list := null, a_source_files ut_varchar2_list, a_test_files ut_varchar2_list, a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null ) is + l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter()); begin - ut.run( - ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console, - a_coverage_schemes, a_source_files, a_test_files, a_include_objects, a_exclude_objects + run_autonomous( + a_paths, l_reporter, ut_utils.boolean_to_int(a_color_console), a_coverage_schemes, a_source_files, a_test_files, + a_include_objects, a_exclude_objects ); + ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id); end; procedure run( - a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false, + a_reporter ut_reporter_base := null, a_color_console boolean := false, a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null, a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null ) is - l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter()); begin - ut_runner.run( - a_paths, l_reporter, a_color_console, a_coverage_schemes, a_source_file_mappings, a_test_file_mappings, - a_include_objects, a_exclude_objects + ut.run( + ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console, + a_coverage_schemes, a_source_file_mappings, a_test_file_mappings, a_include_objects, a_exclude_objects ); - ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id); end; procedure run( - a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false, + a_reporter ut_reporter_base := null, a_color_console boolean := false, a_coverage_schemes ut_varchar2_list := null, a_source_files ut_varchar2_list, a_test_files ut_varchar2_list, a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null ) is - l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter()); begin - ut_runner.run( - a_paths, l_reporter, a_color_console, a_coverage_schemes, a_source_files, a_test_files, - a_include_objects, a_exclude_objects + ut.run( + ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console, + a_coverage_schemes, a_source_files, a_test_files, a_include_objects, a_exclude_objects ); - ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id); end; procedure run( diff --git a/source/api/ut_runner.pkb b/source/api/ut_runner.pkb index e3141b527..d85071ac2 100644 --- a/source/api/ut_runner.pkb +++ b/source/api/ut_runner.pkb @@ -33,6 +33,12 @@ create or replace package body ut_runner is return l_result; end; + procedure finish_run(a_reporters ut_reporters) is + begin + ut_utils.cleanup_temp_tables; + ut_output_buffer.close(a_reporters); + ut_metadata.reset_source_definition_cache; + end; /** @@ -61,8 +67,8 @@ create or replace package body ut_runner is a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null, a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null, a_fail_on_errors boolean default false ) is - l_items_to_run ut_run; - l_listener ut_event_listener; + l_items_to_run ut_run; + l_listener ut_event_listener; begin begin ut_output_buffer.cleanup_buffer(); @@ -84,14 +90,10 @@ create or replace package body ut_runner is ); l_items_to_run.do_execute(l_listener); - ut_utils.cleanup_temp_tables; - ut_output_buffer.close(l_listener.reporters); - ut_metadata.reset_source_definition_cache; - exception + finish_run(l_listener.reporters); + exception when others then - ut_utils.cleanup_temp_tables; - ut_output_buffer.close(l_listener.reporters); - ut_metadata.reset_source_definition_cache; + finish_run(l_listener.reporters); dbms_output.put_line(dbms_utility.format_error_backtrace); dbms_output.put_line(dbms_utility.format_error_stack); raise; diff --git a/test/ut_runner/test_ut_runner.pkb b/test/ut_runner/test_ut_runner.pkb index 5c8e4fb26..b6ec8485e 100644 --- a/test/ut_runner/test_ut_runner.pkb +++ b/test/ut_runner/test_ut_runner.pkb @@ -1,5 +1,72 @@ create or replace package body test_ut_runner is + procedure create_test_spec + as + pragma autonomous_transaction; + begin + execute immediate q'[create or replace package test_cache as + --%suite + + --%test + procedure failing_test; +end; +]'; + end; + + procedure create_test_body(a_number integer) + as + pragma autonomous_transaction; + begin + execute immediate 'create or replace package body test_cache as + procedure failing_test is + begin + ut3.ut.expect('||a_number||').to_be_null; + end; +end;'; + end; + + procedure drop_test_package + as + pragma autonomous_transaction; + begin + execute immediate 'drop package test_cache'; + end; + + + + procedure keep_an_open_transaction is + l_expected varchar2(300); + l_output_data dbms_output.chararr; + l_num_lines integer := 100000; + begin + --Arrange + create_test_spec(); + create_test_body(0); + l_expected := dbms_transaction.local_transaction_id(true); + --Act + ut3.ut.run('test_cache'); + dbms_output.get_lines( l_output_data, l_num_lines); + --Assert + ut.expect(dbms_transaction.local_transaction_id()).to_equal(l_expected); + drop_test_package(); + end; + + procedure close_newly_opened_transaction is + l_output_data dbms_output.chararr; + l_num_lines integer := 100000; + begin + --Arrange + create_test_spec(); + create_test_body(0); + rollback; + --Act + ut3.ut.run('test_cache'); + dbms_output.get_lines( l_output_data, l_num_lines); + --Assert + ut.expect(dbms_transaction.local_transaction_id()).to_be_null(); + drop_test_package(); + end; + procedure version_comp_check_compare is begin ut.expect( ut3.ut_runner.version_compatibility_check('v3.0.0.0','v3.0.0.0') ).to_equal(1); @@ -41,38 +108,6 @@ create or replace package body test_ut_runner is throws('v3.0.0.0','bad_ver'); end; - procedure create_test_spec - as - pragma autonomous_transaction; - begin - execute immediate q'[create or replace package test_cache as - --%suite - - --%test - procedure failing_test; -end; -]'; - end; - - procedure create_test_body(a_number integer) - as - pragma autonomous_transaction; - begin - execute immediate 'create or replace package body test_cache as - procedure failing_test is - begin - ut3.ut.expect('||a_number||').to_be_null; - end; -end;'; - end; - - procedure drop_test_package - as - pragma autonomous_transaction; - begin - execute immediate 'drop package test_cache'; - end; - procedure run_reset_package_body_cache is l_results ut3.ut_varchar2_list; l_expected clob; diff --git a/test/ut_runner/test_ut_runner.pks b/test/ut_runner/test_ut_runner.pks index 993cd1770..cdfa250ae 100644 --- a/test/ut_runner/test_ut_runner.pks +++ b/test/ut_runner/test_ut_runner.pks @@ -1,7 +1,14 @@ create or replace package test_ut_runner is --%suite(ut_runner) - --%suitepath(utplsql.core) + --%suitepath(utplsql) + --%rollback(manual) + + --%test(transaction stays open after the run if it was opened before the run) + procedure keep_an_open_transaction; + + --%test(closes open transactions if no transaction was open before run) + procedure close_newly_opened_transaction; --%test(version_compatibility_check compares major, minor and bugfix number) procedure version_comp_check_compare;