Remove small_datetime from the main schema - it is not a standard datatype
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_oracle.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
9
10 if (not ($dsn && $user && $pass)) {
11     plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
12          'Warning: This test drops and creates a table called \'track\'';
13 }
14
15 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
16   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
17
18 # DateTime::Format::Oracle needs this set
19 $ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
20 $ENV{NLS_TIMESTAMP_FORMAT} = 'YYYY-MM-DD HH24:MI:SSXFF';
21 $ENV{NLS_LANG} = 'AMERICAN_AMERICA.WE8ISO8859P1';
22 $ENV{NLS_SORT} = "BINARY";
23 $ENV{NLS_COMP} = "BINARY";
24
25 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
26
27 # Need to redefine the last_updated_on column
28 my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
29 $schema->class('Track')->add_column( 'last_updated_on' => {
30     data_type => 'date' });
31 $schema->class('Track')->add_column( 'last_updated_at' => {
32     data_type => 'timestamp' });
33
34 my $dbh = $schema->storage->dbh;
35
36 #$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'");
37
38 eval {
39   $dbh->do("DROP TABLE track");
40 };
41 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at TIMESTAMP)");
42
43 # insert a row to play with
44 my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07', last_updated_at => '2009-05-03 21:17:18.5' });
45 is($new->trackid, 1, "insert sucessful");
46
47 my $track = $schema->resultset('Track')->find( 1 );
48
49 is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
50
51 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
52
53 #note '$track->last_updated_at => ', $track->last_updated_at;
54 is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
55
56 is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
57
58 my $dt = DateTime->now();
59 $track->last_updated_on($dt);
60 $track->last_updated_at($dt);
61 $track->update;
62
63 is( $track->last_updated_on->month, $dt->month, "deflate ok");
64 is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
65
66 # test datetime_setup
67
68 $schema->storage->disconnect;
69
70 delete $ENV{NLS_DATE_FORMAT};
71 delete $ENV{NLS_TIMESTAMP_FORMAT};
72
73 $schema->connection($dsn, $user, $pass, {
74     on_connect_call => 'datetime_setup'
75 });
76
77 $dt = DateTime->now();
78
79 my $timestamp = $dt->clone;
80 $timestamp->set_nanosecond( int 500_000_000 );
81
82 $track = $schema->resultset('Track')->find( 1 );
83 $track->update({ last_updated_on => $dt, last_updated_at => $timestamp });
84
85 $track = $schema->resultset('Track')->find(1);
86
87 is( $track->last_updated_on, $dt, 'DateTime round-trip as DATE' );
88 is( $track->last_updated_at, $timestamp, 'DateTime round-trip as TIMESTAMP' );
89
90 is( int $track->last_updated_at->nanosecond, int 500_000_000,
91   'TIMESTAMP nanoseconds survived' );
92
93 done_testing;
94
95 # clean up our mess
96 END {
97     if($schema && ($dbh = $schema->storage->dbh)) {
98         $dbh->do("DROP TABLE track");
99     }
100 }
101