fix test failure in 05testapp.t when env var not set, release
[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
18b829f0 55plan tests => @tests / 2 + @invalid + 1 + 1;
7b1fe8c2 56
57# ignore redefined warnings, and uninitialized warnings from old
58# ::Storage::DBI::Replicated
59local $SIG{__WARN__} = sub {
60 $_[0] !~ /(?:redefined|uninitialized)/i && warn @_
61};
62
63for (my $i = 0; $i < @tests; $i += 2) {
64 my $m = instance(
65 connect_info => $tests[$i]
66 );
67
68 is_deeply $m->connect_info, $tests[$i+1],
69 'connect_info coerced correctly';
70}
71
72throws_ok { instance(connect_info => $_) } qr/valid connect_info/i,
73 'invalid connect_info throws exception'
74 for @invalid;
75
76# try as ConnectInfos (e.g.: replicants)
77my @replicants = map $tests[$_], grep $_ % 2 == 0, 0..$#tests;
78
79{
80 package TryConnectInfos;
81
82 use Moose;
83 use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos';
84
85 has replicants => (is => 'ro', isa => ConnectInfos, coerce => 1);
86}
87
88my $m = TryConnectInfos->new(
89 replicants => \@replicants
90);
91
92is_deeply $m->replicants, [
93 map $tests[$_], grep $_ % 2, 0 .. $#tests
94], 'replicant connect_infos coerced correctly';
95
18b829f0 96{
97 ASchemaClass->connection( @{$tests[0]} );
98 my $m = instance();
99
100 is_deeply $m->connect_info, $tests[1],
101 'connect_info coerced correctly when defining connection in the schema class';
102}
103
7b1fe8c2 104sub instance {
105 Catalyst::Model::DBIC::Schema->new({
106 schema_class => 'ASchemaClass',
107 @_
108 })
109}