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