61d67823791897282a4141445eb7cc7fb9b95f95
[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 DBICTest;
10
11 my $schema = DBICTest->init_schema;
12
13 warnings_are ( sub {
14   throws_ok (
15     sub {
16       $schema->resultset('CD')->create({ title => 'vacation in antarctica' })
17     },
18     qr/DBI Exception.+(?x:
19       \QNOT NULL constraint failed: cd.artist\E
20         |
21       \Qcd.artist may not be NULL\E
22     )/s
23   );  # as opposed to some other error
24 }, [], 'No warnings besides exception' );
25
26 my $dbh = $schema->storage->dbh;
27
28 throws_ok (
29   sub {
30     $dbh->do ('INSERT INTO nonexistent_table VALUES (1)')
31   },
32   qr/DBI Exception.+no such table.+nonexistent_table/s,
33   'DBI exceptions properly handled by dbic-installed callback'
34 );
35
36 # This usage is a bit unusual but it was actually seen in the wild
37 # destruction of everything except the $dbh should use the proper
38 # exception fallback:
39
40 SKIP: {
41   if (DBIx::Class::_ENV_::PEEPEENESS) {
42     skip "Your perl version $] appears to leak like a sieve - skipping garbage collected \$schema test", 1;
43   }
44
45   undef ($schema);
46   throws_ok (
47     sub {
48       $dbh->do ('INSERT INTO nonexistent_table VALUES (1)')
49     },
50     qr/DBI Exception.+unhandled by DBIC.+no such table.+nonexistent_table/s,
51     'callback works after $schema is gone'
52   );
53 }
54
55 done_testing;