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