From: Norbert Buchmuller Date: Tue, 11 Nov 2008 05:59:32 +0000 (+0100) Subject: * Converted some of the test cases to use SQL::Abstract::Test. X-Git-Tag: v0.08240~189^2~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9b4591296c196ea0a32e67159e35091b02ecc539;p=dbsrgits%2FDBIx-Class.git * Converted some of the test cases to use SQL::Abstract::Test. * Added the parens for the join condition (SQL::Abstract::Test can't cope with it, and, besides, they help readability there; plus this gives the same output that older SQLA generated). --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index ea81980..b31ac27 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -237,7 +237,7 @@ sub _recurse_from { } else { push(@sqlf, $self->_make_as($to)); } - push(@sqlf, ' ON ', $self->_join_condition($on)); + push(@sqlf, ' ON (', $self->_join_condition($on), ')'); } return join('', @sqlf); } diff --git a/t/19quotes.t b/t/19quotes.t index 646131b..2412e1d 100644 --- a/t/19quotes.t +++ b/t/19quotes.t @@ -3,30 +3,28 @@ use warnings; use Test::More; use IO::File; +use SQL::Abstract::Test import => ['is_same_sql_bind']; BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 6 ); + : ( tests => 7 ); } use lib qw(t/lib); use_ok('DBICTest'); +use_ok('DBICTest::DBICDebugObj'); my $schema = DBICTest->init_schema(); -my $orig_debugcb = $schema->storage->debugcb; -my $orig_debug = $schema->storage->debug; - diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/)); $schema->storage->sql_maker->quote_char('`'); $schema->storage->sql_maker->name_sep('.'); -my $sql = ''; - -$schema->storage->debugcb(sub { $sql = $_[1] }); +my ($sql, @bind) = (''); +$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind)); $schema->storage->debug(1); my $rs; @@ -35,7 +33,11 @@ $rs = $schema->resultset('CD')->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); eval { $rs->count }; -like($sql, qr/\QSELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )\E/, 'got correct SQL for count query with quoting'); +is_same_sql_bind( + $sql, \@bind, + "SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"], + 'got correct SQL for count query with quoting' +); my $order = 'year DESC'; $rs = $schema->resultset('CD')->search({}, @@ -55,7 +57,11 @@ $rs = $schema->resultset('CD')->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); eval { $rs->count }; -like($sql, qr/\QSELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )\E/, 'got correct SQL for count query with bracket quoting'); +is_same_sql_bind( + $sql, \@bind, + "SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"], + 'got correct SQL for count query with bracket quoting' +); my %data = ( name => 'Bill', @@ -66,6 +72,3 @@ $schema->storage->sql_maker->quote_char('`'); $schema->storage->sql_maker->name_sep('.'); is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE'); - -$schema->storage->debugcb($orig_debugcb); -$schema->storage->debug($orig_debug); diff --git a/t/19quotes_newstyle.t b/t/19quotes_newstyle.t index 98ef777..124bd65 100644 --- a/t/19quotes_newstyle.t +++ b/t/19quotes_newstyle.t @@ -3,21 +3,21 @@ use warnings; use Test::More; use IO::File; +use SQL::Abstract::Test import => ['is_same_sql_bind']; BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 6 ); + : ( tests => 7 ); } use lib qw(t/lib); use_ok('DBICTest'); -my $schema = DBICTest->init_schema(); +use_ok('DBICTest::DBICDebugObj'); -my $orig_debugcb = $schema->storage->debugcb; -my $orig_debug = $schema->storage->debug; +my $schema = DBICTest->init_schema(); diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/)); @@ -30,8 +30,8 @@ $schema->connection( { quote_char => '`', name_sep => '.' }, ); -my $sql = ''; -$schema->storage->debugcb(sub { $sql = $_[1] }); +my ($sql, @bind) = (''); +$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind)), $schema->storage->debug(1); my $rs; @@ -40,7 +40,11 @@ $rs = $schema->resultset('CD')->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); eval { $rs->count }; -like($sql, qr/\QSELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )\E/, 'got correct SQL for count query with quoting'); +is_same_sql_bind( + $sql, \@bind, + "SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"], + 'got correct SQL for count query with quoting' +); my $order = 'year DESC'; $rs = $schema->resultset('CD')->search({}, @@ -59,14 +63,19 @@ $schema->connection( undef, { AutoCommit => 1, quote_char => [qw/[ ]/], name_sep => '.' } ); -$schema->storage->debugcb(sub { $sql = $_[1] }); + +$schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind)), $schema->storage->debug(1); $rs = $schema->resultset('CD')->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); eval { $rs->count }; -like($sql, qr/\QSELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )\E/, 'got correct SQL for count query with bracket quoting'); +is_same_sql_bind( + $sql, \@bind, + "SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"], + 'got correct SQL for count query with bracket quoting' +); my %data = ( name => 'Bill', @@ -81,6 +90,3 @@ $schema->connection( ); is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE'); - -$schema->storage->debugcb($orig_debugcb); -$schema->storage->debug($orig_debug); diff --git a/t/41orrible.t b/t/41orrible.t index c9748b3..54633ce 100644 --- a/t/41orrible.t +++ b/t/41orrible.t @@ -3,6 +3,7 @@ use warnings; use Test::More; #use DBIx::Class::Storage::DBI; +use SQL::Abstract::Test import => ['is_same_sql_bind']; use DBIx::Class::Storage::DBI::Oracle::WhereJoins; plan tests => 4; @@ -29,7 +30,8 @@ WHERE r >= 4 # search with undefined or empty $cond # my ($self, $table, $fields, $where, $order, @rest) = @_; -is($sa->select([ +my ($sql, @bind) = $sa->select( + [ { me => "cd" }, [ { "-join_type" => "LEFT", artist => "artist" }, @@ -38,10 +40,16 @@ is($sa->select([ ], [ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ], undef, - undef), - 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( artist.artistid(+) = me.artist )', 'WhereJoins search with empty where clause'); + undef +); +is_same_sql_bind( + $sql, \@bind, + 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( artist.artistid(+) = me.artist )', [], + 'WhereJoins search with empty where clause' +); -is($sa->select([ +($sql, @bind) = $sa->select( + [ { me => "cd" }, [ { "-join_type" => "", artist => "artist" }, @@ -50,10 +58,16 @@ is($sa->select([ ], [ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ], { 'artist.artistid' => 3 }, - undef), - 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid = me.artist ) AND ( artist.artistid = ? ) ) )', 'WhereJoins search with where clause'); + undef +); +is_same_sql_bind( + $sql, \@bind, + 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid = me.artist ) AND ( artist.artistid = ? ) ) )', [3], + 'WhereJoins search with where clause' +); -is($sa->select([ +($sql, @bind) = $sa->select( + [ { me => "cd" }, [ { "-join_type" => "LEFT", artist => "artist" }, @@ -62,7 +76,12 @@ is($sa->select([ ], [ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ], [{ 'artist.artistid' => 3 }, { 'me.cdid' => 5 }], - undef), - 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid(+) = me.artist ) AND ( ( ( artist.artistid = ? ) OR ( me.cdid = ? ) ) ) ) )', 'WhereJoins search with or in where clause'); + undef +); +is_same_sql_bind( + $sql, \@bind, + 'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM cd me, artist artist WHERE ( ( ( artist.artistid(+) = me.artist ) AND ( ( ( artist.artistid = ? ) OR ( me.cdid = ? ) ) ) ) )', [3, 5], + 'WhereJoins search with or in where clause' +); diff --git a/t/76joins.t b/t/76joins.t index dba7c00..744bfd7 100644 --- a/t/76joins.t +++ b/t/76joins.t @@ -5,6 +5,7 @@ use Test::More; use lib qw(t/lib); use DBICTest; use Data::Dumper; +use SQL::Abstract::Test import => ['is_same_sql_bind']; my $schema = DBICTest->init_schema(); @@ -43,7 +44,11 @@ my $match = 'person child JOIN person father ON ( father.person_id = ' . 'child.father_id ) JOIN person mother ON ( mother.person_id ' . '= child.mother_id )' ; -is( $sa->_recurse_from(@j), $match, 'join 1 ok' ); +is_same_sql_bind( + $sa->_recurse_from(@j), [], + $match, [], + 'join 1 ok' +); my @j2 = ( { mother => 'person' }, @@ -59,7 +64,12 @@ $match = 'person mother JOIN (person child JOIN person father ON (' . ' father.person_id = child.father_id )) ON ( mother.person_id = ' . 'child.mother_id )' ; -is( $sa->_recurse_from(@j2), $match, 'join 2 ok' ); +is_same_sql_bind( + $sa->_recurse_from(@j2), [], + $match, [], + 'join 2 ok' +); + my @j3 = ( { child => 'person' }, @@ -71,7 +81,11 @@ $match = 'person child INNER JOIN person father ON ( father.person_id = ' . '= child.mother_id )' ; -is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok'); +is_same_sql_bind( + $sa->_recurse_from(@j3), [], + $match, [], + 'join 3 (inner join) ok' +); my @j4 = ( { mother => 'person' }, @@ -87,7 +101,11 @@ $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON (' . ' father.person_id = child.father_id )) ON ( mother.person_id = ' . 'child.mother_id )' ; -is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok'); +is_same_sql_bind( + $sa->_recurse_from(@j4), [], + $match, [], + 'join 4 (nested joins + join types) ok' +); my @j5 = ( { child => 'person' }, @@ -98,7 +116,11 @@ $match = 'person child JOIN person father ON ( father.person_id != ' . 'child.father_id ) JOIN person mother ON ( mother.person_id ' . '= child.mother_id )' ; -is( $sa->_recurse_from(@j5), $match, 'join 5 (SCALAR reference for ON statement) ok' ); +is_same_sql_bind( + $sa->_recurse_from(@j5), [], + $match, [], + 'join 5 (SCALAR reference for ON statement) ok' +); my @j6 = ( { child => 'person' }, diff --git a/t/91debug.t b/t/91debug.t index ae059e4..5314ce4 100644 --- a/t/91debug.t +++ b/t/91debug.t @@ -4,10 +4,12 @@ use warnings; use Test::More; use lib qw(t/lib); use DBICTest; +use DBICTest::DBICDebugObj; +use SQL::Abstract::Test import => ['is_same_sql_bind']; my $schema = DBICTest->init_schema(); -plan tests => 6; +plan tests => 7; ok ( $schema->storage->debug(1), 'debug' ); ok ( defined( @@ -49,14 +51,23 @@ open(STDERR, '>&STDERRCOPY'); # test trace output correctness for bind params { - my $sql = ''; + my ($sql, @bind) = (''); $schema->storage->debugcb( sub { $sql = $_[1] } ); my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } ); - like( - $sql, - qr/\QSELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'\E/, - 'got correct SQL with all bind parameters' + is_same_sql_bind( + $sql, [], + "SELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'", [], + 'got correct SQL with all bind parameters (debugcb)' + ); + + $schema->storage->debugcb(undef); + $schema->storage->debugobj(DBICTest::DBICDebugObj->new(\$sql, \@bind)); + @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } ); + is_same_sql_bind( + $sql, \@bind, + "SELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? )", ["'1'", "'1'", "'3'"], + 'got correct SQL with all bind parameters (debugobj)' ); } diff --git a/t/95sql_maker_quote.t b/t/95sql_maker_quote.t index 1f4cd90..7f8b72c 100644 --- a/t/95sql_maker_quote.t +++ b/t/95sql_maker_quote.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use SQL::Abstract::Test import => ['is_same_sql_bind']; BEGIN { @@ -22,7 +23,7 @@ my $sql_maker = $schema->storage->sql_maker; $sql_maker->quote_char('`'); $sql_maker->name_sep('.'); -my ($sql,) = $sql_maker->select( +my ($sql, @bind) = $sql_maker->select( [ { 'me' => 'cd' @@ -51,11 +52,13 @@ my ($sql,) = $sql_maker->select( undef ); -is($sql, - q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, - 'got correct SQL for count query with quoting'); +is_same_sql_bind( + $sql, \@bind, + q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ], + 'got correct SQL and bind parameters for count query with quoting' +); -($sql,) = $sql_maker->select( +($sql, @bind) = $sql_maker->select( [ { 'me' => 'cd' @@ -86,7 +89,7 @@ TODO: { TODO: { local $TODO = "select attr with star needs fixing (mst/nate)"; - ($sql,) = $sql_maker->select( + ($sql, @bind) = $sql_maker->select( [ { 'me' => 'cd' @@ -101,10 +104,14 @@ TODO: { undef ); - is($sql, q/SELECT `me`.* FROM `cd` `me`/, 'select attr with me.* is right'); + is_same_sql_bind( + $sql, \@bind, + q/SELECT `me`.* FROM `cd` `me`/, [], + 'select attr with me.* is right' + ); } -($sql,) = $sql_maker->select( +($sql, @bind) = $sql_maker->select( [ { 'me' => 'cd' @@ -124,18 +131,13 @@ TODO: { undef ); -is($sql, - q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, - 'did not quote ORDER BY with scalarref'); - -my %data = ( - name => 'Bill', - order => 12 +is_same_sql_bind( + $sql, \@bind, + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [], + 'did not quote ORDER BY with scalarref' ); -my @binds; - -($sql,@binds) = $sql_maker->update( +($sql, @bind) = $sql_maker->update( 'group', { 'order' => '12', @@ -143,13 +145,15 @@ my @binds; } ); -is($sql, - q/UPDATE `group` SET `name` = ?, `order` = ?/, - 'quoted table names for UPDATE'); +is_same_sql_bind( + $sql, \@bind, + q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ], + 'quoted table names for UPDATE' +); $sql_maker->quote_char([qw/[ ]/]); -($sql,) = $sql_maker->select( +($sql, @bind) = $sql_maker->select( [ { 'me' => 'cd' @@ -178,12 +182,14 @@ $sql_maker->quote_char([qw/[ ]/]); undef ); -is($sql, - q/SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, - 'got correct SQL for count query with bracket quoting'); +is_same_sql_bind( + $sql, \@bind, + q/SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ], + 'got correct SQL and bind parameters for count query with bracket quoting' +); -($sql,@binds) = $sql_maker->update( +($sql, @bind) = $sql_maker->update( 'group', { 'order' => '12', @@ -191,6 +197,8 @@ is($sql, } ); -is($sql, - q/UPDATE [group] SET [name] = ?, [order] = ?/, - 'bracket quoted table names for UPDATE'); +is_same_sql_bind( + $sql, \@bind, + q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ], + 'bracket quoted table names for UPDATE' +); diff --git a/t/99dbic_sqlt_parser.t b/t/99dbic_sqlt_parser.t index 42eeb92..1ad7832 100644 --- a/t/99dbic_sqlt_parser.t +++ b/t/99dbic_sqlt_parser.t @@ -8,7 +8,7 @@ use DBICTest; BEGIN { eval "use DBD::mysql; use SQL::Translator 0.09;"; plan $@ - ? ( skip_all => 'needs SQL::Translator 0.09 for testing' ) + ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.09 for testing' ) : ( tests => 102 ); } diff --git a/t/lib/DBICTest/DBICDebugObj.pm b/t/lib/DBICTest/DBICDebugObj.pm new file mode 100644 index 0000000..162021b --- /dev/null +++ b/t/lib/DBICTest/DBICDebugObj.pm @@ -0,0 +1,53 @@ +package DBICTest::DBICDebugObj; + +use strict; +use warnings; + +use Exporter; +use Class::C3; + +use base qw/DBIx::Class::Storage::Statistics/; +use base qw/Exporter/; +use base qw/Class::Accessor::Fast/; + +__PACKAGE__->mk_accessors( qw/dbictest_sql_ref dbictest_bind_ref/ ); + + +=head2 new(PKG, SQL_REF, BIND_REF, ...) + +Creates a new instance that on subsequent queries will store +the generated SQL to the scalar pointed to by SQL_REF and bind +values to the array pointed to by BIND_REF. + +=cut + +sub new { + my $pkg = shift; + my $sql_ref = shift; + my $bind_ref = shift; + + my $self = $pkg->SUPER::new(@_); + + $self->debugfh(undef); + + $self->dbictest_sql_ref($sql_ref); + $self->dbictest_bind_ref($bind_ref); + + return $self; +} + +sub query_start { + my $self = shift; + + (${$self->dbictest_sql_ref}, @{$self->dbictest_bind_ref}) = @_; +} + +sub query_end { } + +sub txn_start { } + +sub txn_commit { } + +sub txn_rollback { } + +1;