Remove TODO labels from blocks not using todo_skip() - no test changes
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_oracle.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use DBIx::Class::Optional::Dependencies ();
7 use lib qw(t/lib);
8 use DBICTest;
9
10 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
11   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
12
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
14
15 if (not ($dsn && $user && $pass)) {
16     plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
17          'Warning: This test drops and creates a table called \'track\'';
18 }
19
20 # DateTime::Format::Oracle needs this set
21 $ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
22 $ENV{NLS_TIMESTAMP_FORMAT} = 'YYYY-MM-DD HH24:MI:SSXFF';
23 $ENV{NLS_LANG} = 'AMERICAN_AMERICA.WE8ISO8859P1';
24 $ENV{NLS_SORT} = "BINARY";
25 $ENV{NLS_COMP} = "BINARY";
26
27 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
28
29 # older oracles do not support a TIMESTAMP datatype
30 my $timestamp_datatype = ($schema->storage->_server_info->{normalized_dbms_version}||0) < 9
31   ? 'DATE'
32   : 'TIMESTAMP'
33 ;
34
35 # Need to redefine the last_updated_on column
36 my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
37 $schema->class('Track')->add_column( 'last_updated_on' => {
38     data_type => 'date' });
39 $schema->class('Track')->add_column( 'last_updated_at' => {
40     data_type => $timestamp_datatype });
41
42 my $dbh = $schema->storage->dbh;
43
44 #$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'");
45
46 eval {
47   $dbh->do("DROP TABLE track");
48 };
49 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at $timestamp_datatype)");
50
51 # TODO is in effect for the rest of the tests
52 local $TODO = 'FIXME - something odd is going on with Oracle < 9 datetime support'
53   if ($schema->storage->_server_info->{normalized_dbms_version}||0) < 9;
54
55 lives_ok {
56
57 # insert a row to play with
58 my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07', last_updated_at => '2009-05-03 21:17:18.5' });
59 is($new->trackid, 1, "insert sucessful");
60
61 my $track = $schema->resultset('Track')->find( 1 );
62
63 is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
64
65 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
66
67 #note '$track->last_updated_at => ', $track->last_updated_at;
68 is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
69
70 is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
71
72 my $dt = DateTime->now();
73 $track->last_updated_on($dt);
74 $track->last_updated_at($dt);
75 $track->update;
76
77 is( $track->last_updated_on->month, $dt->month, "deflate ok");
78 is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
79
80 # test datetime_setup
81
82 $schema->storage->disconnect;
83
84 delete $ENV{NLS_DATE_FORMAT};
85 delete $ENV{NLS_TIMESTAMP_FORMAT};
86
87 $schema->connection($dsn, $user, $pass, {
88     on_connect_call => 'datetime_setup'
89 });
90
91 $dt = DateTime->now();
92
93 my $timestamp = $dt->clone;
94 $timestamp->set_nanosecond( int 500_000_000 );
95
96 $track = $schema->resultset('Track')->find( 1 );
97 $track->update({ last_updated_on => $dt, last_updated_at => $timestamp });
98
99 $track = $schema->resultset('Track')->find(1);
100
101 is( $track->last_updated_on, $dt, 'DateTime round-trip as DATE' );
102 is( $track->last_updated_at, $timestamp, 'DateTime round-trip as TIMESTAMP' );
103
104 is( int $track->last_updated_at->nanosecond, int 500_000_000,
105   'TIMESTAMP nanoseconds survived' );
106
107 } 'dateteime operations executed correctly';
108
109 done_testing;
110
111 # clean up our mess
112 END {
113   if($schema && (my $dbh = $schema->storage->dbh)) {
114     $dbh->do("DROP TABLE track");
115   }
116   undef $schema;
117 }
118