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