9 my $schema = DBICTest->init_schema();
11 plan skip_all => 'Inflation tests need ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
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' );
31 sub { $cd->year(\'year +1') },
32 'updated year using a scalarref'
35 $cd->discard_changes();
37 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
39 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
41 is( $cd->year->month, 1, 'month is still 1' );
43 # get_inflated_column test
45 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
48 my $now = DateTime->now;
53 is( $cd->year->year, $now->year, 'deflate ok' );
55 # set_inflated_column test
57 sub { $cd->set_inflated_column('year', $now) },
58 'set_inflated_column with DateTime object'
63 is( $cd->year->year, $now->year, 'deflate ok' );
66 my $before_year = $cd->year->year;
68 sub { $cd->set_inflated_column('year', \'year + 1') },
69 'set_inflated_column to "year + 1"',
73 $cd->store_inflated_column('year', \'year + 1');
74 is_deeply( $cd->year, \'year + 1', 'scalarref deflate passthrough ok' );
77 is( $cd->year->year, $before_year+1, 'deflate ok' );
79 # store_inflated_column test
82 sub { $cd->store_inflated_column('year', $now) },
83 'store_inflated_column with DateTime object'
87 is( $cd->year->year, $now->year, 'deflate ok' );
92 sub { $cd->update({'year' => $now}) },
93 'update using DateTime object ok'
95 is($cd->year->year, $now->year, 'deflate ok');
98 $before_year = $cd->year->year;
100 sub { $cd->update({'year' => \'year + 1'}) },
101 'update using scalarref ok'
105 is($cd->year->year, $before_year + 1, 'deflate ok');
107 # discard_changes test
110 $before_year = $cd->year->year;
111 $cd->update({ year => \'year + 1'});
112 $cd->discard_changes;
114 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
116 my $copy = $cd->copy({ year => $now, title => "zemoose" });
118 is( $copy->year->year, $now->year, "copy" );
122 my $artist = $cd->artist;
128 title => 'create with scalarref',
131 is ($cd->year, $sval, 'scalar value retained');
132 my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' });
133 is ($cd2->year, $sval, 'copied scalar value retained');
135 $cd->discard_changes;
136 is ($cd->year->year, 2012, 'infation upon reload');
138 $cd2->discard_changes;
139 is ($cd2->year->year, 2012, 'infation upon reload of copy');
142 my $precount = $rs->count;
143 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval });
144 is ($rs->count, $precount + 1, 'Row created');
146 is ($cd->year, $sval, 'scalar value retained on creating update_or_create');
147 $cd->discard_changes;
148 is ($cd->year->year, 2012, 'infation upon reload');
150 my $sval2 = \ '2013';
152 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 });
153 is ($rs->count, $precount + 1, 'No more rows created');
155 is ($cd->year, $sval2, 'scalar value retained on updating update_or_create');
156 $cd->discard_changes;
157 is ($cd->year->year, 2013, 'infation upon reload');