Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / 73oracle_inflate.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 eval "use DateTime; use DateTime::Format::Oracle;";
11 if ($@) {
12     plan skip_all => 'needs DateTime and DateTime::Format::Oracle for testing';
13 }
14 elsif (not ($dsn && $user && $pass)) {
15     plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
16          'Warning: This test drops and creates a table called \'track\'';
17 }
18 else {
19     plan tests => 4;
20 }
21
22 # DateTime::Format::Oracle needs this set
23 $ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
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
32 my $dbh = $schema->storage->dbh;
33
34 eval {
35   $dbh->do("DROP TABLE track");
36 };
37 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
38
39 # insert a row to play with
40 my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07' });
41 is($new->trackid, 1, "insert sucessful");
42
43 my $track = $schema->resultset('Track')->find( 1 );
44
45 is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
46
47 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
48
49 my $dt = DateTime->now();
50 $track->last_updated_on($dt);
51 $track->update;
52
53 is( $track->last_updated_on->month, $dt->month, "deflate ok");
54
55 # clean up our mess
56 END {
57     if($dbh) {
58         $dbh->do("DROP TABLE track");
59     }
60 }
61