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