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