Fix parsing DSN when the driver part includes DBI attributes
[dbsrgits/DBIx-Class.git] / t / storage / dbi_env.t
CommitLineData
c1807ed5 1use strict;
2use warnings;
3use lib qw(t/lib);
4use DBICTest;
5use Test::More;
6use Test::Exception;
37b5ab51 7use DBIx::Class::_Util 'sigwarn_silencer';
c1807ed5 8
9BEGIN { delete @ENV{qw(DBI_DSN DBI_DRIVER)} }
10
8d6b1478 11$ENV{DBICTEST_LOCK_HOLDER} = -1;
c1807ed5 12
8d6b1478 13# pre-populate
14my $schema = DBICTest->init_schema(sqlite_use_file => 1);
c1807ed5 15
16my $dbname = DBICTest->_sqlite_dbname(sqlite_use_file => 1);
17
18sub count_sheep {
19 my $schema = shift;
37b5ab51 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
c1807ed5 31 scalar $schema->resultset('Artist')->search( { name => "Exploding Sheep" } )
32 ->all;
33}
34
35$schema = DBICTest::Schema->connect("dbi::$dbname");
36throws_ok { count_sheep($schema) } qr{I can't work out what driver to use},
37 'Driver in DSN empty';
38isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
39
40$schema = DBICTest::Schema->connect("dbi:Test_NonExistant_DBD:$dbname");
41throws_ok { count_sheep($schema) }
42 qr{Can't locate DBD/Test_NonExistant_DBD\.pm in \@INC},
43 "Driver class doesn't exist";
44isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
45
46$ENV{DBI_DSN} = "dbi::$dbname";
47$schema = DBICTest::Schema->connect;
48throws_ok { count_sheep($schema) } qr{I can't work out what driver to use},
49 "Driver class not defined in DBI_DSN either.";
50isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
51
52$ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD2:$dbname";
53$schema = DBICTest::Schema->connect;
54throws_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";
57isa_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;
62throws_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";
65isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
66
67$ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD4:$dbname";
68$schema = DBICTest::Schema->connect;
69throws_ok { count_sheep($schema) }
70qr{Can't locate DBD/Test_NonExistant_DBD4\.pm in \@INC},
71 "Driver class defined in DBI_DSN doesn't exist";
72isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
73
74delete @ENV{qw(DBI_DSN DBI_DRIVER)};
75
76$schema = DBICTest::Schema->connect("dbi:SQLite:$dbname");
77lives_ok { count_sheep($schema) } 'SQLite passed to connect_info';
78isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
79
cae96f70 80$schema = DBICTest::Schema->connect("dbi:SQLite(ReadOnly=1):$dbname");
81lives_ok { count_sheep($schema) } 'SQLite passed to connect_info despite extra arguments present';
82isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
83
c1807ed5 84$ENV{DBI_DRIVER} = 'SQLite';
85$schema = DBICTest::Schema->connect("dbi::$dbname");
86lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER';
87isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
88
442cb03d 89delete $ENV{DBI_DRIVER};
c1807ed5 90$ENV{DBI_DSN} = "dbi:SQLite:$dbname";
91$schema = DBICTest::Schema->connect;
92lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN';
93isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
94
95$ENV{DBI_DRIVER} = 'SQLite';
96$schema = DBICTest::Schema->connect;
97lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN (and DBI_DRIVER)';
98isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
99
100$ENV{DBI_DSN} = "dbi::$dbname";
101$ENV{DBI_DRIVER} = 'SQLite';
102$schema = DBICTest::Schema->connect;
103lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER (not DBI_DSN)';
104isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
105
37b5ab51 106# make sure that dynamically setting DBI_DSN post-connect works
107{
108 local $ENV{DBI_DSN};
109
110 my $s = DBICTest::Schema->connect();
111
112 throws_ok {
113 $s->storage->ensure_connected
114 } qr/You did not provide any connection_info/,
115 'sensible exception on empty conninfo connect';
116
117 $ENV{DBI_DSN} = 'dbi:SQLite::memory:';
118
119 lives_ok { $s->storage->ensure_connected } 'Second connection attempt worked';
120 isa_ok ( $s->storage, 'DBIx::Class::Storage::DBI::SQLite' );
121}
122
c1807ed5 123done_testing;