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