update Changes
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
CommitLineData
70350518 1use strict;
2use warnings;
3
46e3af47 4# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5BEGIN {
6 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7 unshift @INC, $_ for split /:/, $lib_dirs;
8 }
9}
10
70350518 11use Test::More;
8c52ffd3 12use Test::Exception;
70350518 13use lib qw(t/lib);
14use DBICTest;
eef2ff6c 15
16my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
17
70350518 18plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
eef2ff6c 19 unless ($dsn);
20
5064f5c3 21plan tests => 13;
eef2ff6c 22
3ff5b740 23my $schema = DBICTest::Schema->clone;
3ff5b740 24$schema->connection($dsn, $user, $pass);
eef2ff6c 25
b3f41261 26# start disconnected to test reconnection
27$schema->storage->ensure_connected;
526dc858 28$schema->storage->_dbh->disconnect;
b3f41261 29
98464041 30isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server');
651682ae 31
8c52ffd3 32my $dbh;
33lives_ok (sub {
34 $dbh = $schema->storage->dbh;
35}, 'reconnect works');
36
eef2ff6c 37$dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
38 DROP TABLE artist");
b10cb676 39$dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
40 DROP TABLE cd");
eef2ff6c 41
a0dd8679 42$dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
a1cb5921 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);");
3ff5b740 44# Just to test compat shim, Auto is in Core
45$schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 46
47# Test PK
3ff5b740 48my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
eef2ff6c 49ok($new->artistid, "Auto-PK worked");
50
51# Test LIMIT
52for (1..6) {
5432c6ae 53 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
eef2ff6c 54}
55
3ff5b740 56my $it = $schema->resultset('Artist')->search( { },
eef2ff6c 57 { rows => 3,
58 offset => 2,
59 order_by => 'artistid'
60 }
61);
62
b4474f31 63# Test ? in data don't get treated as placeholders
64my $cd = $schema->resultset('CD')->create( {
65 artist => 1,
66 title => 'Does this break things?',
67 year => 2007,
68} );
69ok($cd->id, 'Not treating ? in data as placeholders');
70
eef2ff6c 71is( $it->count, 3, "LIMIT count ok" );
f48dd03f 72ok( $it->next->name, "iterator->next ok" );
eef2ff6c 73$it->next;
74$it->next;
75is( $it->next, undef, "next past end of resultset ok" );
76
5064f5c3 77# test MONEY column support
78$schema->storage->dbh_do (sub {
79 my ($storage, $dbh) = @_;
80 eval { $dbh->do("DROP TABLE money_test") };
81 $dbh->do(<<'SQL');
82
83CREATE TABLE money_test (
84 id INT IDENTITY PRIMARY KEY,
85 amount MONEY NULL
86)
87
88SQL
89
90});
91
92my $rs = $schema->resultset('Money');
93
94my $row;
95lives_ok {
96 $row = $rs->create({ amount => 100 });
97} 'inserted a money value';
98
99is $rs->find($row->id)->amount, 100, 'money value round-trip';
100
101lives_ok {
102 $row->update({ amount => 200 });
103} 'updated a money value';
104
105is $rs->find($row->id)->amount, 200, 'updated money value round-trip';
106
107lives_ok {
108 $row->update({ amount => undef });
109} 'updated a money value to NULL';
110
111is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
112
3ff5b740 113# clean up our mess
114END {
115 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist")
116 if $dbh;
b4474f31 117 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd")
118 if $dbh;
5064f5c3 119 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test")
120 if $dbh;
3ff5b740 121}