Finally implement compound OptDep group augmentation
[dbsrgits/DBIx-Class.git] / t / icdt / core.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( test_rdbms_sqlite icdt );
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my $schema = DBICTest->init_schema();
12
13 $schema->class('CD') ->inflate_column( 'year',
14     { inflate => sub { DateTime->new( year => shift ) },
15       deflate => sub { shift->year } }
16 );
17
18 my $rs = $schema->resultset('CD');
19
20 # inflation test
21 my $cd = $rs->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 lives_ok (
30   sub { $cd->year(\'year +1') },
31   'updated year using a scalarref'
32 );
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 = $rs->find(3);
52 is( $cd->year->year, $now->year, 'deflate ok' );
53
54 # set_inflated_column test
55 lives_ok (
56   sub { $cd->set_inflated_column('year', $now) },
57   'set_inflated_column with DateTime object'
58 );
59 $cd->update;
60
61 $cd = $rs->find(3);
62 is( $cd->year->year, $now->year, 'deflate ok' );
63
64 $cd = $rs->find(3);
65 my $before_year = $cd->year->year;
66 lives_ok (
67   sub { $cd->set_inflated_column('year', \'year + 1') },
68   'set_inflated_column to "year + 1"',
69 );
70 $cd->update;
71
72 $cd->store_inflated_column('year', \'year + 1');
73 is_deeply( $cd->year, \'year + 1', 'scalarref deflate passthrough ok' );
74
75 $cd = $rs->find(3);
76 is( $cd->year->year, $before_year+1, 'deflate ok' );
77
78 # store_inflated_column test
79 $cd = $rs->find(3);
80 lives_ok (
81   sub { $cd->store_inflated_column('year', $now) },
82   'store_inflated_column with DateTime object'
83 );
84 $cd->update;
85
86 is( $cd->year->year, $now->year, 'deflate ok' );
87
88 # update tests
89 $cd = $rs->find(3);
90 lives_ok (
91   sub { $cd->update({'year' => $now}) },
92   'update using DateTime object ok'
93 );
94 is($cd->year->year, $now->year, 'deflate ok');
95
96 $cd = $rs->find(3);
97 $before_year = $cd->year->year;
98 lives_ok (
99   sub { $cd->update({'year' => \'year + 1'}) },
100   'update using scalarref ok'
101 );
102
103 $cd = $rs->find(3);
104 is($cd->year->year, $before_year + 1, 'deflate ok');
105
106 # discard_changes test
107 $cd = $rs->find(3);
108 # inflate the year
109 $before_year = $cd->year->year;
110 $cd->update({ year => \'year + 1'});
111 $cd->discard_changes;
112
113 is($cd->year->year, $before_year + 1, 'discard_changes clears the inflated value');
114
115 my $copy = $cd->copy({ year => $now, title => "zemoose" });
116
117 is( $copy->year->year, $now->year, "copy" );
118
119
120
121 my $artist = $cd->artist;
122 my $sval = \ '2012';
123
124 $cd = $rs->create ({
125         artist => $artist,
126         year => $sval,
127         title => 'create with scalarref',
128 });
129
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');
133
134 $cd->discard_changes;
135 is ($cd->year->year, 2012, 'infation upon reload');
136
137 $cd2->discard_changes;
138 is ($cd2->year->year, 2012, 'infation upon reload of copy');
139
140
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');
144
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');
148
149 my $sval2 = \ '2013';
150
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');
153
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');
157
158 done_testing;