Fix the join/prefetch resolver when dealing with ''/undef/()
[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;
7
8BEGIN { delete @ENV{qw(DBI_DSN DBI_DRIVER)} }
9
10my $schema;
11
12DBICTest->init_schema(sqlite_use_file => 1);
13
14my $dbname = DBICTest->_sqlite_dbname(sqlite_use_file => 1);
15
16sub count_sheep {
17 my $schema = shift;
18 scalar $schema->resultset('Artist')->search( { name => "Exploding Sheep" } )
19 ->all;
20}
21
22$schema = DBICTest::Schema->connect("dbi::$dbname");
23throws_ok { count_sheep($schema) } qr{I can't work out what driver to use},
24 'Driver in DSN empty';
25isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
26
27$schema = DBICTest::Schema->connect("dbi:Test_NonExistant_DBD:$dbname");
28throws_ok { count_sheep($schema) }
29 qr{Can't locate DBD/Test_NonExistant_DBD\.pm in \@INC},
30 "Driver class doesn't exist";
31isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
32
33$ENV{DBI_DSN} = "dbi::$dbname";
34$schema = DBICTest::Schema->connect;
35throws_ok { count_sheep($schema) } qr{I can't work out what driver to use},
36 "Driver class not defined in DBI_DSN either.";
37isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
38
39$ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD2:$dbname";
40$schema = DBICTest::Schema->connect;
41throws_ok { count_sheep($schema) }
42 qr{Can't locate DBD/Test_NonExistant_DBD2\.pm in \@INC},
43 "Driver class defined in DBI_DSN doesn't exist";
44isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
45
46$ENV{DBI_DSN} = "dbi::$dbname";
47$ENV{DBI_DRIVER} = 'Test_NonExistant_DBD3';
48$schema = DBICTest::Schema->connect;
49throws_ok { count_sheep($schema) }
50 qr{Can't locate DBD/Test_NonExistant_DBD3\.pm in \@INC},
51 "Driver class defined in DBI_DRIVER doesn't exist";
52isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
53
54$ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD4:$dbname";
55$schema = DBICTest::Schema->connect;
56throws_ok { count_sheep($schema) }
57qr{Can't locate DBD/Test_NonExistant_DBD4\.pm in \@INC},
58 "Driver class defined in DBI_DSN doesn't exist";
59isa_ok $schema->storage, 'DBIx::Class::Storage::DBI';
60
61delete @ENV{qw(DBI_DSN DBI_DRIVER)};
62
63$schema = DBICTest::Schema->connect("dbi:SQLite:$dbname");
64lives_ok { count_sheep($schema) } 'SQLite passed to connect_info';
65isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
66
67$ENV{DBI_DRIVER} = 'SQLite';
68$schema = DBICTest::Schema->connect("dbi::$dbname");
69lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER';
70isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
71
442cb03d 72delete $ENV{DBI_DRIVER};
c1807ed5 73$ENV{DBI_DSN} = "dbi:SQLite:$dbname";
74$schema = DBICTest::Schema->connect;
75lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN';
76isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
77
78$ENV{DBI_DRIVER} = 'SQLite';
79$schema = DBICTest::Schema->connect;
80lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN (and DBI_DRIVER)';
81isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
82
83$ENV{DBI_DSN} = "dbi::$dbname";
84$ENV{DBI_DRIVER} = 'SQLite';
85$schema = DBICTest::Schema->connect;
86lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER (not DBI_DSN)';
87isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
88
89done_testing;