Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / sybase.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_ase );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Test::Exception;
9 use DBIx::Class::_Util 'scope_guard';
10
11 use DBICTest;
12
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
14
15 DBICTest::Schema->load_classes('EventSmallDT');
16
17 my @storage_types = (
18   'DBI::Sybase::ASE',
19   'DBI::Sybase::ASE::NoBindVars',
20 );
21 my $schema;
22
23 for my $storage_type (@storage_types) {
24   $schema = DBICTest::Schema->clone;
25
26   unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
27     $schema->storage_type("::$storage_type");
28   }
29   $schema->connection($dsn, $user, $pass, {
30     on_connect_call => 'datetime_setup',
31   });
32
33   my $guard = scope_guard { cleanup($schema) };
34
35   $schema->storage->ensure_connected;
36
37   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
38
39   eval { $schema->storage->dbh->do("DROP TABLE track") };
40   $schema->storage->dbh->do(<<"SQL");
41 CREATE TABLE track (
42     trackid INT IDENTITY PRIMARY KEY,
43     cd INT NULL,
44     position INT NULL,
45     last_updated_at DATETIME NULL
46 )
47 SQL
48   eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
49   $schema->storage->dbh->do(<<"SQL");
50 CREATE TABLE event_small_dt (
51     id INT IDENTITY PRIMARY KEY,
52     small_dt SMALLDATETIME NULL,
53 )
54 SQL
55
56 # coltype, column, source, pk, create_extra, datehash
57   my @dt_types = (
58     ['DATETIME',
59      'last_updated_at',
60      'Track',
61      'trackid',
62      { cd => 1 },
63      {
64       year => 2004,
65       month => 8,
66       day => 21,
67       hour => 14,
68       minute => 36,
69       second => 48,
70       nanosecond => 500000000,
71     }],
72     ['SMALLDATETIME', # minute precision
73      'small_dt',
74      'EventSmallDT',
75      'id',
76      {},
77      {
78       year => 2004,
79       month => 8,
80       day => 21,
81       hour => 14,
82       minute => 36,
83     }],
84   );
85
86   for my $dt_type (@dt_types) {
87     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
88
89     ok(my $dt = DateTime->new($sample_dt));
90
91     my $row;
92     ok( $row = $schema->resultset($source)->create({
93           $col => $dt,
94           %$create_extra,
95         }));
96     ok( $row = $schema->resultset($source)
97       ->search({ $pk => $row->$pk }, { select => [$col] })
98       ->first
99     );
100     is( $row->$col, $dt, "$type roundtrip" );
101
102     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
103       'DateTime fractional portion roundtrip' )
104       if exists $sample_dt->{nanosecond};
105   }
106
107   # test a computed datetime column
108   eval { $schema->storage->dbh->do("DROP TABLE track") };
109   $schema->storage->dbh->do(<<"SQL");
110 CREATE TABLE track (
111     trackid INT IDENTITY PRIMARY KEY,
112     cd INT NULL,
113     position INT NULL,
114     title VARCHAR(100) NULL,
115     last_updated_on DATETIME NULL,
116     last_updated_at AS getdate(),
117 )
118 SQL
119
120   my $now = DateTime->now;
121   sleep 1;
122   my $new_row = $schema->resultset('Track')->create({});
123   $new_row->discard_changes;
124
125   lives_and {
126     cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
127   } 'getdate() computed column works';
128 }
129
130 done_testing;
131
132 # clean up our mess
133 sub cleanup {
134   my $schema = shift;
135   if (my $dbh = eval { $schema->storage->dbh }) {
136     $dbh->do('DROP TABLE track');
137     $dbh->do('DROP TABLE event_small_dt');
138   }
139 }