Trailing WS crusade - got to save them bits
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sqlanywhere.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Scope::Guard ();
7 use DBIx::Class::Optional::Dependencies ();
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" }      qw/DSN USER PASS/};
12 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
13
14 plan skip_all => 'Test needs ' .
15   (join ' and ', map { $_ ? $_ : () }
16     DBIx::Class::Optional::Dependencies->req_missing_for('test_dt'),
17     (join ' or ', map { $_ ? $_ : () }
18       DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere'),
19       DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere_odbc')))
20   unless
21     DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt') && (
22     $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere')
23     or
24     $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere_odbc'))
25       or (not $dsn || $dsn2);
26
27 if (not ($dsn || $dsn2)) {
28   plan skip_all => <<'EOF';
29 Set $ENV{DBICTEST_SQLANYWHERE_DSN} and/or $ENV{DBICTEST_SQLANYWHERE_ODBC_DSN}
30 _USER and _PASS to run this test'.
31 Warning: This test drops and creates a table called 'event'";
32 EOF
33 }
34
35 my @info = (
36   [ $dsn,  $user,  $pass  ],
37   [ $dsn2, $user2, $pass2 ],
38 );
39
40 my $schema;
41
42 foreach my $info (@info) {
43   my ($dsn, $user, $pass) = @$info;
44
45   next unless $dsn;
46
47   $schema = DBICTest::Schema->clone;
48
49   $schema->connection($dsn, $user, $pass, {
50     on_connect_call => 'datetime_setup',
51   });
52
53   my $sg = Scope::Guard->new(\&cleanup);
54
55   eval { $schema->storage->dbh->do('DROP TABLE event') };
56   $schema->storage->dbh->do(<<"SQL");
57   CREATE TABLE event (
58     id INT IDENTITY PRIMARY KEY,
59     created_on TIMESTAMP,
60     starts_at DATE
61   )
62 SQL
63
64 # coltype, col, date
65   my @dt_types = (
66     [
67       'TIMESTAMP',
68       'created_on',
69       '2004-08-21 14:36:48.080445',
70     ],
71 # date only (but minute precision according to ASA docs)
72     [
73       'DATE',
74       'starts_at',
75       '2004-08-21 00:00:00.000000',
76     ],
77   );
78
79   for my $dt_type (@dt_types) {
80     my ($type, $col, $sample_dt) = @$dt_type;
81
82     ok(my $dt = $schema->storage->datetime_parser->parse_datetime($sample_dt));
83
84     my $row;
85     ok( $row = $schema->resultset('Event')->create({ $col => $dt, }));
86     ok( $row = $schema->resultset('Event')
87       ->search({ id => $row->id }, { select => [$col] })
88       ->first
89     );
90     is( $row->$col, $dt, "$type roundtrip" );
91
92     is $row->$col->nanosecond, $dt->nanosecond,
93         'nanoseconds survived' if 0+$dt->nanosecond;
94   }
95 }
96
97 done_testing;
98
99 # clean up our mess
100 sub cleanup {
101   if (my $dbh = $schema->storage->dbh) {
102     eval { $dbh->do("DROP TABLE $_") } for qw/event/;
103   }
104 }