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