Inflate/Deflate on all refs but scalars, with tests and all!
[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
ec9c93f1 8DBICTest::Schema::CD->add_column('year');
a47e1233 9my $schema = DBICTest->init_schema();
0567538f 10
11eval { require DateTime };
12plan skip_all => "Need DateTime for inflation tests" if $@;
13
e81a6241 14plan tests => 15;
0567538f 15
2d679367 16DBICTest::Schema::CD->inflate_column( 'year',
0567538f 17 { inflate => sub { DateTime->new( year => shift ) },
18 deflate => sub { shift->year } }
19);
2d679367 20Class::C3->reinitialize;
0567538f 21
22# inflation test
f9db5527 23my $cd = $schema->resultset("CD")->find(3);
0567538f 24
25is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
26
89279e9d 27is( $cd->year->year, 1997, 'inflated year ok' );
28
0567538f 29is( $cd->year->month, 1, 'inflated month ok' );
30
e81a6241 31eval { $cd->year(\'year +1'); };
32ok(!$@, 'updated year using a scalarref');
33$cd->update();
34$cd->discard_changes();
35
36is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
37
38is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
39
40is( $cd->year->month, 1, 'month is still 1' );
41
42# get_inflated_column test
43
44is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
45
0567538f 46# deflate test
47my $now = DateTime->now;
48$cd->year( $now );
49$cd->update;
50
f9db5527 51($cd) = $schema->resultset("CD")->search( year => $now->year );
0567538f 52is( $cd->year->year, $now->year, 'deflate ok' );
53
e81a6241 54# set_inflated_column test
55eval { $cd->set_inflated_column('year', $now) };
56ok(!$@, 'set_inflated_column with DateTime object');
57$cd->update;
58
59($cd) = $schema->resultset("CD")->search( year => $now->year );
60is( $cd->year->year, $now->year, 'deflate ok' );
61
62eval { $cd->set_inflated_column('year', \'year + 1') };
63print STDERR "ERROR: $@" if($@);
64ok(!$@, 'set_inflated_column to "year + 1"');
65$cd->update;
66$cd->discard_changes();
67
68($cd) = $schema->resultset("CD")->search( year => $now->year + 1 );
69is( $cd->year->year, $now->year+1, 'deflate ok' );
70
71# store_inflated_column test
72eval { $cd->store_inflated_column('year', $now) };
73ok(!$@, 'store_inflated_column with DateTime object');
74$cd->update;
75
76is( $cd->year->year, $now->year, 'deflate ok' );
77
78# eval { $cd->store_inflated_column('year', \'year + 1') };
79# print STDERR "ERROR: $@" if($@);
80# ok(!$@, 'store_inflated_column to "year + 1"');
81
82# is_deeply( $cd->year, \'year + 1', 'deflate ok' );
83