DB-Transaction

 view release on metacpan or  search on metacpan

t/transaction.t  view on Meta::CPAN

					1;
				} db_handle => $stub_dbh, on_error => $args{on_error};
			};
			ok( ! $committed->(), "inner transactions don't commit" );

			if ($args{is_inner_die}) {
				ok( ! $ok, 'failures in transactions are messaged back to caller' );

				if ($args{on_error} eq 'continue') {
					ok( ! $rolled_back->(), 'did not roll back nested transaction (on_error => continue)' );
				} else {
					ok( $rolled_back->(), "rolled back on failed inner transaction, $args{on_error}" );
				}
			} else {
				ok( $ok, 'success in transactions is messaged back to caller' );
				is( $@, '', 'no assertions visible' );
				ok( ! $rolled_back->(), 'rolled back nested transaction (on_error => rollback)' );
			}

		} db_handle => $stub_dbh;

		ok( $committed->(), 'committed outer transaction despite a failure' );
		if ($args{on_error} eq 'continue') {
			ok( ! $rolled_back->(), 'did not roll back outer transaction (recovered, presumably)' );
		} else {
			ok( scalar @{$stub_dbh->{method_calls}{rollback} || []} <= 1, 'rolled back inner transaction but not outer transaction' );
		}
	};

	$fun->(
		is_inner_die => 1,
		on_error => 'continue',
	);

	$fun->(
		is_inner_die => 0,
		on_error => 'continue',
	);

	$fun->(
		is_inner_die => 0,
		on_error => 'rollback',
	);

	$fun->(
		is_inner_die => 1,
		on_error => 'rollback',
	);
}

# here's a bad package that does something naughty
{
	package bad::citizen;
	sub new { bless \@_ }
	sub DESTROY { undef $@ }
	sub AUTOLOAD {}
}

# On some versions of perl, destroyers may unset $@; we're immune to it.
{
	my $expected_exceptions = qr/(?:the hills are alive|an error was encountered in your transaction)/;

	{
		local $@;
		eval {
			run_in_transaction {
				my $fake_dbh = bad::citizen->new;
				die 'the hills are alive';
			} db_handle => $stub_dbh, on_error => 'rollback';
		};

		like( $@, $expected_exceptions, 'if $@ is unintentionally unset on object destruction, we set a sensible default error' );
	}

	{
		local $@;
		run_in_transaction {
			my $fake_dbh = bad::citizen->new;
			die 'the hills are alive';
		} db_handle => $stub_dbh, on_error => 'continue';

		like( $@, $expected_exceptions, 'if $@ is unintentionally unset on object destruction, we set a sensible default error' );
	}
}



( run in 0.596 second using v1.01-cache-2.11-cpan-39bf76dae61 )