Ignore MANIFEST.bak
[catagits/Catalyst-Model-DBIC-Schema.git] / t / 07connect_info.t
CommitLineData
7b1fe8c2 1use strict;
2use warnings;
3
4use FindBin '$Bin';
5use lib "$Bin/lib";
6
7use Test::More;
8use Test::Exception;
9use Catalyst::Model::DBIC::Schema;
10use ASchemaClass;
11
12# execise the connect_info coercion
13
2fa0a1f1 14my $coderef = sub {};
15
7b1fe8c2 16my @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 },
2fa0a1f1 43
44 [$coderef, { pg_enable_utf8 => 1, auto_savepoint => 1 }],
45 { dbh_maker => $coderef, pg_enable_utf8 => 1, auto_savepoint => 1 },
7b1fe8c2 46);
47
48my @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
7b1fe8c2 55# ignore redefined warnings, and uninitialized warnings from old
56# ::Storage::DBI::Replicated
57local $SIG{__WARN__} = sub {
58 $_[0] !~ /(?:redefined|uninitialized)/i && warn @_
59};
60
61for (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
70throws_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)
75my @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
86my $m = TryConnectInfos->new(
87 replicants => \@replicants
88);
89
626adb9c 90lives_and {
91 is_deeply(TryConnectInfos->new(replicants => $tests[1])->replicants,
92 [ $tests[1] ])
93} 'single replicant hashref coerces correctly';
94
7b1fe8c2 95is_deeply $m->replicants, [
96 map $tests[$_], grep $_ % 2, 0 .. $#tests
97], 'replicant connect_infos coerced correctly';
98
18b829f0 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
626adb9c 107done_testing;
108
7b1fe8c2 109sub instance {
110 Catalyst::Model::DBIC::Schema->new({
111 schema_class => 'ASchemaClass',
112 @_
113 })
114}