DBIx-Class-Helpers

 view release on metacpan or  search on metacpan

t/Row/OnColumnChange.t  view on Meta::CPAN

#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Deep;
use Test::Fatal 'exception', 'dies_ok';;

use TestSchema;
use TestSchema::Result::Bar;
my $schema = TestSchema->deploy_or_connect();
$schema->prepopulate;

like(
   exception {
      TestSchema::Result::Bar->after_column_change(
         foo_id => {
            method => sub { 1; }
         },
         id => {
            method => sub { 1; }
         },
      );
   },
   qr/Invalid number of arguments\. One \$column => \$args pair at a time\./,
);

TestSchema::Result::Bar->after_column_change(
   foo_id => {
      method => sub { push @TestSchema::Result::Bar::events, [after_foo_id => $_[1], $_[2]] }
   },
);

TestSchema::Result::Bar->after_column_change(
   id => {
      method => sub {
         is($schema->storage->{transaction_depth}, 1, 'transactions turned on for id');
         push @TestSchema::Result::Bar::events, [after_id => $_[1], $_[2]]
      },
      txn_wrap => 1,
   },
);

my $another_txn_test = sub {
   is($schema->storage->{transaction_depth}, 0, 'transactions turned off for non-txn')
};

TestSchema::Result::Bar->around_column_change(
   foo_id => {
      method => sub {
         my ( $self, $fn, $old, $new ) = @_;
         push @TestSchema::Result::Bar::events, [pre_around_foo_id => $old, $new];
         $another_txn_test->();
         $fn->();
         push @TestSchema::Result::Bar::events, [post_around_foo_id => $old, $new];
      },
   },
);

my $first = $schema->resultset('Bar')->search(undef, { order_by => 'id' })->first;

is($first->foo_id, 1, 'foo_id starts as 1');
$first->foo_id(2);
$first->update;
is($first->foo_id, 2, 'foo_id is updated to 2');

$another_txn_test = sub {};



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