cc444a094e167dfabf46baf01876c0c67d600b2a
[dbsrgits/DBIx-Class-Historic.git] / t / 68inflate.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 DBICTest::Schema::CD->add_column('year');
9 my $schema = DBICTest->init_schema();
10
11 eval { require DateTime };
12 plan skip_all => "Need DateTime for inflation tests" if $@;
13
14 plan tests => 15;
15
16 DBICTest::Schema::CD->inflate_column( 'year',
17     { inflate => sub { DateTime->new( year => shift ) },
18       deflate => sub { shift->year } }
19 );
20 Class::C3->reinitialize;
21
22 # inflation test
23 my $cd = $schema->resultset("CD")->find(3);
24
25 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
26
27 is( $cd->year->year, 1997, 'inflated year ok' );
28
29 is( $cd->year->month, 1, 'inflated month ok' );
30
31 eval { $cd->year(\'year +1'); };
32 ok(!$@, 'updated year using a scalarref');
33 $cd->update();
34 $cd->discard_changes();
35
36 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
37
38 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
39
40 is( $cd->year->month, 1, 'month is still 1' );  
41
42 # get_inflated_column test
43
44 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
45
46 # deflate test
47 my $now = DateTime->now;
48 $cd->year( $now );
49 $cd->update;
50
51 ($cd) = $schema->resultset("CD")->search( year => $now->year );
52 is( $cd->year->year, $now->year, 'deflate ok' );
53
54 # set_inflated_column test
55 eval { $cd->set_inflated_column('year', $now) };
56 ok(!$@, 'set_inflated_column with DateTime object');
57 $cd->update;
58
59 ($cd) = $schema->resultset("CD")->search( year => $now->year );                 
60 is( $cd->year->year, $now->year, 'deflate ok' );
61
62 eval { $cd->set_inflated_column('year', \'year + 1') };
63 print STDERR "ERROR: $@" if($@);
64 ok(!$@, 'set_inflated_column to "year + 1"');
65 $cd->update;
66 $cd->discard_changes();
67
68 ($cd) = $schema->resultset("CD")->search( year => $now->year + 1 );                 
69 is( $cd->year->year, $now->year+1, 'deflate ok' );
70
71 # store_inflated_column test
72 eval { $cd->store_inflated_column('year', $now) };
73 ok(!$@, 'store_inflated_column with DateTime object');
74 $cd->update;
75
76 is( $cd->year->year, $now->year, 'deflate ok' );
77
78 # eval { $cd->store_inflated_column('year', \'year + 1') };
79 # print STDERR "ERROR: $@" if($@);
80 # ok(!$@, 'store_inflated_column to "year + 1"');
81
82 # is_deeply( $cd->year, \'year + 1', 'deflate ok' );
83