X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F746mssql.t;h=d1b8773f05a2137cf2ddeff25d3e50c19dea4734;hb=6c7ca96253b07f8ac9b6940ac35368d25ed3b139;hp=ac16bc810dc7957a6cde330d61ca16ee76d50e61;hpb=9ffaf8a3d9eede54f95134bd62af2bcc6e650bba;p=dbsrgits%2FDBIx-Class.git diff --git a/t/746mssql.t b/t/746mssql.t index ac16bc8..d1b8773 100644 --- a/t/746mssql.t +++ b/t/746mssql.t @@ -1,21 +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 lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; +use Test::Warn; use Try::Tiny; -use DBIx::Class::SQLMaker::LimitDialects; -my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype; -my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype; -my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/}; +use DBICTest; -plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test' - unless ($dsn && $user); +my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/}; { my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version}; @@ -39,10 +36,9 @@ my $schema = DBICTest::Schema->connect($dsn, $user, $pass); isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' ); { - my $schema2 = $schema->connect ($schema->storage->connect_info); + my $schema2 = $schema->connect (@{$schema->storage->connect_info}); ok (! $schema2->storage->connected, 'a re-connected cloned schema starts unconnected'); } - $schema->storage->_dbh->disconnect; lives_ok { @@ -53,10 +49,12 @@ my %opts = ( use_mars => { opts => { on_connect_call => 'use_mars' } }, use_dynamic_cursors => - { opts => { on_connect_call => 'use_dynamic_cursors' }, required => 1 }, + { opts => { on_connect_call => 'use_dynamic_cursors' }, + required => $schema->storage->_using_freetds ? 0 : 1, + }, use_server_cursors => { opts => { on_connect_call => 'use_server_cursors' } }, - NO_OPTION => + plain => { opts => {}, required => 1 }, ); @@ -70,12 +68,13 @@ for my $opts_name (keys %opts) { } 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 -"on_connect_call option '$opts_name' not functional in this configuration: $_", -1; + "on_connect_call option '$opts_name' not functional in this configuration: $_", + 1 + ; } }; @@ -100,35 +99,69 @@ 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' ) { - my $artist_rs = $schema->resultset('Artist'); + # keep the first cursor alive (as long as $rs is alive) + my $rs = $schema->resultset("Artist"); - $artist_rs->delete; + my $a1 = $rs->next; - $artist_rs->create({ name => "Artist$_" }) for (1..3); + my $a2; - my $forward = $artist_rs->search({}, - { order_by => { -asc => 'artistid' } }); - my $backward = $artist_rs->search({}, - { order_by => { -desc => 'artistid' } }); + 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'; - my @map = ( - [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/] + is_deeply( + { $a1->get_columns }, + { $a2->get_columns }, + 'Same data', ); - my @result; - while (my $forward_row = $forward->next) { - my $backward_row = $backward->next; - push @result, [$forward_row->name, $backward_row->name]; - } + dies_ok { + $rs->next; + } 'Invalid cursor did not silently return garbage'; + } + +# Test multiple active statements + else { + $schema->storage->ensure_connected; + + lives_ok { - is_deeply \@result, \@map, "multiple active statements in $opts_name"; + no warnings 'redefine'; + local *DBI::connect = sub { die "NO RECONNECTS!!!" }; - $artist_rs->delete; + my $artist_rs = $schema->resultset('Artist'); + + $artist_rs->delete; + + $artist_rs->create({ name => "Artist$_" }) for (1..3); + + my $forward = $artist_rs->search({}, + { order_by => { -asc => 'artistid' } }); + my $backward = $artist_rs->search({}, + { order_by => { -desc => 'artistid' } }); + + my @map = ( + [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/] + ); + my @result; + + while (my $forward_row = $forward->next) { + my $backward_row = $backward->next; + push @result, [$forward_row->name, $backward_row->name]; + } + + is_deeply \@result, \@map, "multiple active statements in $opts_name"; + + $artist_rs->delete; + + is($artist_rs->count, 0, '$dbh still viable'); + } "Multiple active statements survive $opts_name"; } # Test populate @@ -235,7 +268,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); @@ -268,41 +301,12 @@ 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", ); } - { - my $book_owner_ids = $schema->resultset ('BooksInLibrary')->search ({}, { - rows => 6, - offset => 2, - join => 'owner', - distinct => 1, - order_by => 'owner.name', - unsafe_subselect_ok => 1 - })->get_column ('owner'); - - my @ids = $book_owner_ids->all; - - is (@ids, 6, 'Limit works'); - - my $book_owners = $schema->resultset ('Owners')->search ({ - id => { -in => $book_owner_ids->as_query } - }); - - TODO: { - local $TODO = "Correlated limited IN subqueries will probably never preserve order"; - - is_deeply ( - [ map { $_->id } ($book_owners->all) ], - [ $book_owner_ids->all ], - "$test_type: Sort is preserved across IN subqueries", - ); - } - } - # still even with lost order of IN, we should be getting correct # sets { @@ -348,20 +352,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) ], @@ -378,35 +375,18 @@ SQL }, { prefetch => 'books', - order_by => { -asc => \['name + ?', [ test => 'xxx' ]] }, # test bindvar propagation + order_by => [ { -asc => \['name + ?', [ test => 'xxx' ]] }, 'me.id' ], # test bindvar propagation group_by => [ map { "me.$_" } $schema->source('Owners')->columns ], # the literal order_by requires an explicit group_by rows => 3, # 8 results total unsafe_subselect_ok => 1, }, ); - 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"); @@ -424,42 +404,16 @@ SQL having => \['1 = ?', [ test => 1 ] ], #test having propagation prefetch => 'owner', rows => 2, # 3 results total - order_by => { -desc => 'me.owner' }, + order_by => [{ -desc => 'me.owner' }, 'me.id'], unsafe_subselect_ok => 1, }, ); - ($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"); @@ -529,11 +483,16 @@ CREATE TABLE money_test ( SQL }); - TODO: { + { + my $freetds_and_dynamic_cursors = 1 + if $opts_name eq 'use_dynamic_cursors' && + $schema->storage->_using_freetds; + local $TODO = 'these tests fail on freetds with dynamic cursors for some reason' - if $opts_name eq 'use_dynamic_cursors' - && $schema->storage->using_freetds; + if $freetds_and_dynamic_cursors; + local $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1 + if $freetds_and_dynamic_cursors; my $rs = $schema->resultset('Money'); my $row; @@ -571,5 +530,6 @@ END { eval { $dbh->do("DROP TABLE $_") } for qw/artist artist_guid money_test books owners/; } + undef $schema; } # vim:sw=2 sts=2