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