on_connect_call implementation and set_datetime_format support for Oracle
[dbsrgits/DBIx-Class.git] / t / 68inflate.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
0567538f 9
10eval { require DateTime };
11plan skip_all => "Need DateTime for inflation tests" if $@;
12
3a975f43 13plan tests => 21;
0567538f 14
93405cf0 15$schema->class('CD')
16#DBICTest::Schema::CD
17->inflate_column( 'year',
0567538f 18 { inflate => sub { DateTime->new( year => shift ) },
19 deflate => sub { shift->year } }
20);
2d679367 21Class::C3->reinitialize;
0567538f 22
23# inflation test
f9db5527 24my $cd = $schema->resultset("CD")->find(3);
0567538f 25
26is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
27
89279e9d 28is( $cd->year->year, 1997, 'inflated year ok' );
29
0567538f 30is( $cd->year->month, 1, 'inflated month ok' );
31
e81a6241 32eval { $cd->year(\'year +1'); };
33ok(!$@, 'updated year using a scalarref');
34$cd->update();
35$cd->discard_changes();
36
37is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
38
39is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
40
41is( $cd->year->month, 1, 'month is still 1' );
42
43# get_inflated_column test
44
45is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
46
0567538f 47# deflate test
48my $now = DateTime->now;
49$cd->year( $now );
50$cd->update;
51
ad5d0ee9 52$cd = $schema->resultset("CD")->find(3);
0567538f 53is( $cd->year->year, $now->year, 'deflate ok' );
54
e81a6241 55# set_inflated_column test
56eval { $cd->set_inflated_column('year', $now) };
57ok(!$@, 'set_inflated_column with DateTime object');
58$cd->update;
59
ad5d0ee9 60$cd = $schema->resultset("CD")->find(3);
e81a6241 61is( $cd->year->year, $now->year, 'deflate ok' );
62
ad5d0ee9 63$cd = $schema->resultset("CD")->find(3);
64my $before_year = $cd->year->year;
e81a6241 65eval { $cd->set_inflated_column('year', \'year + 1') };
e81a6241 66ok(!$@, 'set_inflated_column to "year + 1"');
67$cd->update;
e81a6241 68
ad5d0ee9 69$cd = $schema->resultset("CD")->find(3);
70is( $cd->year->year, $before_year+1, 'deflate ok' );
e81a6241 71
72# store_inflated_column test
ad5d0ee9 73$cd = $schema->resultset("CD")->find(3);
e81a6241 74eval { $cd->store_inflated_column('year', $now) };
75ok(!$@, 'store_inflated_column with DateTime object');
76$cd->update;
77
78is( $cd->year->year, $now->year, 'deflate ok' );
79
ad5d0ee9 80# update tests
81$cd = $schema->resultset("CD")->find(3);
82eval { $cd->update({'year' => $now}) };
83ok(!$@, 'update using DateTime object ok');
84is($cd->year->year, $now->year, 'deflate ok');
85
86$cd = $schema->resultset("CD")->find(3);
87$before_year = $cd->year->year;
88eval { $cd->update({'year' => \'year + 1'}) };
89ok(!$@, 'update using scalarref ok');
90
91$cd = $schema->resultset("CD")->find(3);
92is($cd->year->year, $before_year + 1, 'deflate ok');
93
94# discard_changes test
95$cd = $schema->resultset("CD")->find(3);
96# inflate the year
97$before_year = $cd->year->year;
98$cd->update({ year => \'year + 1'});
99$cd->discard_changes;
100
101is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
07d4fb20 102
103my $copy = $cd->copy({ year => $now, title => "zemoose" });
104
105isnt( $copy->year->year, $before_year, "copy" );
ad5d0ee9 106
e81a6241 107# eval { $cd->store_inflated_column('year', \'year + 1') };
108# print STDERR "ERROR: $@" if($@);
109# ok(!$@, 'store_inflated_column to "year + 1"');
110
111# is_deeply( $cd->year, \'year + 1', 'deflate ok' );
112