Move around inflation tests
[dbsrgits/DBIx-Class-Historic.git] / t / inflate / core.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 eval { require DateTime };
11 plan skip_all => "Need DateTime for inflation tests" if $@;
12
13 plan tests => 22;
14
15 $schema->class('CD') ->inflate_column( 'year',
16     { inflate => sub { DateTime->new( year => shift ) },
17       deflate => sub { shift->year } }
18 );
19
20 # inflation test
21 my $cd = $schema->resultset("CD")->find(3);
22
23 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
24
25 is( $cd->year->year, 1997, 'inflated year ok' );
26
27 is( $cd->year->month, 1, 'inflated month ok' );
28
29 eval { $cd->year(\'year +1'); };
30 ok(!$@, 'updated year using a scalarref');
31 $cd->update();
32 $cd->discard_changes();
33
34 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
35
36 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
37
38 is( $cd->year->month, 1, 'month is still 1' );  
39
40 # get_inflated_column test
41
42 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
43
44 # deflate test
45 my $now = DateTime->now;
46 $cd->year( $now );
47 $cd->update;
48
49 $cd = $schema->resultset("CD")->find(3);
50 is( $cd->year->year, $now->year, 'deflate ok' );
51
52 # set_inflated_column test
53 eval { $cd->set_inflated_column('year', $now) };
54 ok(!$@, 'set_inflated_column with DateTime object');
55 $cd->update;
56
57 $cd = $schema->resultset("CD")->find(3);                 
58 is( $cd->year->year, $now->year, 'deflate ok' );
59
60 $cd = $schema->resultset("CD")->find(3);                 
61 my $before_year = $cd->year->year;
62 eval { $cd->set_inflated_column('year', \'year + 1') };
63 ok(!$@, 'set_inflated_column to "year + 1"');
64 $cd->update;
65
66 TODO: {
67   local $TODO = 'this was left in without a TODO - should it work?';
68
69   eval {
70     $cd->store_inflated_column('year', \'year + 1');
71     is_deeply( $cd->year, \'year + 1', 'deflate ok' );
72   };
73   ok(!$@, 'store_inflated_column to "year + 1"');
74 }
75
76 $cd = $schema->resultset("CD")->find(3);                 
77 is( $cd->year->year, $before_year+1, 'deflate ok' );
78
79 # store_inflated_column test
80 $cd = $schema->resultset("CD")->find(3);                 
81 eval { $cd->store_inflated_column('year', $now) };
82 ok(!$@, 'store_inflated_column with DateTime object');
83 $cd->update;
84
85 is( $cd->year->year, $now->year, 'deflate ok' );
86
87 # update tests
88 $cd = $schema->resultset("CD")->find(3);                 
89 eval { $cd->update({'year' => $now}) };
90 ok(!$@, 'update using DateTime object ok');
91 is($cd->year->year, $now->year, 'deflate ok');
92
93 $cd = $schema->resultset("CD")->find(3);                 
94 $before_year = $cd->year->year;
95 eval { $cd->update({'year' => \'year + 1'}) };
96 ok(!$@, 'update using scalarref ok');
97
98 $cd = $schema->resultset("CD")->find(3);                 
99 is($cd->year->year, $before_year + 1, 'deflate ok');
100
101 # discard_changes test
102 $cd = $schema->resultset("CD")->find(3);                 
103 # inflate the year
104 $before_year = $cd->year->year;
105 $cd->update({ year => \'year + 1'});
106 $cd->discard_changes;
107
108 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
109
110 my $copy = $cd->copy({ year => $now, title => "zemoose" });
111
112 isnt( $copy->year->year, $before_year, "copy" );
113