From: Daniel Westermann-Clark Date: Sat, 16 Sep 2006 16:56:02 +0000 (+0000) Subject: Rework quote tests to not rely on SQLite, instead using debugcb to get generated SQL X-Git-Tag: v0.07003~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5a1f2d730c2ce5d0b7e8b996ffaa529c4872b410;p=dbsrgits%2FDBIx-Class.git Rework quote tests to not rely on SQLite, instead using debugcb to get generated SQL --- diff --git a/t/19quotes.t b/t/19quotes.t index 2ce7a26..303f297 100644 --- a/t/19quotes.t +++ b/t/19quotes.t @@ -16,34 +16,37 @@ use lib qw(t/lib); use_ok('DBICTest'); DBICTest->init_schema(); +my $orig_debugcb = DBICTest->schema->storage->debugcb; +my $orig_debug = DBICTest->schema->storage->debug; + +diag('Testing against ' . join(' ', map { DBICTest->schema->storage->dbh->get_info($_) } qw/17 18/)); + DBICTest->schema->storage->sql_maker->quote_char('`'); DBICTest->schema->storage->sql_maker->name_sep('.'); -my $rs = DBICTest::CD->search( +my $sql = ''; + +DBICTest->schema->storage->debugcb(sub { $sql = $_[1] }); +DBICTest->schema->storage->debug(1); + +my $rs; + +$rs = DBICTest::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'); -cmp_ok( $rs->count, '==', 1, "join with fields quoted"); - +my $order = 'year DESC'; $rs = DBICTest::CD->search({}, - { 'order_by' => 'year DESC'}); -{ - my $warnings = ''; - local $SIG{__WARN__} = sub { $warnings .= $_[0] }; - my $first = eval{ $rs->first() }; - like( $warnings, qr/no such column: year DESC/, "Problem with ORDER BY quotes" ); -} + { 'order_by' => $order }); +eval { $rs->first }; +like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)'); -my $order = 'year DESC'; $rs = DBICTest::CD->search({}, { 'order_by' => \$order }); -{ - my $warnings = ''; - local $SIG{__WARN__} = sub { $warnings .= $_[0] }; - my $first = $rs->first(); - ok( $warnings !~ /ORDER BY terms/, - "No problem handling ORDER by scalaref" ); -} +eval { $rs->first }; +like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref'); DBICTest->schema->storage->sql_maker->quote_char([qw/[ ]/]); DBICTest->schema->storage->sql_maker->name_sep('.'); @@ -51,7 +54,8 @@ DBICTest->schema->storage->sql_maker->name_sep('.'); $rs = DBICTest::CD->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); -cmp_ok($rs->count,'==', 1,"join quoted with brackets."); +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'); my %data = ( name => 'Bill', @@ -61,5 +65,7 @@ my %data = ( DBICTest->schema->storage->sql_maker->quote_char('`'); DBICTest->schema->storage->sql_maker->name_sep('.'); -cmp_ok(DBICTest->schema->storage->sql_maker->update('group', \%data), 'eq', 'UPDATE `group` SET `name` = ?, `order` = ?', "quoted table names for UPDATE"); +is(DBICTest->schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE'); +DBICTest->schema->storage->debugcb($orig_debugcb); +DBICTest->schema->storage->debug($orig_debug); diff --git a/t/19quotes_newstyle.t b/t/19quotes_newstyle.t index de32393..31feaa3 100644 --- a/t/19quotes_newstyle.t +++ b/t/19quotes_newstyle.t @@ -16,42 +16,46 @@ use lib qw(t/lib); use_ok('DBICTest'); DBICTest->init_schema(); -my $dsn = DBICTest->schema->storage->connect_info->[0]; +my $orig_debugcb = DBICTest->schema->storage->debugcb; +my $orig_debug = DBICTest->schema->storage->debug; + +diag('Testing against ' . join(' ', map { DBICTest->schema->storage->dbh->get_info($_) } qw/17 18/)); +my $dsn = DBICTest->schema->storage->connect_info->[0]; DBICTest->schema->connection($dsn, { quote_char => '`', name_sep => '.' }); -my $rs = DBICTest::CD->search( +my $sql = ''; +DBICTest->schema->storage->debugcb(sub { $sql = $_[1] }); +DBICTest->schema->storage->debug(1); + +my $rs; + +$rs = DBICTest::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'); -cmp_ok( $rs->count, '==', 1, "join with fields quoted"); - +my $order = 'year DESC'; $rs = DBICTest::CD->search({}, - { 'order_by' => 'year DESC'}); -{ - my $warnings = ''; - local $SIG{__WARN__} = sub { $warnings .= $_[0] }; - my $first = eval{ $rs->first() }; - like( $warnings, qr/no such column: year DESC/, "Problem with ORDER BY quotes" ); -} + { 'order_by' => $order }); +eval { $rs->first }; +like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)'); -my $order = 'year DESC'; $rs = DBICTest::CD->search({}, { 'order_by' => \$order }); -{ - my $warnings = ''; - local $SIG{__WARN__} = sub { $warnings .= $_[0] }; - my $first = $rs->first(); - ok( $warnings !~ /ORDER BY terms/, - "No problem handling ORDER by scalaref" ); -} +eval { $rs->first }; +like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref'); DBICTest->schema->connection($dsn, { quote_char => [qw/[ ]/], name_sep => '.' }); +DBICTest->schema->storage->debugcb(sub { $sql = $_[1] }); +DBICTest->schema->storage->debug(1); $rs = DBICTest::CD->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); -cmp_ok($rs->count,'==', 1,"join quoted with brackets."); +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'); my %data = ( name => 'Bill', @@ -60,5 +64,7 @@ my %data = ( DBICTest->schema->connection($dsn, { quote_char => '`', name_sep => '.' }); -cmp_ok(DBICTest->schema->storage->sql_maker->update('group', \%data), 'eq', 'UPDATE `group` SET `name` = ?, `order` = ?', "quoted table names for UPDATE"); +is(DBICTest->schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE'); +DBICTest->schema->storage->debugcb($orig_debugcb); +DBICTest->schema->storage->debug($orig_debug);