Massive cleanup of DateTime test dependencies, other interim
[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
23 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
24
25 # Need to redefine the last_updated_on column
26 my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
27 $schema->class('Track')->add_column( 'last_updated_on' => {
28     data_type => 'date' });
29 $schema->class('Track')->add_column( 'last_updated_at' => {
30     data_type => 'timestamp' });
31
32 my $dbh = $schema->storage->dbh;
33
34 #$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'");
35
36 eval {
37   $dbh->do("DROP TABLE track");
38 };
39 $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, small_dt DATE)");
40
41 # insert a row to play with
42 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' });
43 is($new->trackid, 1, "insert sucessful");
44
45 my $track = $schema->resultset('Track')->find( 1 );
46
47 is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
48
49 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
50
51 #note '$track->last_updated_at => ', $track->last_updated_at;
52 is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
53
54 is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
55
56 my $dt = DateTime->now();
57 $track->last_updated_on($dt);
58 $track->last_updated_at($dt);
59 $track->update;
60
61 is( $track->last_updated_on->month, $dt->month, "deflate ok");
62 is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
63
64 # test datetime_setup
65
66 $schema->storage->disconnect;
67
68 delete $ENV{NLS_DATE_FORMAT};
69 delete $ENV{NLS_TIMESTAMP_FORMAT};
70
71 $schema->connection($dsn, $user, $pass, {
72     on_connect_call => 'datetime_setup'
73 });
74
75 $dt = DateTime->now();
76
77 my $timestamp = $dt->clone;
78 $timestamp->set_nanosecond( int 500_000_000 );
79
80 $track = $schema->resultset('Track')->find( 1 );
81 $track->update({ last_updated_on => $dt, last_updated_at => $timestamp });
82
83 $track = $schema->resultset('Track')->find(1);
84
85 is( $track->last_updated_on, $dt, 'DateTime round-trip as DATE' );
86 is( $track->last_updated_at, $timestamp, 'DateTime round-trip as TIMESTAMP' );
87
88 is( int $track->last_updated_at->nanosecond, int 500_000_000,
89   'TIMESTAMP nanoseconds survived' );
90
91 done_testing;
92
93 # clean up our mess
94 END {
95     if($schema && ($dbh = $schema->storage->dbh)) {
96         $dbh->do("DROP TABLE track");
97     }
98 }
99