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"');
68 local $TODO = 'this was left in without a TODO - should it work?';
71 $cd->store_inflated_column('year', \'year + 1');
72 is_deeply( $cd->year, \'year + 1', 'deflate ok' );
73 }, 'store_inflated_column to "year + 1"');
77 is( $cd->year->year, $before_year+1, 'deflate ok' );
79 # store_inflated_column test
81 eval { $cd->store_inflated_column('year', $now) };
82 ok(!$@, 'store_inflated_column with DateTime object');
85 is( $cd->year->year, $now->year, 'deflate ok' );
89 eval { $cd->update({'year' => $now}) };
90 ok(!$@, 'update using DateTime object ok');
91 is($cd->year->year, $now->year, 'deflate ok');
94 $before_year = $cd->year->year;
95 eval { $cd->update({'year' => \'year + 1'}) };
96 ok(!$@, 'update using scalarref ok');
99 is($cd->year->year, $before_year + 1, 'deflate ok');
101 # discard_changes test
104 $before_year = $cd->year->year;
105 $cd->update({ year => \'year + 1'});
106 $cd->discard_changes;
108 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
110 my $copy = $cd->copy({ year => $now, title => "zemoose" });
112 is( $copy->year->year, $now->year, "copy" );
116 my $artist = $cd->artist;
122 title => 'create with scalarref',
125 is ($cd->year, $sval, 'scalar value retained');
126 my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' });
127 is ($cd2->year, $sval, 'copied scalar value retained');
129 $cd->discard_changes;
130 is ($cd->year->year, 2012, 'infation upon reload');
132 $cd2->discard_changes;
133 is ($cd2->year->year, 2012, 'infation upon reload of copy');
136 my $precount = $rs->count;
137 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval });
138 is ($rs->count, $precount + 1, 'Row created');
140 is ($cd->year, $sval, 'scalar value retained on creating update_or_create');
141 $cd->discard_changes;
142 is ($cd->year->year, 2012, 'infation upon reload');
144 my $sval2 = \ '2013';
146 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 });
147 is ($rs->count, $precount + 1, 'No more rows created');
149 is ($cd->year, $sval2, 'scalar value retained on updating update_or_create');
150 $cd->discard_changes;
151 is ($cd->year->year, 2013, 'infation upon reload');