Proper support for timestamp inflation. Added last_updated_at to DBICTest::Schema...
[dbsrgits/DBIx-Class.git] / t / 73oracle_inflate.t
CommitLineData
8f7e044c 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
9
95cbe02e 10if (not ($dsn && $user && $pass)) {
8f7e044c 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}
14else {
95cbe02e 15 eval "use DateTime; use DateTime::Format::Oracle;";
16 if ($@) {
17 plan skip_all => 'needs DateTime and DateTime::Format::Oracle for testing';
18 }
19 else {
abc914bd 20 plan tests => 7;
95cbe02e 21 }
8f7e044c 22}
23
24# DateTime::Format::Oracle needs this set
25$ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
abc914bd 26$ENV{NLS_TIMESTAMP_FORMAT} = 'YYYY-MM-DD HH24:MI:SSXFF';
8f7e044c 27
28my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
29
30# Need to redefine the last_updated_on column
31my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
32$schema->class('Track')->add_column( 'last_updated_on' => {
33 data_type => 'date' });
abc914bd 34$schema->class('Track')->add_column( 'last_updated_at' => {
35 data_type => 'timestamp' });
8f7e044c 36
37my $dbh = $schema->storage->dbh;
38
abc914bd 39#$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'");
40
8f7e044c 41eval {
42 $dbh->do("DROP TABLE track");
43};
abc914bd 44$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)");
8f7e044c 45
46# insert a row to play with
abc914bd 47my $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' });
8f7e044c 48is($new->trackid, 1, "insert sucessful");
49
50my $track = $schema->resultset('Track')->find( 1 );
51
52is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
53
54is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
55
abc914bd 56#note '$track->last_updated_at => ', $track->last_updated_at;
57is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
58
59is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
60
8f7e044c 61my $dt = DateTime->now();
62$track->last_updated_on($dt);
abc914bd 63$track->last_updated_at($dt);
8f7e044c 64$track->update;
65
66is( $track->last_updated_on->month, $dt->month, "deflate ok");
abc914bd 67is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
8f7e044c 68
69# clean up our mess
70END {
8f7e044c 71 if($dbh) {
72 $dbh->do("DROP TABLE track");
73 }
74}
75