88049be930913f7d7e6b1004426a7cfe5dc99b68
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sybase_asa.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use Scope::Guard ();
7 use lib qw(t/lib);
8 use DBICTest;
9
10 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_SYBASE_ASA_${_}" }      qw/DSN USER PASS/};
11 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SYBASE_ASA_ODBC_${_}" } qw/DSN USER PASS/};
12
13 if (not ($dsn || $dsn2)) {
14   plan skip_all => <<'EOF';
15 Set $ENV{DBICTEST_SYBASE_ASA_DSN} and/or $ENV{DBICTEST_SYBASE_ASA_ODBC_DSN}
16 _USER and _PASS to run this test'.
17 Warning: This test drops and creates a table called 'track'";
18 EOF
19 } else {
20   eval "use DateTime; use DateTime::Format::Strptime;";
21   if ($@) {
22     plan skip_all => 'needs DateTime and DateTime::Format::Strptime for testing';
23   }
24 }
25
26 my @info = (
27   [ $dsn,  $user,  $pass  ],
28   [ $dsn2, $user2, $pass2 ],
29 );
30
31 my $schema;
32
33 foreach my $info (@info) {
34   my ($dsn, $user, $pass) = @$info;
35
36   next unless $dsn;
37
38   $schema = DBICTest::Schema->clone;
39
40   $schema->connection($dsn, $user, $pass, {
41     on_connect_call => [ 'datetime_setup' ],
42   });
43
44   my $sg = Scope::Guard->new(\&cleanup); 
45
46 # coltype, col, date
47   my @dt_types = (
48     ['TIMESTAMP', 'last_updated_at', '2004-08-21 14:36:48.080445'],
49 # date only (but minute precision according to ASA docs)
50     ['DATE', 'small_dt', '2004-08-21 00:00:00.000000'],
51   );
52
53   for my $dt_type (@dt_types) {
54     my ($type, $col, $sample_dt) = @$dt_type;
55
56     eval { $schema->storage->dbh->do("DROP TABLE track") };
57     $schema->storage->dbh->do(<<"SQL");
58     CREATE TABLE track (
59       trackid INT IDENTITY PRIMARY KEY,
60       cd INT,
61       position INT,
62       $col $type,
63     )
64 SQL
65     ok(my $dt = $schema->storage->datetime_parser->parse_datetime($sample_dt));
66
67     my $row;
68     ok( $row = $schema->resultset('Track')->create({
69           $col => $dt,
70           cd => 1,
71         }));
72     ok( $row = $schema->resultset('Track')
73       ->search({ trackid => $row->trackid }, { select => [$col] })
74       ->first
75     );
76     is( $row->$col, $dt, "$type roundtrip" );
77
78     is $row->$col->nanosecond, $dt->nanosecond,
79         'nanoseconds survived' if 0+$dt->nanosecond;
80   }
81 }
82
83 done_testing;
84
85 # clean up our mess
86 sub cleanup {
87   if (my $dbh = $schema->storage->dbh) {
88     eval { $dbh->do("DROP TABLE $_") } for qw/track/;
89   }
90 }