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