Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 746mssql.t
index b57d218..e3ddd6d 100644 (file)
@@ -1,26 +1,18 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_mssql_odbc';
+
 use strict;
 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;
-
-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');
+use Test::Warn;
 
-my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype;
-my $TOTAL  = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype;
+use DBICTest;
+use DBIx::Class::_Util qw( dbic_internal_try dbic_internal_catch );
 
 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
 
-plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
-  unless ($dsn && $user);
-
 {
   my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
   ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
@@ -70,10 +62,10 @@ for my $opts_name (keys %opts) {
     my $opts = $opts{$opts_name}{opts};
     $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
 
-    try {
+    dbic_internal_try {
       $schema->storage->ensure_connected
     }
-    catch {
+    dbic_internal_catch {
       if ($opts{$opts_name}{required}) {
         die "on_connect_call option '$opts_name' is not functional: $_";
       }
@@ -106,11 +98,35 @@ SQL
 
     ok(($new->artistid||0) > 0, "Auto-PK worked for $opts_name");
 
-# Test multiple active statements
-    SKIP: {
-      skip 'not a multiple active statements configuration', 1
-        if $opts_name eq 'plain';
+# Test graceful error handling if not supporting multiple active statements
+    if( $opts_name eq 'plain' ) {
+
+      # keep the first cursor alive (as long as $rs is alive)
+      my $rs = $schema->resultset("Artist");
+
+      my $a1 = $rs->next;
+
+      my $a2;
+
+      warnings_are {
+        # second cursor, invalidates $rs, but it doesn't
+        # matter as long as we do not try to use it
+        $a2 = $schema->resultset("Artist")->next;
+      } [], 'No warning on retry due to previous cursor invalidation';
 
+      is_deeply(
+        { $a1->get_columns },
+        { $a2->get_columns },
+        'Same data',
+      );
+
+      dies_ok {
+        $rs->next;
+      } 'Invalid cursor did not silently return garbage';
+    }
+
+# Test multiple active statements
+    else {
       $schema->storage->ensure_connected;
 
       lives_ok {
@@ -284,8 +300,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 +351,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,77 +381,6 @@ SQL
           },
         );
 
-        is_same_sql_bind (
-          $owners->page(3)->as_query,
-          $dialect eq 'Top'
-            ? '(
-              SELECT TOP 2147483647 [me].[id], [me].[name],
-                                    [books].[id], [books].[source], [books].[owner], [books].[title], [books].[price]
-                FROM (
-                  SELECT TOP 2147483647 [me].[id], [me].[name]
-                    FROM (
-                      SELECT TOP 3 [me].[id], [me].[name], [ORDER__BY__001]
-                        FROM (
-                          SELECT TOP 9 [me].[id], [me].[name], name + ? AS [ORDER__BY__001]
-                            FROM [owners] [me]
-                            LEFT JOIN [books] [books]
-                              ON [books].[owner] = [me].[id]
-                          WHERE [books].[id] IS NOT NULL AND [me].[name] != ?
-                          GROUP BY [me].[id], [me].[name]
-                          ORDER BY name + ? ASC, [me].[id]
-                        ) [me]
-                      ORDER BY [ORDER__BY__001] DESC, [me].[id] DESC
-                    ) [me]
-                  ORDER BY [ORDER__BY__001] ASC, [me].[id]
-                ) [me]
-                LEFT JOIN [books] [books]
-                  ON [books].[owner] = [me].[id]
-              WHERE [books].[id] IS NOT NULL AND [me].[name] != ?
-              ORDER BY name + ? ASC, [me].[id]
-            )'
-            : '(
-              SELECT TOP 2147483647 [me].[id], [me].[name],
-                                    [books].[id], [books].[source], [books].[owner], [books].[title], [books].[price]
-                FROM (
-                  SELECT TOP 2147483647 [me].[id], [me].[name]
-                    FROM (
-                      SELECT [me].[id], [me].[name],
-                             ROW_NUMBER() OVER( ORDER BY [ORDER__BY__001] ASC, [me].[id] ) AS [rno__row__index]
-                        FROM (
-                          SELECT [me].[id], [me].[name], name + ? AS [ORDER__BY__001]
-                            FROM [owners] [me]
-                            LEFT JOIN [books] [books]
-                              ON [books].[owner] = [me].[id]
-                          WHERE [books].[id] IS NOT NULL AND [me].[name] != ?
-                          GROUP BY [me].[id], [me].[name]
-                        ) [me]
-                    ) [me]
-                  WHERE [rno__row__index] >= ? AND [rno__row__index] <= ?
-                ) [me]
-                LEFT JOIN [books] [books]
-                  ON [books].[owner] = [me].[id]
-              WHERE [books].[id] IS NOT NULL AND [me].[name] != ?
-              ORDER BY name + ? ASC, [me].[id]
-            )'
-          ,
-          [
-            [ { dbic_colname => 'test' }
-              => 'xxx' ],
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
-              => 'somebogusstring' ],
-
-            ($dialect eq 'Top'
-              ? [ { dbic_colname => 'test' } => 'xxx' ]  # the extra re-order bind
-              : ([ $OFFSET => 7 ], [ $TOTAL => 9 ]) # parameterised RNO
-            ),
-
-            [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
-              => 'somebogusstring' ],
-            [ { dbic_colname => 'test' }
-              => 'xxx' ],
-          ],
-        ) if $quoted;
-
         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");
 
@@ -470,88 +408,6 @@ SQL
           },
         );
 
-        is_same_sql_bind (
-          $books->page(3)->as_query,
-          $dialect eq 'Top'
-            ? '(
-              SELECT TOP 2147483647 [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price],
-                                    [owner].[id], [owner].[name]
-                FROM (
-                  SELECT TOP 2147483647 [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                    FROM (
-                      SELECT TOP 2 [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                        FROM (
-                          SELECT TOP 6 [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                            FROM [books] [me]
-                            JOIN [owners] [owner]
-                              ON [owner].[id] = [me].[owner]
-                          WHERE ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ?
-                          GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                          HAVING 1 = ?
-                          ORDER BY [me].[owner] DESC, [me].[id]
-                        ) [me]
-                      ORDER BY [me].[owner] ASC, [me].[id] DESC
-                    ) [me]
-                  ORDER BY [me].[owner] DESC, [me].[id]
-                ) [me]
-                JOIN [owners] [owner]
-                  ON [owner].[id] = [me].[owner]
-              WHERE ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ?
-              ORDER BY [me].[owner] DESC, [me].[id]
-            )'
-            : '(
-              SELECT TOP 2147483647 [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price],
-                                    [owner].[id], [owner].[name]
-                FROM (
-                  SELECT TOP 2147483647 [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                    FROM (
-                      SELECT [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price],
-                             ROW_NUMBER() OVER( ORDER BY [me].[owner] DESC, [me].[id] ) AS [rno__row__index]
-                        FROM (
-                          SELECT [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                            FROM [books] [me]
-                            JOIN [owners] [owner]
-                              ON [owner].[id] = [me].[owner]
-                          WHERE ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ?
-                          GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
-                          HAVING 1 = ?
-                        ) [me]
-                    ) [me]
-                  WHERE [rno__row__index] >= ? AND [rno__row__index] <= ?
-                ) [me]
-                JOIN [owners] [owner]
-                  ON [owner].[id] = [me].[owner]
-              WHERE ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ?
-              ORDER BY [me].[owner] DESC, [me].[id]
-            )'
-          ,
-          [
-            # 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' ],
-
-            # top(?)
-            $dialect eq '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' ],
-          ],
-        ) if $quoted;
-
         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");
 
@@ -644,24 +500,69 @@ SQL
           $row = $rs->create({ amount => 100 });
         } 'inserted a money value';
 
-        cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 100,
-          'money value round-trip');
+        cmp_ok (
+          ( eval { $rs->find($row->id)->amount } ) || 0,
+          '==',
+          100,
+          'money value round-trip'
+        );
 
         lives_ok {
           $row->update({ amount => 200 });
         } 'updated a money value';
 
-        cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 200,
-          'updated money value round-trip');
+        cmp_ok (
+          ( eval { $rs->find($row->id)->amount } ) || 0,
+          '==',
+          200,
+          'updated money value round-trip'
+        );
 
         lives_ok {
           $row->update({ amount => undef });
         } 'updated a money value to NULL';
 
