[Bast-commits] r6463 - in
DBIx-Class/0.08/branches/order_by_refactor/t: . bind
ribasushi at dev.catalyst.perl.org
ribasushi at dev.catalyst.perl.org
Sat May 30 14:55:37 GMT 2009
Author: ribasushi
Date: 2009-05-30 14:55:37 +0000 (Sat, 30 May 2009)
New Revision: 6463
Added:
DBIx-Class/0.08/branches/order_by_refactor/t/bind/
DBIx-Class/0.08/branches/order_by_refactor/t/bind/attribute.t
DBIx-Class/0.08/branches/order_by_refactor/t/bind/bindtype_columns.t
DBIx-Class/0.08/branches/order_by_refactor/t/bind/order_by.t
Removed:
DBIx-Class/0.08/branches/order_by_refactor/t/47bind_attribute.t
DBIx-Class/0.08/branches/order_by_refactor/t/bindtype_columns.t
DBIx-Class/0.08/branches/order_by_refactor/t/order/
Log:
Restructure bind tests
Deleted: DBIx-Class/0.08/branches/order_by_refactor/t/47bind_attribute.t
===================================================================
--- DBIx-Class/0.08/branches/order_by_refactor/t/47bind_attribute.t 2009-05-30 14:54:37 UTC (rev 6462)
+++ DBIx-Class/0.08/branches/order_by_refactor/t/47bind_attribute.t 2009-05-30 14:55:37 UTC (rev 6463)
@@ -1,96 +0,0 @@
-use strict;
-use warnings;
-
-use Test::More;
-use lib qw(t/lib);
-use DBIC::SqlMakerTest;
-
-use_ok('DBICTest');
-
-my $schema = DBICTest->init_schema;
-
-BEGIN {
- eval "use DBD::SQLite";
- plan $@
- ? ( skip_all => 'needs DBD::SQLite for testing' )
- : ( tests => 9 );
-}
-
-my $where_bind = {
- where => \'name like ?',
- bind => [ 'Cat%' ],
-};
-
-my $rs;
-
-TODO: {
- local $TODO = 'bind args order needs fixing (semifor)';
-
- # First, the simple cases...
- $rs = $schema->resultset('Artist')->search(
- { artistid => 1 },
- $where_bind,
- );
-
- is ( $rs->count, 1, 'where/bind combined' );
-
- $rs= $schema->resultset('Artist')->search({}, $where_bind)
- ->search({ artistid => 1});
-
- is ( $rs->count, 1, 'where/bind first' );
-
- $rs = $schema->resultset('Artist')->search({ artistid => 1})
- ->search({}, $where_bind);
-
- is ( $rs->count, 1, 'where/bind last' );
-}
-
-# More complex cases, based primarily on the Cookbook
-# "Arbitrary SQL through a custom ResultSource" technique,
-# which seems to be the only place the bind attribute is
-# documented. Breaking this technique probably breaks existing
-# application code.
-my $source = DBICTest::Artist->result_source_instance;
-my $new_source = $source->new($source);
-$new_source->source_name('Complex');
-
-$new_source->name(\<<'');
-( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
- FROM artist a
- JOIN cd ON cd.artist = a.artistid
- WHERE cd.year = ?)
-
-$schema->register_extra_source('Complex' => $new_source);
-
-$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
-is ( $rs->count, 1, 'cookbook arbitrary sql example' );
-
-$rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
-is ( $rs->count, 1, '...coobook + search condition' );
-
-$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
- ->search({ 'artistid' => 1 });
-is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
-
-{
- $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
- is_same_sql_bind(
- $rs->as_query,
- "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) WHERE title LIKE ?)",
- [
- [ '!!dummy' => '1999' ],
- [ '!!dummy' => 'Spoon%' ]
- ],
- 'got correct SQL'
-);
-
-}
-
-TODO: {
- local $TODO = 'bind args order needs fixing (semifor)';
- $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
- ->search({ 'artistid' => 1 }, {
- where => \'title like ?',
- bind => [ 'Spoon%' ] });
- is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
-}
Copied: DBIx-Class/0.08/branches/order_by_refactor/t/bind/attribute.t (from rev 6459, DBIx-Class/0.08/branches/order_by_refactor/t/47bind_attribute.t)
===================================================================
--- DBIx-Class/0.08/branches/order_by_refactor/t/bind/attribute.t (rev 0)
+++ DBIx-Class/0.08/branches/order_by_refactor/t/bind/attribute.t 2009-05-30 14:55:37 UTC (rev 6463)
@@ -0,0 +1,96 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBIC::SqlMakerTest;
+
+use_ok('DBICTest');
+
+my $schema = DBICTest->init_schema;
+
+BEGIN {
+ eval "use DBD::SQLite";
+ plan $@
+ ? ( skip_all => 'needs DBD::SQLite for testing' )
+ : ( tests => 9 );
+}
+
+my $where_bind = {
+ where => \'name like ?',
+ bind => [ 'Cat%' ],
+};
+
+my $rs;
+
+TODO: {
+ local $TODO = 'bind args order needs fixing (semifor)';
+
+ # First, the simple cases...
+ $rs = $schema->resultset('Artist')->search(
+ { artistid => 1 },
+ $where_bind,
+ );
+
+ is ( $rs->count, 1, 'where/bind combined' );
+
+ $rs= $schema->resultset('Artist')->search({}, $where_bind)
+ ->search({ artistid => 1});
+
+ is ( $rs->count, 1, 'where/bind first' );
+
+ $rs = $schema->resultset('Artist')->search({ artistid => 1})
+ ->search({}, $where_bind);
+
+ is ( $rs->count, 1, 'where/bind last' );
+}
+
+# More complex cases, based primarily on the Cookbook
+# "Arbitrary SQL through a custom ResultSource" technique,
+# which seems to be the only place the bind attribute is
+# documented. Breaking this technique probably breaks existing
+# application code.
+my $source = DBICTest::Artist->result_source_instance;
+my $new_source = $source->new($source);
+$new_source->source_name('Complex');
+
+$new_source->name(\<<'');
+( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
+ FROM artist a
+ JOIN cd ON cd.artist = a.artistid
+ WHERE cd.year = ?)
+
+$schema->register_extra_source('Complex' => $new_source);
+
+$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
+is ( $rs->count, 1, 'cookbook arbitrary sql example' );
+
+$rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
+is ( $rs->count, 1, '...coobook + search condition' );
+
+$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
+ ->search({ 'artistid' => 1 });
+is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
+
+{
+ $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
+ is_same_sql_bind(
+ $rs->as_query,
+ "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) WHERE title LIKE ?)",
+ [
+ [ '!!dummy' => '1999' ],
+ [ '!!dummy' => 'Spoon%' ]
+ ],
+ 'got correct SQL'
+);
+
+}
+
+TODO: {
+ local $TODO = 'bind args order needs fixing (semifor)';
+ $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
+ ->search({ 'artistid' => 1 }, {
+ where => \'title like ?',
+ bind => [ 'Spoon%' ] });
+ is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
+}
Copied: DBIx-Class/0.08/branches/order_by_refactor/t/bind/bindtype_columns.t (from rev 6459, DBIx-Class/0.08/branches/order_by_refactor/t/bindtype_columns.t)
===================================================================
--- DBIx-Class/0.08/branches/order_by_refactor/t/bind/bindtype_columns.t (rev 0)
+++ DBIx-Class/0.08/branches/order_by_refactor/t/bind/bindtype_columns.t 2009-05-30 14:55:37 UTC (rev 6463)
@@ -0,0 +1,88 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
+
+plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
+ unless ($dsn && $dbuser);
+
+plan tests => 6;
+
+my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
+
+my $dbh = $schema->storage->dbh;
+
+{
+ local $SIG{__WARN__} = sub {};
+ $dbh->do('DROP TABLE IF EXISTS bindtype_test');
+
+ # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
+ $dbh->do(qq[
+ CREATE TABLE bindtype_test
+ (
+ id serial NOT NULL PRIMARY KEY,
+ bytea bytea NULL,
+ blob bytea NULL,
+ clob text NULL
+ );
+ ],{ RaiseError => 1, PrintError => 1 });
+}
+
+my $big_long_string = "\x00\x01\x02 abcd" x 125000;
+
+my $new;
+# test inserting a row
+{
+ $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
+
+ ok($new->id, "Created a bytea row");
+ is($new->bytea, $big_long_string, "Set the blob correctly.");
+}
+
+# test retrieval of the bytea column
+{
+ my $row = $schema->resultset('BindType')->find({ id => $new->id });
+ is($row->get_column('bytea'), $big_long_string, "Created the blob correctly.");
+}
+
+TODO: {
+ local $TODO =
+ 'Passing bind attributes to $sth->bind_param() should be implemented (it only works in $storage->insert ATM)';
+
+ my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
+
+ # search on the bytea column (select)
+ {
+ my $row = $rs->first;
+ is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
+ }
+
+ # search on the bytea column (update)
+ {
+ my $new_big_long_string = $big_long_string . "2";
+ $schema->txn_do(sub {
+ $rs->update({ bytea => $new_big_long_string });
+ my $row = $schema->resultset('BindType')->find({ id => $new->id });
+ is($row ? $row->get_column('bytea') : undef, $new_big_long_string,
+ "Updated the row correctly (searching on the bytea column)."
+ );
+ $schema->txn_rollback;
+ });
+ }
+
+ # search on the bytea column (delete)
+ {
+ $schema->txn_do(sub {
+ $rs->delete;
+ my $row = $schema->resultset('BindType')->find({ id => $new->id });
+ is($row, undef, "Deleted the row correctly (searching on the bytea column).");
+ $schema->txn_rollback;
+ });
+ }
+}
+
+$dbh->do("DROP TABLE bindtype_test");
Copied: DBIx-Class/0.08/branches/order_by_refactor/t/bind/order_by.t (from rev 6462, DBIx-Class/0.08/branches/order_by_refactor/t/order/with_bind.t)
===================================================================
--- DBIx-Class/0.08/branches/order_by_refactor/t/bind/order_by.t (rev 0)
+++ DBIx-Class/0.08/branches/order_by_refactor/t/bind/order_by.t 2009-05-30 14:55:37 UTC (rev 6463)
@@ -0,0 +1,106 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::SqlMakerTest;
+
+my $schema = DBICTest->init_schema;
+
+my $rs = $schema->resultset('FourKeys');
+
+sub test_order {
+
+ TODO: {
+ my $args = shift;
+
+ local $TODO = "Not implemented" if $args->{todo};
+
+ lives_ok {
+ is_same_sql_bind(
+ $rs->search(
+ { foo => 'bar' },
+ {
+ order_by => $args->{order_by},
+ having =>
+ [ { read_count => { '>' => 5 } }, \[ 'read_count < ?', 8 ] ]
+ }
+ )->as_query,
+ "(
+ SELECT me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count
+ FROM fourkeys me
+ WHERE ( foo = ? )
+ HAVING read_count > ? OR read_count < ?
+ ORDER BY $args->{order_req}
+ )",
+ [
+ [qw(foo bar)],
+ [qw(read_count 5)],
+ 8,
+ $args->{bind}
+ ? @{ $args->{bind} }
+ : ()
+ ],
+ );
+ };
+ fail('Fail the unfinished is_same_sql_bind') if $@;
+ }
+}
+
+my @tests = (
+ {
+ order_by => \'foo DESC',
+ order_req => 'foo DESC',
+ bind => [],
+ },
+ {
+ order_by => { -asc => 'foo' },
+ order_req => 'foo ASC',
+ bind => [],
+ },
+ {
+ order_by => { -desc => \[ 'colA LIKE ?', 'test' ] },
+ order_req => 'colA LIKE ? DESC',
+ bind => [qw(test)],
+ },
+ {
+ order_by => \[ 'colA LIKE ? DESC', 'test' ],
+ order_req => 'colA LIKE ? DESC',
+ bind => [qw(test)],
+ },
+ {
+ order_by => [
+ { -asc => \['colA'] },
+ { -desc => \[ 'colB LIKE ?', 'test' ] },
+ { -asc => \[ 'colC LIKE ?', 'tost' ] }
+ ],
+ order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
+ bind => [qw(test tost)],
+ },
+
+ # (mo) this would be really really nice!
+ # (ribasushi) I don't think so, not writing it - patches welcome
+ {
+ order_by => [
+ { -asc => 'colA' },
+ { -desc => { colB => { 'LIKE' => 'test' } } },
+ { -asc => { colC => { 'LIKE' => 'tost' } } }
+ ],
+ order_req => 'colA ASC, colB LIKE ? DESC, colC LIKE ? ASC',
+ bind => [ [ colB => 'test' ], [ colC => 'tost' ] ], # ???
+ todo => 1,
+ },
+ {
+ order_by => { -desc => { colA => { LIKE => 'test' } } },
+ order_req => 'colA LIKE ? DESC',
+ bind => [qw(test)],
+ todo => 1,
+ },
+);
+
+plan( tests => scalar @tests * 2 );
+
+test_order($_) for @tests;
+
Deleted: DBIx-Class/0.08/branches/order_by_refactor/t/bindtype_columns.t
===================================================================
--- DBIx-Class/0.08/branches/order_by_refactor/t/bindtype_columns.t 2009-05-30 14:54:37 UTC (rev 6462)
+++ DBIx-Class/0.08/branches/order_by_refactor/t/bindtype_columns.t 2009-05-30 14:55:37 UTC (rev 6463)
@@ -1,88 +0,0 @@
-use strict;
-use warnings;
-
-use Test::More;
-use lib qw(t/lib);
-use DBICTest;
-
-my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
-
-plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
- unless ($dsn && $dbuser);
-
-plan tests => 6;
-
-my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
-
-my $dbh = $schema->storage->dbh;
-
-{
- local $SIG{__WARN__} = sub {};
- $dbh->do('DROP TABLE IF EXISTS bindtype_test');
-
- # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
- $dbh->do(qq[
- CREATE TABLE bindtype_test
- (
- id serial NOT NULL PRIMARY KEY,
- bytea bytea NULL,
- blob bytea NULL,
- clob text NULL
- );
- ],{ RaiseError => 1, PrintError => 1 });
-}
-
-my $big_long_string = "\x00\x01\x02 abcd" x 125000;
-
-my $new;
-# test inserting a row
-{
- $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
-
- ok($new->id, "Created a bytea row");
- is($new->bytea, $big_long_string, "Set the blob correctly.");
-}
-
-# test retrieval of the bytea column
-{
- my $row = $schema->resultset('BindType')->find({ id => $new->id });
- is($row->get_column('bytea'), $big_long_string, "Created the blob correctly.");
-}
-
-TODO: {
- local $TODO =
- 'Passing bind attributes to $sth->bind_param() should be implemented (it only works in $storage->insert ATM)';
-
- my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
-
- # search on the bytea column (select)
- {
- my $row = $rs->first;
- is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
- }
-
- # search on the bytea column (update)
- {
- my $new_big_long_string = $big_long_string . "2";
- $schema->txn_do(sub {
- $rs->update({ bytea => $new_big_long_string });
- my $row = $schema->resultset('BindType')->find({ id => $new->id });
- is($row ? $row->get_column('bytea') : undef, $new_big_long_string,
- "Updated the row correctly (searching on the bytea column)."
- );
- $schema->txn_rollback;
- });
- }
-
- # search on the bytea column (delete)
- {
- $schema->txn_do(sub {
- $rs->delete;
- my $row = $schema->resultset('BindType')->find({ id => $new->id });
- is($row, undef, "Deleted the row correctly (searching on the bytea column).");
- $schema->txn_rollback;
- });
- }
-}
-
-$dbh->do("DROP TABLE bindtype_test");
More information about the Bast-commits
mailing list