9 my $schema = DBICTest->init_schema();
11 eval { require DateTime };
12 plan skip_all => "Need DateTime for inflation tests" if $@;
14 $schema->class('CD') ->inflate_column( 'year',
15 { inflate => sub { DateTime->new( year => shift ) },
16 deflate => sub { shift->year } }
19 my $rs = $schema->resultset('CD');
22 my $cd = $rs->find(3);
24 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
26 is( $cd->year->year, 1997, 'inflated year ok' );
28 is( $cd->year->month, 1, 'inflated month ok' );
30 eval { $cd->year(\'year +1'); };
31 ok(!$@, 'updated year using a scalarref');
33 $cd->discard_changes();
35 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
37 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
39 is( $cd->year->month, 1, 'month is still 1' );
41 # get_inflated_column test
43 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
46 my $now = DateTime->now;
51 is( $cd->year->year, $now->year, 'deflate ok' );
53 # set_inflated_column test
54 eval { $cd->set_inflated_column('year', $now) };
55 ok(!$@, 'set_inflated_column with DateTime object');
59 is( $cd->year->year, $now->year, 'deflate ok' );
62 my $before_year = $cd->year->year;
63 eval { $cd->set_inflated_column('year', \'year + 1') };
64 ok(!$@, 'set_inflated_column to "year + 1"');
67 $cd->store_inflated_column('year', \'year + 1');
68 is_deeply( $cd->year, \'year + 1', 'scalarref deflate passthrough ok' );
71 is( $cd->year->year, $before_year+1, 'deflate ok' );
73 # store_inflated_column test
75 eval { $cd->store_inflated_column('year', $now) };
76 ok(!$@, 'store_inflated_column with DateTime object');
79 is( $cd->year->year, $now->year, 'deflate ok' );
83 eval { $cd->update({'year' => $now}) };
84 ok(!$@, 'update using DateTime object ok');
85 is($cd->year->year, $now->year, 'deflate ok');
88 $before_year = $cd->year->year;
89 eval { $cd->update({'year' => \'year + 1'}) };
90 ok(!$@, 'update using scalarref ok');
93 is($cd->year->year, $before_year + 1, 'deflate ok');
95 # discard_changes test
98 $before_year = $cd->year->year;
99 $cd->update({ year => \'year + 1'});
100 $cd->discard_changes;
102 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
104 my $copy = $cd->copy({ year => $now, title => "zemoose" });
106 is( $copy->year->year, $now->year, "copy" );
110 my $artist = $cd->artist;
116 title => 'create with scalarref',
119 is ($cd->year, $sval, 'scalar value retained');
120 my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' });
121 is ($cd2->year, $sval, 'copied scalar value retained');
123 $cd->discard_changes;
124 is ($cd->year->year, 2012, 'infation upon reload');
126 $cd2->discard_changes;
127 is ($cd2->year->year, 2012, 'infation upon reload of copy');
130 my $precount = $rs->count;
131 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval });
132 is ($rs->count, $precount + 1, 'Row created');
134 is ($cd->year, $sval, 'scalar value retained on creating update_or_create');
135 $cd->discard_changes;
136 is ($cd->year->year, 2012, 'infation upon reload');
138 my $sval2 = \ '2013';
140 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 });
141 is ($rs->count, $precount + 1, 'No more rows created');
143 is ($cd->year, $sval2, 'scalar value retained on updating update_or_create');
144 $cd->discard_changes;
145 is ($cd->year->year, 2013, 'infation upon reload');