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