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