fix rounding issues in mssql money tests
[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
7379eb67 21my $TESTS = 13;
eef2ff6c 22
7379eb67 23plan tests => $TESTS * 2;
eef2ff6c 24
7379eb67 25my @storage_types = (
c29ce317 26 'DBI::Sybase::Microsoft_SQL_Server',
27 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
7379eb67 28);
29my $storage_idx = -1;
30my $schema;
31
32for my $storage_type (@storage_types) {
33 $storage_idx++;
b3f41261 34
7379eb67 35 $schema = DBICTest::Schema->clone;
36
37 if ($storage_idx != 0) { # autodetect
c29ce317 38 $schema->storage_type("::$storage_type");
7379eb67 39 }
651682ae 40
7379eb67 41 $schema->connection($dsn, $user, $pass);
8c52ffd3 42
7379eb67 43 $schema->storage->ensure_connected;
eef2ff6c 44
7379eb67 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
c29ce317 51 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
7379eb67 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);");
3ff5b740 68# Just to test compat shim, Auto is in Core
7379eb67 69 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 70
71# Test PK
7379eb67 72 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
73 ok($new->artistid, "Auto-PK worked");
eef2ff6c 74
75# Test LIMIT
7379eb67 76 for (1..6) {
77 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
78 }
eef2ff6c 79
7379eb67 80 my $it = $schema->resultset('Artist')->search( { },
81 { rows => 3,
82 offset => 2,
83 order_by => 'artistid'
84 }
85 );
eef2ff6c 86
b4474f31 87# Test ? in data don't get treated as placeholders
7379eb67 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" );
eef2ff6c 100
5064f5c3 101# test MONEY column support
7379eb67 102 $schema->storage->dbh_do (sub {
103 my ($storage, $dbh) = @_;
104 eval { $dbh->do("DROP TABLE money_test") };
105 $dbh->do(<<'SQL');
5064f5c3 106
7379eb67 107 CREATE TABLE money_test (
108 id INT IDENTITY PRIMARY KEY,
109 amount MONEY NULL
110 )
5064f5c3 111
112SQL
113
7379eb67 114 });
5064f5c3 115
7379eb67 116 my $rs = $schema->resultset('Money');
5064f5c3 117
7379eb67 118 my $row;
119 lives_ok {
120 $row = $rs->create({ amount => 100 });
121 } 'inserted a money value';
5064f5c3 122
c8365716 123 is int($rs->find($row->id)->amount), 100, 'money value round-trip';
5064f5c3 124
7379eb67 125 lives_ok {
126 $row->update({ amount => 200 });
127 } 'updated a money value';
5064f5c3 128
c8365716 129 is int($rs->find($row->id)->amount), 200, 'updated money value round-trip';
5064f5c3 130
7379eb67 131 lives_ok {
132 $row->update({ amount => undef });
133 } 'updated a money value to NULL';
5064f5c3 134
c8365716 135 is $rs->find($row->id)->amount,
136 undef, 'updated money value to NULL round-trip';
7379eb67 137}
5064f5c3 138
3ff5b740 139# clean up our mess
140END {
7379eb67 141 if (my $dbh = eval { $schema->storage->dbh }) {
142 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
143 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
144 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
145 }
3ff5b740 146}