WIP: Make ->count call ->count_rs->first unless software_limit=1
[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
80$ENV{DBI_DRIVER} = 'SQLite';
81$schema = DBICTest::Schema->connect("dbi::$dbname");
82lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER';
83isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
84
442cb03d 85delete $ENV{DBI_DRIVER};
c1807ed5 86$ENV{DBI_DSN} = "dbi:SQLite:$dbname";
87$schema = DBICTest::Schema->connect;
88lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN';
89isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
90
91$ENV{DBI_DRIVER} = 'SQLite';
92$schema = DBICTest::Schema->connect;
93lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN (and DBI_DRIVER)';
94isa_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;
99lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER (not DBI_DSN)';
100isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite';
101
37b5ab51 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
c1807ed5 119done_testing;