f1ff6fcc9cea2d67c6ddd5f77d977030c3f9e999
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sybase.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
10
11 if (not ($dsn && $user)) {
12   plan skip_all =>
13     'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
14     "\nWarning: This test drops and creates a table called 'track'";
15 } else {
16   eval "use DateTime; use DateTime::Format::Sybase;";
17   if ($@) {
18     plan skip_all => 'needs DateTime and DateTime::Format::Sybase for testing';
19   }
20 }
21
22 my @storage_types = (
23   'DBI::Sybase::ASE',
24   'DBI::Sybase::ASE::NoBindVars',
25 );
26 my $schema;
27
28 for my $storage_type (@storage_types) {
29   $schema = DBICTest::Schema->clone;
30
31   unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
32     $schema->storage_type("::$storage_type");
33   }
34   $schema->connection($dsn, $user, $pass, {
35     AutoCommit => 1,
36     on_connect_call => [ 'datetime_setup' ],
37   });
38
39   $schema->storage->ensure_connected;
40
41   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
42
43 # coltype, col, date
44   my @dt_types = (
45     ['DATETIME', 'last_updated_at', '2004-08-21T14:36:48.080Z'],
46 # minute precision
47     ['SMALLDATETIME', 'small_dt', '2004-08-21T14:36:00.000Z'],
48   );
49   
50   for my $dt_type (@dt_types) {
51     my ($type, $col, $sample_dt) = @$dt_type;
52
53     eval { $schema->storage->dbh->do("DROP TABLE track") };
54     $schema->storage->dbh->do(<<"SQL");
55 CREATE TABLE track (
56    trackid INT IDENTITY PRIMARY KEY,
57    cd INT NULL,
58    position INT NULL,
59    $col $type NULL
60 )
61 SQL
62     ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt));
63
64     my $row;
65     ok( $row = $schema->resultset('Track')->create({
66           $col => $dt,
67           cd => 1,
68         }));
69     ok( $row = $schema->resultset('Track')
70       ->search({ trackid => $row->trackid }, { select => [$col] })
71       ->first
72     );
73     is( $row->$col, $dt, "$type roundtrip" );
74
75     is( $row->$col->nanosecond, $dt->nanosecond,
76       'fractional DateTime portion roundtrip' )
77       if $dt->nanosecond > 0;
78   }
79
80   # test a computed datetime column
81   eval { $schema->storage->dbh->do("DROP TABLE track") };
82   $schema->storage->dbh->do(<<"SQL");
83 CREATE TABLE track (
84    trackid INT IDENTITY PRIMARY KEY,
85    cd INT NULL,
86    position INT NULL,
87    title VARCHAR(100) NULL,
88    last_updated_on DATETIME NULL,
89    last_updated_at AS getdate(),
90    small_dt SMALLDATETIME NULL
91 )
92 SQL
93
94   my $now     = DateTime->now;
95   sleep 1;
96   my $new_row = $schema->resultset('Track')->create({});
97   $new_row->discard_changes;
98
99   lives_and {
100     cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
101   } 'getdate() computed column works';
102 }
103
104 done_testing;
105
106 # clean up our mess
107 END {
108   if (my $dbh = eval { $schema->storage->_dbh }) {
109     $dbh->do('DROP TABLE track');
110   }
111 }