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