5b5b8666872ae4dba47890b5a00cfe134cd5e7b9
[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 lib qw(t/lib);
8 use DBICTest;
9
10 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" }      qw/DSN USER PASS/};
11 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
12
13 if (not ($dsn || $dsn2)) {
14   plan skip_all => <<'EOF';
15 Set $ENV{DBICTEST_SQLANYWHERE_DSN} and/or $ENV{DBICTEST_SQLANYWHERE_ODBC_DSN}
16 _USER and _PASS to run this test'.
17 Warning: This test drops and creates a table called 'event'";
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   eval { $schema->storage->dbh->do('DROP TABLE event') };
45   $schema->storage->dbh->do(<<"SQL");
46   CREATE TABLE event (
47     id INT IDENTITY PRIMARY KEY,
48     created_on TIMESTAMP,
49     starts_at DATE
50   )
51 SQL
52
53 # coltype, col, date
54   my @dt_types = (
55     [
56       'TIMESTAMP',
57       'created_on',
58       '2004-08-21 14:36:48.080445',
59     ],
60 # date only (but minute precision according to ASA docs)
61     [
62       'DATE',
63       'starts_at',
64       '2004-08-21 00:00:00.000000',
65     ],
66   );
67
68   for my $dt_type (@dt_types) {
69     my ($type, $col, $sample_dt) = @$dt_type;
70
71     ok(my $dt = $schema->storage->datetime_parser->parse_datetime($sample_dt));
72
73     my $row;
74     ok( $row = $schema->resultset('Event')->create({ $col => $dt, }));
75     ok( $row = $schema->resultset('Event')
76       ->search({ id => $row->id }, { select => [$col] })
77       ->first
78     );
79     is( $row->$col, $dt, "$type roundtrip" );
80
81     is $row->$col->nanosecond, $dt->nanosecond,
82         'nanoseconds survived' if 0+$dt->nanosecond;
83   }
84 }
85
86 done_testing;
87
88 # clean up our mess
89 sub cleanup {
90   if (my $dbh = $schema->storage->dbh) {
91     eval { $dbh->do("DROP TABLE $_") } for qw/event/;
92   }
93 }