release 0.63
[catagits/Catalyst-Model-DBIC-Schema.git] / t / 07connect_info.t
1 use strict;
2 use warnings;
3
4 use FindBin '$Bin';
5 use lib "$Bin/lib";
6
7 use Test::More;
8 use Test::Exception;
9 use Catalyst::Model::DBIC::Schema;
10 use ASchemaClass;
11
12 # execise the connect_info coercion
13
14 my $coderef = sub {};
15
16 my @tests = (
17     ['dbi:SQLite:foo.db', '', ''],
18     { dsn => 'dbi:SQLite:foo.db', user => '', password => '' },
19
20     ['dbi:SQLite:foo.db', ''],
21     { dsn => 'dbi:SQLite:foo.db', user => '', password => '' },
22
23     ['dbi:SQLite:foo.db'],
24     { dsn => 'dbi:SQLite:foo.db', user => '', password => '' },
25
26     'dbi:SQLite:foo.db',
27     { dsn => 'dbi:SQLite:foo.db', user => '', password => '' },
28
29     ['dbi:Pg:dbname=foo', 'user', 'pass',
30         { pg_enable_utf8 => 1, auto_savepoint => 1 }],
31     { dsn => 'dbi:Pg:dbname=foo', user => 'user', password => 'pass',
32         pg_enable_utf8 => 1, auto_savepoint => 1 },
33
34     ['dbi:Pg:dbname=foo', 'user', 'pass',
35         { pg_enable_utf8 => 1 }, { auto_savepoint => 1 }],
36     { dsn => 'dbi:Pg:dbname=foo', user => 'user', password => 'pass',
37         pg_enable_utf8 => 1, auto_savepoint => 1 },
38
39     [ { dsn => 'dbi:Pg:dbname=foo', user => 'user', password => 'pass',
40         pg_enable_utf8 => 1, auto_savepoint => 1 } ],
41     { dsn => 'dbi:Pg:dbname=foo', user => 'user', password => 'pass',
42         pg_enable_utf8 => 1, auto_savepoint => 1 },
43
44     [$coderef, { pg_enable_utf8 => 1, auto_savepoint => 1 }],
45     { dbh_maker => $coderef, pg_enable_utf8 => 1, auto_savepoint => 1 },
46 );
47
48 my @invalid = (
49     { foo => 'bar' },
50     [ { foo => 'bar' } ],
51     ['dbi:Pg:dbname=foo', 'user', 'pass',
52         { pg_enable_utf8 => 1 }, { AutoCommit => 1 }, { auto_savepoint => 1 }],
53 );
54
55 # ignore redefined warnings, and uninitialized warnings from old
56 # ::Storage::DBI::Replicated
57 local $SIG{__WARN__} = sub {
58     $_[0] !~ /(?:redefined|uninitialized)/i && warn @_
59 };
60
61 for (my $i = 0; $i < @tests; $i += 2) {
62     my $m = instance(
63         connect_info => $tests[$i]
64     );
65
66     is_deeply $m->connect_info, $tests[$i+1],
67         'connect_info coerced correctly';
68 }
69
70 throws_ok { instance(connect_info => $_) } qr/valid connect_info/i,
71     'invalid connect_info throws exception'
72     for @invalid;
73
74 # try as ConnectInfos (e.g.: replicants)
75 my @replicants = map $tests[$_], grep $_ % 2 == 0, 0..$#tests;
76
77 {
78     package TryConnectInfos;
79
80     use Moose;
81     use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos';
82
83     has replicants => (is => 'ro', isa => ConnectInfos, coerce => 1);
84 }
85
86 my $m = TryConnectInfos->new(
87     replicants   => \@replicants
88 );
89
90 lives_and {
91     is_deeply(TryConnectInfos->new(replicants => $tests[1])->replicants,
92         [ $tests[1] ])
93 } 'single replicant hashref coerces correctly';
94
95 is_deeply $m->replicants, [
96     map $tests[$_], grep $_ % 2, 0 .. $#tests
97 ], 'replicant connect_infos coerced correctly';
98
99 {
100     ASchemaClass->connection( @{$tests[0]} );
101     my $m = instance();
102
103     is_deeply $m->connect_info, $tests[1],
104         'connect_info coerced correctly when defining connection in the schema class';
105 }
106
107 done_testing;
108
109 sub instance {
110     Catalyst::Model::DBIC::Schema->new({
111         schema_class => 'ASchemaClass',
112         @_
113     })
114 }