Massive cleanup of DateTime test dependencies, other interim
[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 }
16
17 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ase')
18   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ase');
19
20 my @storage_types = (
21   'DBI::Sybase::ASE',
22   'DBI::Sybase::ASE::NoBindVars',
23 );
24 my $schema;
25
26 for my $storage_type (@storage_types) {
27   $schema = DBICTest::Schema->clone;
28
29   unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
30     $schema->storage_type("::$storage_type");
31   }
32   $schema->connection($dsn, $user, $pass, {
33     AutoCommit => 1,
34     on_connect_call => [ 'datetime_setup' ],
35   });
36
37   $schema->storage->ensure_connected;
38
39   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
40
41 # coltype, col, date
42   my @dt_types = (
43     ['DATETIME', 'last_updated_at', '2004-08-21T14:36:48.080Z'],
44 # minute precision
45     ['SMALLDATETIME', 'small_dt', '2004-08-21T14:36:00.000Z'],
46   );
47
48   for my $dt_type (@dt_types) {
49     my ($type, $col, $sample_dt) = @$dt_type;
50
51     eval { $schema->storage->dbh->do("DROP TABLE track") };
52     $schema->storage->dbh->do(<<"SQL");
53 CREATE TABLE track (
54    trackid INT IDENTITY PRIMARY KEY,
55    cd INT NULL,
56    position INT NULL,
57    $col $type NULL
58 )
59 SQL
60     ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt));
61
62     my $row;
63     ok( $row = $schema->resultset('Track')->create({
64           $col => $dt,
65           cd => 1,
66         }));
67     ok( $row = $schema->resultset('Track')
68       ->search({ trackid => $row->trackid }, { select => [$col] })
69       ->first
70     );
71     is( $row->$col, $dt, "$type roundtrip" );
72
73     is( $row->$col->nanosecond, $dt->nanosecond,
74       'fractional DateTime portion roundtrip' )
75       if $dt->nanosecond > 0;
76   }
77
78   # test a computed datetime column
79   eval { $schema->storage->dbh->do("DROP TABLE track") };
80   $schema->storage->dbh->do(<<"SQL");
81 CREATE TABLE track (
82    trackid INT IDENTITY PRIMARY KEY,
83    cd INT NULL,
84    position INT NULL,
85    title VARCHAR(100) NULL,
86    last_updated_on DATETIME NULL,
87    last_updated_at AS getdate(),
88    small_dt SMALLDATETIME NULL
89 )
90 SQL
91
92   my $now     = DateTime->now;
93   sleep 1;
94   my $new_row = $schema->resultset('Track')->create({});
95   $new_row->discard_changes;
96
97   lives_and {
98     cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
99   } 'getdate() computed column works';
100 }
101
102 done_testing;
103
104 # clean up our mess
105 END {
106   if (my $dbh = eval { $schema->storage->_dbh }) {
107     $dbh->do('DROP TABLE track');
108   }
109 }