Add all database connections via DBICTest::Schema to the leaktrace pool
[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: {
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 lives_ok {
55
56 # insert a row to play with
57 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' });
58 is($new->trackid, 1, "insert sucessful");
59
60 my $track = $schema->resultset('Track')->find( 1 );
61
62 is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
63
64 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
65
66 #note '$track->last_updated_at => ', $track->last_updated_at;
67 is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
68
69 is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
70
71 my $dt = DateTime->now();
72 $track->last_updated_on($dt);
73 $track->last_updated_at($dt);
74 $track->update;
75
76 is( $track->last_updated_on->month, $dt->month, "deflate ok");
77 is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
78
79 # test datetime_setup
80
81 $schema->storage->disconnect;
82
83 delete $ENV{NLS_DATE_FORMAT};
84 delete $ENV{NLS_TIMESTAMP_FORMAT};
85
86 $schema->connection($dsn, $user, $pass, {
87     on_connect_call => 'datetime_setup'
88 });
89
90 $dt = DateTime->now();
91
92 my $timestamp = $dt->clone;
93 $timestamp->set_nanosecond( int 500_000_000 );
94
95 $track = $schema->resultset('Track')->find( 1 );
96 $track->update({ last_updated_on => $dt, last_updated_at => $timestamp });
97
98 $track = $schema->resultset('Track')->find(1);
99
100 is( $track->last_updated_on, $dt, 'DateTime round-trip as DATE' );
101 is( $track->last_updated_at, $timestamp, 'DateTime round-trip as TIMESTAMP' );
102
103 is( int $track->last_updated_at->nanosecond, int 500_000_000,
104   'TIMESTAMP nanoseconds survived' );
105
106 } 'dateteime operations executed correctly' } # end of lives_ok/TODO block
107
108 done_testing;
109
110 # clean up our mess
111 END {
112   if($schema && (my $dbh = $schema->storage->dbh)) {
113     $dbh->do("DROP TABLE track");
114   }
115   undef $schema;
116 }
117