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