05d245bd9c1bcc4a99ef057889983e1b9d14aa6a
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
1 use strict;
2 use warnings;
3
4 use lib qw(t/lib);
5 use Test::More;
6 use Test::Exception;
7 use DBICTest;
8
9 #plan tests => 5;
10 plan 'no_plan';
11
12 my $schema = DBICTest->init_schema();
13
14 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
15
16 my ($fa, $fb) = $tkfks->related_resultset ('fourkeys')->populate ([
17   [qw/foo bar hello goodbye sensors read_count/],
18   [qw/1   1   1     1       a       10         /],
19   [qw/2   2   2     2       b       20         /],
20 ]);
21
22 # This is already provided by DBICTest
23 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
24 #  [qw/artist  cd /],
25 #  [qw/1       1  /],
26 #  [qw/2       2  /],
27 #]);
28 my ($ta, $tb) = $schema->resultset ('TwoKeys')
29                   ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
30                     ->all;
31
32 my $tkfk_cnt = $tkfks->count;
33
34 my $non_void_ctx = $tkfks->populate ([
35   { autopilot => 'a', fourkeys =>  $fa, twokeys => $ta, pilot_sequence => 10 },
36   { autopilot => 'b', fourkeys =>  $fb, twokeys => $tb, pilot_sequence => 20 },
37   { autopilot => 'x', fourkeys =>  $fa, twokeys => $tb, pilot_sequence => 30 },
38   { autopilot => 'y', fourkeys =>  $fb, twokeys => $ta, pilot_sequence => 40 },
39 ]);
40 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
41
42 #
43 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
44 #
45
46 # create a resultset matching $fa and $fb only
47 my $fks = $schema->resultset ('FourKeys')
48                   ->search ({ map { $_ => [1, 2] } qw/foo bar hello goodbye/}, { join => 'fourkeys_to_twokeys' });
49
50 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
51 $fks->update ({ read_count => \ 'read_count + 1' });
52 $_->discard_changes for ($fa, $fb);
53
54 is ($fa->read_count, 11, 'Update ran only once on joined resultset');
55 is ($fb->read_count, 21, 'Update ran only once on joined resultset');
56
57
58 #
59 # Make sure multicolumn in or the equivalen functions correctly
60 #
61
62 my $sub_rs = $tkfks->search (
63   [
64     { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
65     { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
66   ],
67   {
68     join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
69   },
70 );
71
72 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
73
74 # attempts to delete a grouped rs should fail miserably
75 throws_ok (
76   sub { $sub_rs->search ({}, { distinct => 1 })->delete },
77   qr/attempted a delete operation on a resultset which does group_by/,
78   'Grouped rs update/delete not allowed',
79 );
80
81 # grouping on PKs only should pass
82 $sub_rs->search (
83   {},
84   {
85     group_by => [ reverse $sub_rs->result_source->primary_columns ],     # reverse to make sure the PK-list comaprison works
86   },
87 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
88
89 is_deeply (
90   [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
91             ->get_column ('pilot_sequence')->all 
92   ],
93   [qw/11 21 30 40/],
94   'Only two rows incremented',
95 );
96
97 # also make sure weird scalarref usage works (RT#51409)
98 $tkfks->search (
99   \ 'pilot_sequence BETWEEN 11 AND 21',
100 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
101
102 is_deeply (
103   [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
104             ->get_column ('pilot_sequence')->all 
105   ],
106   [qw/12 22 30 40/],
107   'Only two rows incremented (where => scalarref works)',
108 );
109
110 $sub_rs->delete;
111
112 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');