[Bast-commits] r6413 - DBIx-Class/0.08/trunk/t

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Mon May 25 09:11:01 GMT 2009


Author: ribasushi
Date: 2009-05-25 09:11:01 +0000 (Mon, 25 May 2009)
New Revision: 6413

Modified:
   DBIx-Class/0.08/trunk/t/42toplimit.t
Log:
Define how Top limit emulation should behave

Modified: DBIx-Class/0.08/trunk/t/42toplimit.t
===================================================================
--- DBIx-Class/0.08/trunk/t/42toplimit.t	2009-05-25 07:47:01 UTC (rev 6412)
+++ DBIx-Class/0.08/trunk/t/42toplimit.t	2009-05-25 09:11:01 UTC (rev 6413)
@@ -2,58 +2,118 @@
 use warnings;
 
 use Test::More;
-use DBIx::Class::Storage::DBI;
 use lib qw(t/lib);
-use DBICTest; # do not remove even though it is not used
+use DBICTest;
 use DBIC::SqlMakerTest;
 
-plan tests => 10;
+my $schema = DBICTest->init_schema;
 
-my $sa = new DBIx::Class::SQLAHacks;
-$sa->limit_dialect( 'Top' );
+# Trick the sqlite DB to use Top limit emulation
+delete $schema->storage->_sql_maker->{_cached_syntax};
+$schema->storage->_sql_maker->limit_dialect ('Top');
 
+my $rs = $schema->resultset ('FourKeys')->search ({}, { rows => 1, offset => 3 });
+
 sub test_order {
   my $args = shift;
-  my $order_by = $args->{order_by};
-  my $expected_sql_order = $args->{expected_sql_order};
 
-  my $query = $sa->select( 'foo', [qw{bar baz}], undef, {
-      order_by => $order_by,
-     }, 1, 3
+  my $req_order = $args->{order_req}
+    ? "ORDER BY $args->{order_req}"
+    : ''
+  ;
+
+  is_same_sql_bind(
+    $rs->search ({}, {order_by => $args->{order_by}})->as_query,
+    "(
+      SELECT * FROM (
+        SELECT TOP 1 * FROM (
+          SELECT TOP 4 me.foo, me.bar, me.hello, me.goodbye, me.sensors, me.read_count FROM fourkeys me ORDER BY $args->{order_inner}
+        ) foo ORDER BY $args->{order_outer}
+      ) bar
+      $req_order
+    )",
+    [],
   );
-  is_same_sql(
-    $query,
-    "SELECT * FROM ( SELECT TOP 1 * FROM ( SELECT TOP 4 bar,baz FROM foo ORDER BY $expected_sql_order->[0] ) AS foo ORDER BY $expected_sql_order->[1] ) AS bar ORDER BY $expected_sql_order->[0]",
-  );
 }
 
-  test_order({ order_by => \'foo DESC'       , expected_sql_order => [ 'foo DESC', 'foo ASC' ] });
-  test_order({ order_by => 'foo'             , expected_sql_order => [ 'foo ASC', 'foo DESC'] });
-  test_order({ order_by => [ qw{ foo bar}   ], expected_sql_order => [ 'foo ASC,bar ASC', 'foo DESC, bar DESC']});
-  test_order({ order_by => { -asc => 'foo'  }, expected_sql_order => [ 'foo ASC', 'foo DESC' ] });
-  test_order({ order_by => { -desc => 'foo' }, expected_sql_order => [ 'foo DESC', 'foo ASC' ] });
+my @tests = (
+  {
+    order_by => \ 'foo DESC',
+    order_req => 'foo DESC',
+    order_inner => 'foo DESC',
+    order_outer => 'foo ASC' 
+  },
+  {
+    order_by => { -asc => 'foo'  },
+    order_req => 'foo ASC',
+    order_inner => 'foo ASC',
+    order_outer => 'foo DESC',
+  },
+  {
+    order_by => 'foo',
+    order_req => 'foo',
+    order_inner => 'foo ASC',
+    order_outer => 'foo DESC',
+  },
+  {
+    order_by => [ qw{ foo bar}   ],
+    order_req => 'foo, bar',
+    order_inner => 'foo ASC,bar ASC',
+    order_outer => 'foo DESC, bar DESC',
+  },
+  {
+    order_by => { -desc => 'foo' },
+    order_req => 'foo DESC',
+    order_inner => 'foo DESC',
+    order_outer => 'foo ASC',
+  },
+  {
+    order_by => ['foo', { -desc => 'bar' } ],
+    order_req => 'foo, bar DESC',
+    order_inner => 'foo ASC, bar DESC',
+    order_outer => 'foo DESC, bar ASC',
+  },
+  {
+    order_by => { -asc => [qw{ foo bar }] },
+    order_req => 'foo ASC, bar ASC',
+    order_inner => 'foo ASC, bar ASC',
+    order_outer => 'foo DESC, bar DESC',
+  },
+  {
+    order_by => [
+      { -asc => 'foo' },
+      { -desc => [qw{bar}] },
+      { -asc  => [qw{hello sensors}]},
+    ],
+    order_req => 'foo ASC, bar DESC, hello ASC, sensors ASC',
+    order_inner => 'foo ASC, bar DESC, hello ASC, sensors ASC',
+    order_outer => 'foo DESC, bar ASC, hello DESC, sensors DESC',
+  },
+  {
+    order_by => undef,
+    order_req => undef,
+    order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
+    order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
+  },
+  {
+    order_by => '',
+    order_req => undef,
+    order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
+    order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
+  },
+  {
+    order_by => {},
+    order_req => undef,
+    order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
+    order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
+  },
+  {
+    order_by => [],
+    order_req => undef,
+    order_inner => 'foo ASC, bar ASC, hello ASC, goodbye ASC',
+    order_outer => 'foo DESC, bar DESC, hello DESC, goodbye DESC',
+  },
+);
 
-  test_order({ order_by => ['foo', { -desc => 'bar' } ], expected_sql_order => [ 'foo ASC, bar DESC', 'foo DESC, bar ASC'] });
-  test_order({ order_by => {-asc => [qw{ foo bar }] }, expected_sql_order => ['foo ASC, bar ASC', 'foo DESC, bar DESC' ] });
-  test_order({ order_by =>
-      [
-        { -asc => 'foo' },
-        { -desc => [qw{bar}] },
-        { -asc  => [qw{baz frew}]},
-      ],
-      expected_sql_order => ['foo ASC, bar DESC, baz ASC, frew ASC', 'foo DESC, bar ASC, baz DESC, frew DESC']
-  });
-
-{
-
-  my @w;
-  local $SIG{__WARN__} = sub { $_[0] =~ /Use of uninitialized value/ ? push @w, @_ : warn @_ };
-  my $subquery = $sa->select( [{ subq => \['(SELECT * FROM foo)'] }], [qw{bar baz}], undef, undef, 1, 3);
-  is_same_sql(
-    $subquery,
-    "SELECT * FROM ( SELECT TOP 1 * FROM ( SELECT TOP 4 bar,baz FROM (SELECT * FROM foo) ) AS foo) AS bar",
-  );
-
-  is (@w, 0, 'No warnings on limit with subquery')
-    || diag join ("\n", @w);
-}
+plan (tests => scalar @tests);
+test_order ($_) for @tests;




More information about the Bast-commits mailing list