From: Sebastian Willert Date: Sat, 6 Apr 2013 14:43:44 +0000 (+0200) Subject: Testcase to demonstrate that DBI is not always loaded before being used X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fghpr%2Fclosed%2Finspiring_524343a9;p=dbsrgits%2FDBIx-Class-Historic.git Testcase to demonstrate that DBI is not always loaded before being used A simple 'use DBI;' in DBIx::Class::Storage::DBI will fix this. The fix is not included. --- diff --git a/t/storage/bare_bones.db b/t/storage/bare_bones.db new file mode 100644 index 0000000..f23f121 Binary files /dev/null and b/t/storage/bare_bones.db differ diff --git a/t/storage/bare_bones.t b/t/storage/bare_bones.t new file mode 100644 index 0000000..2b2ac44 --- /dev/null +++ b/t/storage/bare_bones.t @@ -0,0 +1,70 @@ +use strict; +use warnings; + +use Test::More; +use Test::Warn; +use Test::Exception; + +use FindBin qw/$Bin/; +use File::Spec; +use File::Copy qw/copy/; + +use lib qw(t/lib); +use DBICTest; + +my $db_file = File::Spec->catfile( $Bin, 'bare_bones.db' ); + +my $test_db = DBICTest::_sqlite_dbfilename(); +copy( $db_file, $test_db ); + +{ + package MySchema::Result::Item; + use base 'DBIx::Class::Core'; + __PACKAGE__->table('item'); + __PACKAGE__->add_column( id => { data_type => 'INTEGER' }); +} + +{ + package MySchema; + use base 'DBIx::Class::Schema'; + __PACKAGE__->register_class( Item => 'MySchema::Result::Item' ); +} + +# Calling $schema->storage->dbh loads DBI and masks this bug, +# so we can't use in-memory databases to demonstrate it +# my $schema = MySchema->connect( "dbi:SQLite::memory:"); +# $schema->storage->dbh->do('CREATE TABLE item( id INTEGER )'); + +my $schema = MySchema->connect( "dbi:SQLite:${test_db}"); +is_deeply( + [ $schema->sources ], [ 'Item' ], + 'Creating a schema with a single source' +); + +# Calling deploy (which loads DBI) masks the bug I am trying to isolate +# lives_ok { $schema->deploy } 'Deploying minimal schema'; + +my $item; +lives_ok { + $item = $schema->resultset('Item')->create({ id => 1 }); +} 'Creating a row in a pre-existing database' + or diag "A simple 'use DBI;' in DBIx::Class::Storage::DBI will fix this"; + +isa_ok( $item, 'DBIx::Class::Row' ); + +# clean up behind the test db to recreate it +unlink $test_db; + +$schema = MySchema->connect( "dbi:SQLite:${test_db}"); + +lives_ok { + $schema->deploy +} 'Deploying a copy of the schema to an in-memory DB'; + +lives_ok { + $item = $schema->resultset('Item')->create({ id => 1 }); +} 'deploy has loaded DBI so everything is fine now'; + +isa_ok( $item, 'DBIx::Class::Row' ); + +done_testing;