Only load DBICTest::Schema when needed in tests
[dbsrgits/DBIx-Class.git] / t / 746mssql.t
index 611d9e0..598b7a2 100644 (file)
@@ -4,17 +4,13 @@ use warnings;
 use Test::More;
 use Test::Exception;
 use Try::Tiny;
-use DBIx::Class::SQLMaker::LimitDialects;
-use DBIx::Class::Optional::Dependencies ();
-use lib qw(t/lib);
-use DBICTest;
-use DBIC::SqlMakerTest;
 
+use DBIx::Class::Optional::Dependencies ();
 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mssql_odbc')
   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mssql_odbc');
 
-my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype;
-my $TOTAL  = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype;
+use lib qw(t/lib);
+use DBICTest;
 
 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
 
@@ -22,7 +18,7 @@ plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this
   unless ($dsn && $user);
 
 {
-  my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
+  my $srv_ver = DBICTest->connect_schema($dsn, $user, $pass)->storage->_server_info->{dbms_version};
   ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
 }
 
@@ -68,14 +64,14 @@ my %opts = (
 for my $opts_name (keys %opts) {
   SKIP: {
     my $opts = $opts{$opts_name}{opts};
-    $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
+    $schema = DBICTest->connect_schema($dsn, $user, $pass, $opts);
 
     try {
       $schema->storage->ensure_connected
     }
     catch {
       if ($opts{$opts_name}{required}) {
-        BAIL_OUT "on_connect_call option '$opts_name' is not functional: $_";
+        die "on_connect_call option '$opts_name' is not functional: $_";
       }
       else {
         skip
@@ -172,7 +168,7 @@ SQL
 
       lives_ok ( sub {
         # start a new connection, make sure rebless works
-        my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
+        my $schema = DBICTest->connect_schema($dsn, $user, $pass, $opts);
         $schema->populate ('Owners', [
           [qw/id  name  /],
           [qw/1   wiggle/],
@@ -197,7 +193,7 @@ SQL
       lives_ok (sub {
         # start a new connection, make sure rebless works
         # test an insert with a supplied identity, followed by one without
-        my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
+        my $schema = DBICTest->connect_schema($dsn, $user, $pass, $opts);
         for (2, 1) {
           my $id = $_ * 20 ;
           $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
@@ -209,7 +205,7 @@ SQL
 
       lives_ok ( sub {
         # start a new connection, make sure rebless works
-        my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
+        my $schema = DBICTest->connect_schema($dsn, $user, $pass, $opts);
         $schema->populate ('BooksInLibrary', [
           [qw/source  owner title   /],
           [qw/Library 1     secrets0/],
@@ -239,7 +235,7 @@ SQL
     ) {
       for my $quoted (0, 1) {
 
-        $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
+        $schema = DBICTest->connect_schema($dsn, $user, $pass, {
             limit_dialect => $dialect,
             %$opts,
             $quoted
@@ -251,7 +247,7 @@ SQL
         my $test_type = "Dialect:$dialect Quoted:$quoted";
 
         # basic limit support
-        TODO: {
+        {
           my $art_rs = $schema->resultset ('Artist');
           $art_rs->delete;
           $art_rs->create({ name => 'Artist ' . $_ }) for (1..6);
@@ -284,8 +280,8 @@ SQL
           my $sealed_owners = $owners->as_subselect_rs;
 
           is_deeply (
-            [ map { $_->name } ($sealed_owners->all) ],
-            [ map { $_->name } ($owners->all) ],
+            [ sort map { $_->name } ($sealed_owners->all) ],
+            [ sort map { $_->name } ($owners->all) ],
             "$test_type: Sort preserved from within a subquery",
           );
         }
@@ -335,20 +331,13 @@ SQL
           is ($limited_rs->count, 6, "$test_type: Correct count of limited right-sorted joined resultset");
           is ($limited_rs->count_rs->next, 6, "$test_type: Correct count_rs of limited right-sorted joined resultset");
 
-          my $queries;
-          my $orig_debug = $schema->storage->debug;
-          $schema->storage->debugcb(sub { $queries++; });
-          $schema->storage->debug(1);
-
-          is_deeply (
-            [map { $_->owner->name } ($limited_rs->all) ],
-            [@owner_names[2 .. 7]],
-            "$test_type: Prefetch-limited rows were properly ordered"
-          );
-          is ($queries, 1, "$test_type: Only one query with prefetch");
-
-          $schema->storage->debugcb(undef);
-          $schema->storage->debug($orig_debug);
+          $schema->is_executed_querycount( sub {
+            is_deeply (
+              [map { $_->owner->name } ($limited_rs->all) ],
+              [@owner_names[2 .. 7]],
+              "$test_type: Prefetch-limited rows were properly ordered"
+            );
+          }, 1, "$test_type: Only one query with prefetch" );
 
           is_deeply (
             [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
@@ -372,28 +361,11 @@ SQL
           },
         );
 
-        my ($sql, @bind) = @${$owners->page(3)->as_query};
-        is_same_bind (
-          \@bind,
-          [
-            ($dialect eq 'Top' ? [ { dbic_colname => 'test' } => 'xxx' ] : ()), # the extra re-order bind
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
-              => 'somebogusstring' ],
-            [ { dbic_colname => 'test' }
-              => 'xxx' ],
-            ($dialect ne 'Top' ? ( [ $OFFSET => 7 ], [ $TOTAL => 9 ] ) : ()), # parameterised RNO
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
-              => 'somebogusstring' ],
-            [ { dbic_colname => 'test' }
-              => 'xxx' ],
-          ],
-        );
-
         is ($owners->page(1)->all, 3, "$test_type: has_many prefetch returns correct number of rows");
         is ($owners->page(1)->count, 3, "$test_type: has-many prefetch returns correct count");
 
         is ($owners->page(3)->count, 2, "$test_type: has-many prefetch returns correct count");
-        TODO: {
+        {
           local $TODO = "Top-limit does not work when your limit ends up past the resultset"
             if $dialect eq 'Top';
           is ($owners->page(3)->all, 2, "$test_type: has_many prefetch returns correct number of rows");
@@ -416,37 +388,11 @@ SQL
           },
         );
 
-        ($sql, @bind) = @${$books->page(3)->as_query};
-        is_same_bind (
-          \@bind,
-          [
-            # inner
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
-              => 'wiggle' ],
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
-              => 'woggle' ],
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
-              => 'Library' ],
-            [ { dbic_colname => 'test' }
-              => '1' ],
-
-            # rno(?)
-            $dialect ne 'Top' ? ( [ $OFFSET => 5 ], [ $TOTAL => 6 ] ) : (),
-            # outer
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
-              => 'wiggle' ],
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
-              => 'woggle' ],
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
-              => 'Library' ],
-          ],
-        );
-
         is ($books->page(1)->all, 2, "$test_type: Prefetched grouped search returns correct number of rows");
         is ($books->page(1)->count, 2, "$test_type: Prefetched grouped search returns correct count");
 
         is ($books->page(2)->count, 1, "$test_type: Prefetched grouped search returns correct count");
-        TODO: {
+        {
           local $TODO = "Top-limit does not work when your limit ends up past the resultset"
             if $dialect eq 'Top';
           is ($books->page(2)->all, 1, "$test_type: Prefetched grouped search returns correct number of rows");
@@ -474,7 +420,7 @@ SQL
       });
 
       # start disconnected to make sure insert works on an un-reblessed storage
-      $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
+      $schema = DBICTest->connect_schema($dsn, $user, $pass, $opts);
 
       my $row;
       lives_ok {
@@ -516,7 +462,7 @@ CREATE TABLE money_test (
 SQL
       });
 
-      TODO: {
+      {
         my $freetds_and_dynamic_cursors = 1
           if $opts_name eq 'use_dynamic_cursors' &&
             $schema->storage->_using_freetds;