Commit | Line | Data |
4ffbc1d6 |
1 | use strict; |
2 | use warnings; |
3 | |
e7827df0 |
4 | use FindBin; |
5 | use File::Copy; |
4ffbc1d6 |
6 | use Test::More; |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | |
b5bf138f |
10 | plan tests => 6; |
e7827df0 |
11 | |
12 | my $db_orig = "$FindBin::Bin/var/DBIxClass.db"; |
13 | my $db_tmp = "$db_orig.tmp"; |
4ffbc1d6 |
14 | |
15 | # Set up the "usual" sqlite for DBICTest |
fcf741b1 |
16 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); |
4ffbc1d6 |
17 | |
18 | # Make sure we're connected by doing something |
19 | my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
20 | cmp_ok(@art, '==', 3, "Three artists returned"); |
21 | |
22 | # Disconnect the dbh, and be sneaky about it |
b5bf138f |
23 | # Also test if DBD::SQLite finaly knows how to ->disconnect properly |
24 | TODO: { |
25 | local $TODO = 'SQLite is evil/braindead. Once this test starts passing, remove the related atrocity from DBIx::Class::Storage::DBI::SQLite'; |
26 | my $w; |
27 | local $SIG{__WARN__} = sub { $w = shift }; |
28 | $schema->storage->_dbh->disconnect; |
29 | ok ($w !~ /active statement handles/, 'SQLite can disconnect properly \o/'); |
30 | } |
4ffbc1d6 |
31 | |
32 | # Try the operation again - What should happen here is: |
33 | # 1. S::DBI blindly attempts the SELECT, which throws an exception |
34 | # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state... |
35 | # 3. Reconnects, and retries the operation |
36 | # 4. Success! |
37 | my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
38 | cmp_ok(@art_two, '==', 3, "Three artists returned"); |
e7827df0 |
39 | |
40 | ### Now, disconnect the dbh, and move the db file; |
41 | # create a new one and chmod 000 to prevent SQLite from connecting. |
42 | $schema->storage->_dbh->disconnect; |
43 | move( $db_orig, $db_tmp ); |
44 | open DBFILE, '>', $db_orig; |
45 | print DBFILE 'THIS IS NOT A REAL DATABASE'; |
46 | close DBFILE; |
47 | chmod 0000, $db_orig; |
48 | |
49 | ### Try the operation again... it should fail, since there's no db |
b5bf138f |
50 | { |
51 | # Catch the DBI connection error |
52 | local $SIG{__WARN__} = sub {}; |
53 | eval { |
54 | my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); |
55 | }; |
56 | ok( $@, 'The operation failed' ); |
57 | } |
e7827df0 |
58 | |
59 | ### Now, move the db file back to the correct name |
60 | unlink($db_orig); |
61 | move( $db_tmp, $db_orig ); |
62 | |
413abe68 |
63 | SKIP: { |
64 | skip "Cannot reconnect if original connection didn't fail", 2 |
65 | if ( $@ =~ /encrypted or is not a database/ ); |
66 | |
67 | ### Try the operation again... this time, it should succeed |
68 | my @art_four; |
69 | eval { |
70 | @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); |
71 | }; |
72 | ok( !$@, 'The operation succeeded' ); |
73 | cmp_ok( @art_four, '==', 3, "Three artists returned" ); |
74 | } |