0beb8581ebec45bdcbbe30082ef08b5887db8dd9
[dbsrgits/DBIx-Class.git] / t / storage / dbh_do.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8
9 my $schema = DBICTest->init_schema();
10 my $storage = $schema->storage;
11
12 # test (re)connection
13 for my $disconnect (0, 1) {
14   $schema->storage->_dbh->disconnect if $disconnect;
15   is_deeply (
16     $schema->storage->dbh_do(sub { $_[1]->selectall_arrayref('SELECT 1') }),
17     [ [ 1 ] ],
18     'dbh_do on fresh handle worked',
19   );
20 }
21
22 my @args;
23 my $test_func = sub { @args = @_ };
24
25 $storage->dbh_do($test_func, "foo", "bar");
26 is_deeply (
27   \@args,
28   [ $storage, $storage->dbh, "foo", "bar" ],
29 );
30
31
32 my $storage_class = ref $storage;
33 {
34   no strict 'refs';
35   local *{$storage_class .'::__test_method'} = $test_func;
36   $storage->dbh_do("__test_method", "baz", "buz");
37 }
38
39 is_deeply (
40   \@args,
41   [ $storage, $storage->dbh, "baz", "buz" ],
42 );
43
44 # test nested aliasing
45 my $res = 'original';
46 $storage->dbh_do (sub {
47   shift->dbh_do(sub { $_[3] = 'changed' }, @_)
48 }, $res);
49
50 is ($res, 'changed', "Arguments properly aliased for dbh_do");
51
52 done_testing;