fix and regression test for RT #62642
[dbsrgits/DBIx-Class.git] / t / storage / error.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Warn;
6 use Test::Exception;
7
8 use lib qw(t/lib);
9 use_ok( 'DBICTest' );
10 use_ok( 'DBICTest::Schema' );
11
12 my $schema = DBICTest->init_schema;
13
14 warnings_are ( sub {
15   throws_ok (
16     sub {
17       $schema->resultset('CD')->create({ title => 'vacation in antarctica' })
18     },
19     qr/DBI Exception.+constraint failed.+cd\.artist.+NULL/s
20   );  # as opposed to some other error
21 }, [], 'No warnings besides exception' );
22
23 my $dbh = $schema->storage->dbh;
24
25 throws_ok (
26   sub {
27     $dbh->do ('INSERT INTO nonexistent_table VALUES (1)')
28   },
29   qr/DBI Exception.+no such table.+nonexistent_table/s,
30   'DBI exceptions properly handled by dbic-installed callback'
31 );
32
33 # This usage is a bit unusual but it was actually seen in the wild
34 # destruction of everything except the $dbh should use the proper
35 # exception fallback:
36
37 {
38   undef ($schema);
39   throws_ok (
40     sub {
41       $dbh->do ('INSERT INTO nonexistent_table VALUES (1)')
42     },
43     qr/DBI Exception.+unhandled by DBIC.+no such table.+nonexistent_table/s,
44     'callback works after $schema is gone'
45   );
46 }
47
48 done_testing;