Merge 'trunk' into 'sybase_support'
[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 my $TESTS = 15;
22
23 plan tests => $TESTS * 2;
24
25 my @storage_types = (
26   'DBI::Sybase::Microsoft_SQL_Server',
27   'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
28 );
29 my $storage_idx = -1;
30 my $schema;
31
32 for my $storage_type (@storage_types) {
33   $storage_idx++;
34
35   $schema = DBICTest::Schema->clone;
36
37   if ($storage_idx != 0) { # autodetect
38     $schema->storage_type("::$storage_type");
39   }
40
41   $schema->connection($dsn, $user, $pass);
42
43   $schema->storage->ensure_connected;
44
45   if ($storage_idx == 0 && ref($schema->storage) =~ /NoBindVars\z/) {
46     my $tb = Test::More->builder;
47     $tb->skip('no placeholders') for 1..$TESTS;
48     next;
49   }
50
51   isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
52
53 # start disconnected to test reconnection
54   $schema->storage->_dbh->disconnect;
55
56   my $dbh;
57   lives_ok (sub {
58     $dbh = $schema->storage->dbh;
59   }, 'reconnect works');
60
61   $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
62       DROP TABLE artist");
63   $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
64       DROP TABLE cd");
65
66   $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
67   $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);");
68 # Just to test compat shim, Auto is in Core
69   $schema->class('Artist')->load_components('PK::Auto::MSSQL');
70
71 # Test PK
72   my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
73   ok($new->artistid, "Auto-PK worked");
74
75 # Test LIMIT
76   for (1..6) {
77       $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
78   }
79
80   my $it = $schema->resultset('Artist')->search( { },
81       { rows     => 3,
82         offset   => 2,
83         order_by => 'artistid'
84       }
85   );
86
87 # Test ? in data don't get treated as placeholders
88   my $cd = $schema->resultset('CD')->create( {
89       artist      => 1,
90       title       => 'Does this break things?',
91       year        => 2007,
92   } );
93   ok($cd->id, 'Not treating ? in data as placeholders');
94
95   is( $it->count, 3, "LIMIT count ok" );
96   ok( $it->next->name, "iterator->next ok" );
97   $it->next;
98   $it->next;
99   is( $it->next, undef, "next past end of resultset ok" );
100
101 # test MONEY column support
102   $schema->storage->dbh_do (sub {
103       my ($storage, $dbh) = @_;
104       eval { $dbh->do("DROP TABLE money_test") };
105       $dbh->do(<<'SQL');
106   CREATE TABLE money_test (
107      id INT IDENTITY PRIMARY KEY,
108      amount MONEY NULL
109   )
110 SQL
111
112   });
113
114   my $rs = $schema->resultset('Money');
115
116   my $row;
117   lives_ok {
118     $row = $rs->create({ amount => 100 });
119   } 'inserted a money value';
120
121   cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
122
123   lives_ok {
124     $row->update({ amount => 200 });
125   } 'updated a money value';
126
127   cmp_ok $rs->find($row->id)->amount, '==', 200,
128     'updated money value round-trip';
129
130   lives_ok {
131     $row->update({ amount => undef });
132   } 'updated a money value to NULL';
133
134   is $rs->find($row->id)->amount,
135     undef, 'updated money value to NULL round-trip';
136
137   $rs->create({ amount => 300 }) for (1..3);
138
139   # test multiple active statements
140   lives_ok {
141     my $artist_rs = $schema->resultset('Artist');
142     while (my $row = $rs->next) {
143       my $artist = $artist_rs->next;
144     }
145     $rs->reset;
146   } 'multiple active statements';
147
148   # test multiple active statements in a transaction
149   TODO: {
150     local $TODO = 'needs similar FreeTDS fixes to the ones in Sybase.pm';
151     lives_ok {
152       $schema->txn_do(sub {
153         $rs->create({ amount => 400 });
154       });
155     } 'simple transaction';
156   }
157 }
158
159 # clean up our mess
160 END {
161   if (my $dbh = eval { $schema->storage->dbh }) {
162     $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
163     $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
164     $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
165   }
166 }