Streamline connection codepath, fix $ENV{DBI_DSN} regression from d87929a4
[dbsrgits/DBIx-Class.git] / t / storage / dbi_env.t
1 use strict;
2 use warnings;
3 use lib qw(t/lib);
4 use DBICTest;
5 use Test::More;
6 use Test::Exception;
7 use DBIx::Class::_Util 'sigwarn_silencer';
8
9 BEGIN { delete @ENV{qw(DBI_DSN DBI_DRIVER)} }
10
11 $ENV{DBICTEST_LOCK_HOLDER} = -1;
12
13 # pre-populate
14 my $schema = DBICTest->init_schema(sqlite_use_file => 1);
15
16 my $dbname = DBICTest->_sqlite_dbname(sqlite_use_file => 1);
17
18 sub count_sheep {
19     my $schema = shift;
20
21     local $SIG{__WARN__} = sigwarn_silencer(
22       qr/
23         \QThis version of DBIC does not yet seem to supply a driver for your particular RDBMS\E
24           |
25         \QUnable to extract a driver name from connect info\E
26           |
27         \QYour storage class (DBIx::Class::Storage::DBI) does not set sql_limit_dialect\E
28       /x
29     );
30
31     scalar $schema->resultset('Artist')->search( { name => "Exploding Sheep" } )
32         ->all;
33 }
34
35 $schema = DBICTest::Schema->connect("dbi::$dbname");
36 throws_ok { count_sheep($schema) } qr{I can't work out what driver to use},
37     'Driver in DSN empty';
38 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
39
40 $schema = DBICTest::Schema->connect("dbi:Test_NonExistant_DBD:$dbname");
41 throws_ok { count_sheep($schema) }
42     qr{Can't locate DBD/Test_NonExistant_DBD\.pm in \@INC},
43     "Driver class doesn't exist";
44 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
45
46 $ENV{DBI_DSN} = "dbi::$dbname";
47 $schema = DBICTest::Schema->connect;
48 throws_ok { count_sheep($schema) } qr{I can't work out what driver to use},
49     "Driver class not defined in DBI_DSN either.";
50 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
51
52 $ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD2:$dbname";
53 $schema = DBICTest::Schema->connect;
54 throws_ok { count_sheep($schema) }
55     qr{Can't locate DBD/Test_NonExistant_DBD2\.pm in \@INC},
56     "Driver class defined in DBI_DSN doesn't exist";
57 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
58
59 $ENV{DBI_DSN} = "dbi::$dbname";
60 $ENV{DBI_DRIVER} = 'Test_NonExistant_DBD3';
61 $schema = DBICTest::Schema->connect;
62 throws_ok { count_sheep($schema) }
63     qr{Can't locate DBD/Test_NonExistant_DBD3\.pm in \@INC},
64     "Driver class defined in DBI_DRIVER doesn't exist";
65 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
66
67 $ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD4:$dbname";
68 $schema = DBICTest::Schema->connect;
69 throws_ok { count_sheep($schema) }
70 qr{Can't locate DBD/Test_NonExistant_DBD4\.pm in \@INC},
71     "Driver class defined in DBI_DSN doesn't exist";
72 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
73
74 delete @ENV{qw(DBI_DSN DBI_DRIVER)};
75
76 $schema = DBICTest::Schema->connect("dbi:SQLite:$dbname");
77 lives_ok { count_sheep($schema) } 'SQLite passed to connect_info';
78 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
79
80 $ENV{DBI_DRIVER} = 'SQLite';
81 $schema = DBICTest::Schema->connect("dbi::$dbname");
82 lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER';
83 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
84
85 delete $ENV{DBI_DRIVER};
86 $ENV{DBI_DSN} = "dbi:SQLite:$dbname";
87 $schema = DBICTest::Schema->connect;
88 lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN';
89 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
90
91 $ENV{DBI_DRIVER} = 'SQLite';
92 $schema = DBICTest::Schema->connect;
93 lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN (and DBI_DRIVER)';
94 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
95
96 $ENV{DBI_DSN} = "dbi::$dbname";
97 $ENV{DBI_DRIVER} = 'SQLite';
98 $schema = DBICTest::Schema->connect;
99 lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER (not DBI_DSN)';
100 isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
101
102 # make sure that dynamically setting DBI_DSN post-connect works
103 {
104   local $ENV{DBI_DSN};
105
106   my $s = DBICTest::Schema->connect();
107
108   throws_ok {
109     $s->storage->ensure_connected
110   } qr/You did not provide any connection_info/,
111   'sensible exception on empty conninfo connect';
112
113   $ENV{DBI_DSN} = 'dbi:SQLite::memory:';
114
115   lives_ok { $s->storage->ensure_connected } 'Second connection attempt worked';
116   isa_ok ( $s->storage, 'DBIx::Class::Storage::DBI::SQLite' );
117 }
118
119 done_testing;