File: on_connect_call.t

package info (click to toggle)
libdbix-class-perl 0.08123-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,520 kB
  • ctags: 1,695
  • sloc: perl: 19,821; sql: 353; makefile: 10
file content (98 lines) | stat: -rw-r--r-- 2,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use strict;
use warnings;
no warnings qw/once redefine/;

use lib qw(t/lib);
use DBI;
use DBICTest;
use DBICTest::Schema;
use DBIx::Class::Storage::DBI;

# !!! do not replace this with done_testing - tests reside in the callbacks
# !!! number of calls is important
use Test::More tests => 16;
# !!!

my $schema = DBICTest::Schema->clone;

{
  *DBIx::Class::Storage::DBI::connect_call_foo = sub {
    isa_ok $_[0], 'DBIx::Class::Storage::DBI',
      'got storage in connect_call method';
    is $_[1], 'bar', 'got param in connect_call method';
  };

  *DBIx::Class::Storage::DBI::disconnect_call_foo = sub {
    isa_ok $_[0], 'DBIx::Class::Storage::DBI',
      'got storage in disconnect_call method';
  };

  ok $schema->connection(
      DBICTest->_database,
    {
      on_connect_call => [
          [ do_sql => 'create table test1 (id integer)' ],
          [ do_sql => [ 'insert into test1 values (?)', {}, 1 ] ],
          [ do_sql => sub { ['insert into test1 values (2)'] } ],
          [ sub { $_[0]->dbh->do($_[1]) }, 'insert into test1 values (3)' ],
          # this invokes $storage->connect_call_foo('bar') (above)
          [ foo => 'bar' ],
      ],
      on_connect_do => 'insert into test1 values (4)',
      on_disconnect_call => 'foo',
    },
  ), 'connection()';

  ok (! $schema->storage->connected, 'start disconnected');

  is_deeply (
    $schema->storage->dbh->selectall_arrayref('select * from test1'),
    [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ],
    'on_connect_call/do actions worked'
  );

  $schema->storage->disconnect;
}

{
  *DBIx::Class::Storage::DBI::connect_call_foo = sub {
    isa_ok $_[0], 'DBIx::Class::Storage::DBI',
      'got storage in connect_call method';
  };

  *DBIx::Class::Storage::DBI::connect_call_bar = sub {
    isa_ok $_[0], 'DBIx::Class::Storage::DBI',
      'got storage in connect_call method';
  };


  ok $schema->connection(
    DBICTest->_database,
    {
      # method list form
      on_connect_call => [ 'foo', sub { ok 1, "coderef in list form" }, 'bar' ],
    },
  ), 'connection()';

  ok (! $schema->storage->connected, 'start disconnected');
  $schema->storage->ensure_connected;
  $schema->storage->disconnect; # this should not fire any tests
}

{
  ok $schema->connection(
    sub { DBI->connect(DBICTest->_database) },
    {
      # method list form
      on_connect_call => [ sub { ok 1, "on_connect_call after DT parser" }, ],
      on_disconnect_call => [ sub { ok 1, "on_disconnect_call after DT parser" }, ],
    },
  ), 'connection()';

  ok (! $schema->storage->connected, 'start disconnected');

  $schema->storage->_determine_driver;  # this should connect due to the coderef

  ok ($schema->storage->connected, 'determine driver connects');
  $schema->storage->disconnect;
}