Massive cleanup of DateTime test dependencies, other interim
[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 }
20
21 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
22   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
23
24 my @info = (
25   [ $dsn,  $user,  $pass  ],
26   [ $dsn2, $user2, $pass2 ],
27 );
28
29 my $schema;
30
31 foreach my $info (@info) {
32   my ($dsn, $user, $pass) = @$info;
33
34   next unless $dsn;
35
36   $schema = DBICTest::Schema->clone;
37
38   $schema->connection($dsn, $user, $pass, {
39     on_connect_call => [ 'datetime_setup' ],
40   });
41
42   my $sg = Scope::Guard->new(\&cleanup); 
43
44 # coltype, col, date
45   my @dt_types = (
46     ['TIMESTAMP', 'last_updated_at', '2004-08-21 14:36:48.080445'],
47 # date only (but minute precision according to ASA docs)
48     ['DATE', 'small_dt', '2004-08-21 00:00:00.000000'],
49   );
50
51   for my $dt_type (@dt_types) {
52     my ($type, $col, $sample_dt) = @$dt_type;
53
54     eval { $schema->storage->dbh->do("DROP TABLE track") };
55     $schema->storage->dbh->do(<<"SQL");
56     CREATE TABLE track (
57       trackid INT IDENTITY PRIMARY KEY,
58       cd INT,
59       position INT,
60       $col $type,
61     )
62 SQL
63     ok(my $dt = $schema->storage->datetime_parser->parse_datetime($sample_dt));
64
65     my $row;
66     ok( $row = $schema->resultset('Track')->create({
67           $col => $dt,
68           cd => 1,
69         }));
70     ok( $row = $schema->resultset('Track')
71       ->search({ trackid => $row->trackid }, { select => [$col] })
72       ->first
73     );
74     is( $row->$col, $dt, "$type roundtrip" );
75
76     is $row->$col->nanosecond, $dt->nanosecond,
77         'nanoseconds survived' if 0+$dt->nanosecond;
78   }
79 }
80
81 done_testing;
82
83 # clean up our mess
84 sub cleanup {
85   if (my $dbh = $schema->storage->dbh) {
86     eval { $dbh->do("DROP TABLE $_") } for qw/track/;
87   }
88 }