1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( test_rdbms_sqlite ic_dt );
11 my $schema = DBICTest->init_schema();
13 $schema->class('CD') ->inflate_column( 'year',
14 { inflate => sub { DateTime->new( year => shift ) },
15 deflate => sub { shift->year } }
18 my $rs = $schema->resultset('CD');
21 my $cd = $rs->find(3);
23 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
25 is( $cd->year->year, 1997, 'inflated year ok' );
27 is( $cd->year->month, 1, 'inflated month ok' );
30 sub { $cd->year(\'year +1') },
31 'updated year using a scalarref'
34 $cd->discard_changes();
36 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
38 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
40 is( $cd->year->month, 1, 'month is still 1' );
42 # get_inflated_column test
44 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
47 my $now = DateTime->now;
52 is( $cd->year->year, $now->year, 'deflate ok' );
54 # set_inflated_column test
56 sub { $cd->set_inflated_column('year', $now) },
57 'set_inflated_column with DateTime object'
62 is( $cd->year->year, $now->year, 'deflate ok' );
65 my $before_year = $cd->year->year;
67 sub { $cd->set_inflated_column('year', \'year + 1') },
68 'set_inflated_column to "year + 1"',
72 $cd->store_inflated_column('year', \'year + 1');
73 is_deeply( $cd->year, \'year + 1', 'scalarref deflate passthrough ok' );
76 is( $cd->year->year, $before_year+1, 'deflate ok' );
78 # store_inflated_column test
81 sub { $cd->store_inflated_column('year', $now) },
82 'store_inflated_column with DateTime object'
86 is( $cd->year->year, $now->year, 'deflate ok' );
91 sub { $cd->update({'year' => $now}) },
92 'update using DateTime object ok'
94 is($cd->year->year, $now->year, 'deflate ok');
97 $before_year = $cd->year->year;
99 sub { $cd->update({'year' => \'year + 1'}) },
100 'update using scalarref ok'
104 is($cd->year->year, $before_year + 1, 'deflate ok');
106 # discard_changes test
109 $before_year = $cd->year->year;
110 $cd->update({ year => \'year + 1'});
111 $cd->discard_changes;
113 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
115 my $copy = $cd->copy({ year => $now, title => "zemoose" });
117 is( $copy->year->year, $now->year, "copy" );
121 my $artist = $cd->artist;
127 title => 'create with scalarref',
130 is ($cd->year, $sval, 'scalar value retained');
131 my $cd2 = $cd->copy ({ title => 'copy with scalar in coldata' });
132 is ($cd2->year, $sval, 'copied scalar value retained');
134 $cd->discard_changes;
135 is ($cd->year->year, 2012, 'infation upon reload');
137 $cd2->discard_changes;
138 is ($cd2->year->year, 2012, 'infation upon reload of copy');
141 my $precount = $rs->count;
142 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval });
143 is ($rs->count, $precount + 1, 'Row created');
145 is ($cd->year, $sval, 'scalar value retained on creating update_or_create');
146 $cd->discard_changes;
147 is ($cd->year->year, 2012, 'infation upon reload');
149 my $sval2 = \ '2013';
151 $cd = $rs->update_or_create ({artist => $artist, title => 'nonexisting update/create test row', year => $sval2 });
152 is ($rs->count, $precount + 1, 'No more rows created');
154 is ($cd->year, $sval2, 'scalar value retained on updating update_or_create');
155 $cd->discard_changes;
156 is ($cd->year->year, 2013, 'infation upon reload');