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