fix test bug, also: fuck you, multi create
[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 my $schema = DBICTest->init_schema();
9
10 eval { require DateTime };
11 plan skip_all => "Need DateTime for inflation tests" if $@;
12
13 plan tests => 21;
14
15 $schema->class('CD')
16 #DBICTest::Schema::CD
17 ->inflate_column( 'year',
18     { inflate => sub { DateTime->new( year => shift ) },
19       deflate => sub { shift->year } }
20 );
21 Class::C3->reinitialize;
22
23 # inflation test
24 my $cd = $schema->resultset("CD")->find(3);
25
26 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
27
28 is( $cd->year->year, 1997, 'inflated year ok' );
29
30 is( $cd->year->month, 1, 'inflated month ok' );
31
32 eval { $cd->year(\'year +1'); };
33 ok(!$@, 'updated year using a scalarref');
34 $cd->update();
35 $cd->discard_changes();
36
37 is( ref($cd->year), 'DateTime', 'year is still a DateTime, ok' );
38
39 is( $cd->year->year, 1998, 'updated year, bypassing inflation' );
40
41 is( $cd->year->month, 1, 'month is still 1' );  
42
43 # get_inflated_column test
44
45 is( ref($cd->get_inflated_column('year')), 'DateTime', 'get_inflated_column produces a DateTime');
46
47 # deflate test
48 my $now = DateTime->now;
49 $cd->year( $now );
50 $cd->update;
51
52 $cd = $schema->resultset("CD")->find(3);
53 is( $cd->year->year, $now->year, 'deflate ok' );
54
55 # set_inflated_column test
56 eval { $cd->set_inflated_column('year', $now) };
57 ok(!$@, 'set_inflated_column with DateTime object');
58 $cd->update;
59
60 $cd = $schema->resultset("CD")->find(3);                 
61 is( $cd->year->year, $now->year, 'deflate ok' );
62
63 $cd = $schema->resultset("CD")->find(3);                 
64 my $before_year = $cd->year->year;
65 eval { $cd->set_inflated_column('year', \'year + 1') };
66 ok(!$@, 'set_inflated_column to "year + 1"');
67 $cd->update;
68
69 $cd = $schema->resultset("CD")->find(3);                 
70 is( $cd->year->year, $before_year+1, 'deflate ok' );
71
72 # store_inflated_column test
73 $cd = $schema->resultset("CD")->find(3);                 
74 eval { $cd->store_inflated_column('year', $now) };
75 ok(!$@, 'store_inflated_column with DateTime object');
76 $cd->update;
77
78 is( $cd->year->year, $now->year, 'deflate ok' );
79
80 # update tests
81 $cd = $schema->resultset("CD")->find(3);                 
82 eval { $cd->update({'year' => $now}) };
83 ok(!$@, 'update using DateTime object ok');
84 is($cd->year->year, $now->year, 'deflate ok');
85
86 $cd = $schema->resultset("CD")->find(3);                 
87 $before_year = $cd->year->year;
88 eval { $cd->update({'year' => \'year + 1'}) };
89 ok(!$@, 'update using scalarref ok');
90
91 $cd = $schema->resultset("CD")->find(3);                 
92 is($cd->year->year, $before_year + 1, 'deflate ok');
93
94 # discard_changes test
95 $cd = $schema->resultset("CD")->find(3);                 
96 # inflate the year
97 $before_year = $cd->year->year;
98 $cd->update({ year => \'year + 1'});
99 $cd->discard_changes;
100
101 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
102
103 my $copy = $cd->copy({ year => $now, title => "zemoose" });
104
105 isnt( $copy->year->year, $before_year, "copy" );
106  
107 # eval { $cd->store_inflated_column('year', \'year + 1') };
108 # print STDERR "ERROR: $@" if($@);
109 # ok(!$@, 'store_inflated_column to "year + 1"');
110
111 # is_deeply( $cd->year, \'year + 1', 'deflate ok' );
112