final changes for 08004 dist
[dbsrgits/DBIx-Class.git] / t / 33storage_reconnect.t
CommitLineData
4ffbc1d6 1use strict;
2use warnings;
3
e7827df0 4use FindBin;
5use File::Copy;
4ffbc1d6 6use Test::More;
7use lib qw(t/lib);
8use DBICTest;
9
e7827df0 10plan tests => 5;
11
12my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
13my $db_tmp = "$db_orig.tmp";
4ffbc1d6 14
15# Set up the "usual" sqlite for DBICTest
16my $schema = DBICTest->init_schema;
17
18# Make sure we're connected by doing something
19my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
20cmp_ok(@art, '==', 3, "Three artists returned");
21
22# Disconnect the dbh, and be sneaky about it
23$schema->storage->_dbh->disconnect;
24
25# Try the operation again - What should happen here is:
26# 1. S::DBI blindly attempts the SELECT, which throws an exception
27# 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
28# 3. Reconnects, and retries the operation
29# 4. Success!
30my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
31cmp_ok(@art_two, '==', 3, "Three artists returned");
e7827df0 32
33### Now, disconnect the dbh, and move the db file;
34# create a new one and chmod 000 to prevent SQLite from connecting.
35$schema->storage->_dbh->disconnect;
36move( $db_orig, $db_tmp );
37open DBFILE, '>', $db_orig;
38print DBFILE 'THIS IS NOT A REAL DATABASE';
39close DBFILE;
40chmod 0000, $db_orig;
41
42### Try the operation again... it should fail, since there's no db
43eval {
44 my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
45};
46ok( $@, 'The operation failed' );
47
48### Now, move the db file back to the correct name
49unlink($db_orig);
50move( $db_tmp, $db_orig );
51
52### Try the operation again... this time, it should succeed
53my @art_four;
54eval {
55 @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
56};
57ok( !$@, 'The operation succedded' );
58cmp_ok( @art_four, '==', 3, "Three artists returned" );
59