Segfault
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
1 use strict;
2 use warnings;  
3
4 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5 BEGIN {
6   if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7     unshift @INC, $_ for split /:/, $lib_dirs;
8   }
9 }
10
11 use Test::More;
12 use Test::Exception;
13 use lib qw(t/lib);
14 use DBICTest;
15
16 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
17
18 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
19   unless ($dsn);
20
21 plan tests => 7;
22
23 my $schema = DBICTest::Schema->clone;
24 $schema->connection($dsn, $user, $pass);
25
26 # start disconnected to test reconnection
27 $schema->storage->ensure_connected;
28 $schema->storage->_dbh->disconnect;
29
30 isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server');
31
32 my $dbh;
33 lives_ok (sub {
34   $dbh = $schema->storage->dbh;
35 }, 'reconnect works');
36
37 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
38     DROP TABLE artist");
39 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
40     DROP TABLE cd");
41
42 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
43 $dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT,  title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
44 # Just to test compat shim, Auto is in Core
45 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
46
47 # Test PK
48 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
49 ok($new->artistid, "Auto-PK worked");
50
51 # Test LIMIT
52 for (1..6) {
53     $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
54 }
55
56 my $it = $schema->resultset('Artist')->search( { },
57     { rows     => 3,
58       offset   => 2,
59       order_by => 'artistid'
60     }
61 );
62
63 # Test ? in data don't get treated as placeholders
64 my $cd = $schema->resultset('CD')->create( {
65     artist      => 1,
66     title       => 'Does this break things?',
67     year        => 2007,
68 } );
69 ok($cd->id, 'Not treating ? in data as placeholders');
70
71 is( $it->count, 3, "LIMIT count ok" );
72 ok( $it->next->name, "iterator->next ok" );
73 $it->next;
74 $it->next;
75 is( $it->next, undef, "next past end of resultset ok" );
76
77 # clean up our mess
78 END {
79     $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist")
80         if $dbh;
81     $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd")
82         if $dbh;
83 }