1use strict; 2use warnings; 3 4use Test::More; 5use Test::Exception; 6use lib qw(t/lib); 7use DBICTest; 8 9my $schema = DBICTest->init_schema(); 10 11plan 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'); 13 14$schema->class('CD') ->inflate_column( 'year', 15 { inflate => sub { DateTime->new( year => shift ) }, 16 deflate => sub { shift->year } } 17); 18 19my $rs = $schema->resultset('CD'); 20 21# inflation test 22my $cd = $rs->find(3); 23 24is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); 25 26is( $cd->year->year, 1997, 'inflated year ok' ); 27 28is( $cd->year->month, 1, 'inflated month ok' ); 29 30lives_ok ( 31 sub { $cd->year(\'year +1') }, 32 'updated year using a scalarref' 33); 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 47# deflate test 48my $now = DateTime->now; 49$cd->year( $now ); 50$cd->update; 51 52$cd = $rs->find(3); 53is( $cd->year->year, $now->year, 'deflate ok' ); 54 55# set_inflated_column test 56lives_ok ( 57 sub { $cd->set_inflated_column('year', $now) }, 58 'set_inflated_column with DateTime object' 59); 60$cd->update; 61 62$cd = $rs->find(3); 63is( $cd->year->year, $now->year, 'deflate ok' ); 64 65$cd = $rs->find(3); 66my $before_year = $cd->year->year; 67lives_ok ( 68 sub { $cd->set_inflated_column('year', \'year + 1') }, 69 'set_inflated_column to "year + 1"', 70); 71$cd->update; 72 73$cd->store_inflated_column('year', \'year + 1'); 74is_deeply( $cd->year, \'year + 1', 'scalarref deflate passthrough ok' ); 75 76$cd = $rs->find(3); 77is( $cd->year->year, $before_year+1, 'deflate ok' ); 78 79# store_inflated_column test 80$cd = $rs->find(3); 81lives_ok ( 82 sub { $cd->store_inflated_column('year', $now) }, 83 'store_inflated_column with DateTime object' 84); 85$cd->update; 86 87is( $cd->year->year, $now->year, 'deflate ok' ); 88 89# update tests 90$cd = $rs->find(3); 91lives_ok ( 92 sub { $cd->update({'year' => $now}) }, 93 'update using DateTime object ok' 94); 95is($cd->year->year, $now->year, 'deflate ok'); 96 97$cd = $rs->find(3); 98$before_year = $cd->year->year; 99lives_ok ( 100 sub { $cd->update({'year' => \'year + 1'}) }, 101 'update using scalarref ok' 102); 103 104$cd = $rs->find(3); 105is($cd->year->year, $before_year + 1, 'deflate ok'); 106 107# discard_changes test 108$cd = $rs->find(3); 109# inflate the year 110$before_year = $cd->year->year; 111$cd->update({ year => \'year + 1'}); 112$cd->discard_changes; 113 114is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value'); 115 116my $copy = $cd->copy({ year => $now, title => "zemoose" }); 117 118is( $copy->year->year, $now->year, "copy" ); 119 120 121 122my $artist = $cd->artist; 123my $sval = \ '2012'; 124 125$cd = $rs->create ({ 126 artist => $artist, 127 year => $sval, 128 title => 'create with scalarref', 129}); 130 131is ($cd->year, $sval, 'scalar value retained'); 132my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' }); 133is ($cd2->year, $sval, 'copied scalar value retained'); 134 135$cd->discard_changes; 136is ($cd->year->year, 2012, 'infation upon reload'); 137 138$cd2->discard_changes; 139is ($cd2->year->year, 2012, 'infation upon reload of copy'); 140 141 142my $precount = $rs->count; 143$cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval }); 144is ($rs->count, $precount + 1, 'Row created'); 145 146is ($cd->year, $sval, 'scalar value retained on creating update_or_create'); 147$cd->discard_changes; 148is ($cd->year->year, 2012, 'infation upon reload'); 149 150my $sval2 = \ '2013'; 151 152$cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 }); 153is ($rs->count, $precount + 1, 'No more rows created'); 154 155is ($cd->year, $sval2, 'scalar value retained on updating update_or_create'); 156$cd->discard_changes; 157is ($cd->year->year, 2013, 'infation upon reload'); 158 159done_testing; 160