Possible to set locale in IC::DateTime extra => {} config
[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
b5bf138f 10plan tests => 6;
e7827df0 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
fcf741b1 16my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
4ffbc1d6 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
b5bf138f 23# Also test if DBD::SQLite finaly knows how to ->disconnect properly
24TODO: {
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!
37my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
38cmp_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;
43move( $db_orig, $db_tmp );
44open DBFILE, '>', $db_orig;
45print DBFILE 'THIS IS NOT A REAL DATABASE';
46close DBFILE;
47chmod 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
60unlink($db_orig);
61move( $db_tmp, $db_orig );
62
413abe68 63SKIP: {
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}