DBIx-Class-Storage-TxnEndHook

 view release on metacpan or  search on metacpan

t/01_basic.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use lib qw(t/lib);
use MyApp::Schema;
use Test::Fatal;

sub gen_schema {
    my $schema = MyApp::Schema->connect("dbi:SQLite::memory:", "", "", {
        sqlite_use_immediate_transaction => 1,
    });
}

subtest 'add_txn_end_hook should call in transaction' => sub {
    my $schema = gen_schema();

    like(
        exception {
            $schema->storage->add_txn_end_hook(sub {});
        },
        qr/only can call add_txn_end_hook in transaction/,
        "die if called add_txn_end_hook method without transaction",
    );
};

subtest 'DBIx::Schema->txn_do style' => sub {
    my $schema = gen_schema();
    my $call_count = 0;

    $schema->txn_do(sub{
            $schema->storage->add_txn_end_hook(sub {
                    $call_count++;
                });
            is $call_count, 0, "not yet call";
            is @{ $schema->storage->_hooks }, 1, "hooks count is 1";
        });

    is $call_count, 1, "add_txn_end_hook is called";
    is @{ $schema->storage->_hooks }, 0, "all hooks is executed";
};

subtest 'DBIx::Schema->txn_begin and txn_commit style' => sub {
    my $schema = gen_schema();
    my $call_count = 0;

    $schema->txn_begin;
    $schema->storage->add_txn_end_hook(sub {
        $call_count++;
    });
    is $call_count, 0, "not yet call";
    is @{ $schema->storage->_hooks }, 1, "hooks count is 1";

    $schema->txn_commit;

    is $call_count, 1, "called";
    is @{ $schema->storage->_hooks }, 0, "all hooks is executed";
};

subtest 'DBIx::Schema->storage->txn_begin and txn_commit style' => sub {
    my $schema = gen_schema();
    my $call_count = 0;

    $schema->storage->txn_begin;
    $schema->storage->add_txn_end_hook(sub {
        $call_count++;
    });



( run in 0.854 second using v1.01-cache-2.11-cpan-54e63673c56 )