-        is try { $rs->find($row->id)->amount }, undef,
-          'updated money value to NULL round-trip';
+        lives_ok {
+          is(
+            $rs->find($row->id)->amount,
+            undef,
+            'updated money value to NULL round-trip'
+          );
+        }
       }
     }
+
+# Test leakage of PK on implicit retrieval
+    {
+
+      my $next_owner = $schema->resultset('Owners')->get_column('id')->max + 1;
+      my $next_book = $schema->resultset('BooksInLibrary')->get_column('id')->max + 1;
+
+      cmp_ok(
+        $next_owner,
+        '!=',
+        $next_book,
+        'Preexisting auto-inc PKs staggered'
+      );
+
+      my $yet_another_owner = $schema->resultset('Owners')->create({ name => 'YAO' });
+      my $yet_another_book;
+      warnings_exist {
+        $yet_another_book = $yet_another_owner->create_related( books => { title => 'YAB' })
+      } qr/Missing value for primary key column 'id' on BooksInLibrary - perhaps you forgot to set its 'is_auto_increment'/;
+
+      is(
+        $yet_another_owner->id,
+        $next_owner,
+        'Expected Owner id'
+      );
+
+      is(
+        $yet_another_book->id,
+        $next_book,
+        'Expected Book id'
+      );
+    }
   }
 }