_cond_for_update_delete is hopelessly broken attempting to introspect SQLA1. Replace...
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
CommitLineData
ff1234ad 1use strict;
2use warnings;
3
4use lib qw(t/lib);
5use Test::More;
6use Test::Exception;
7use DBICTest;
8
9#plan tests => 5;
10plan 'no_plan';
11
12my $schema = DBICTest->init_schema();
13
14my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
15
ff1234ad 16my ($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#]);
28my ($ta, $tb) = $schema->resultset ('TwoKeys')
29 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
30 ->all;
31
32my $tkfk_cnt = $tkfks->count;
33
34my $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]);
40is ($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
47my $fks = $schema->resultset ('FourKeys')
48 ->search ({ map { $_ => [1, 2] } qw/foo bar hello goodbye/}, { join => 'fourkeys_to_twokeys' });
49
50is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
51$fks->update ({ read_count => \ 'read_count + 1' });
52$_->discard_changes for ($fa, $fb);
53
54is ($fa->read_count, 11, 'Update ran only once on joined resultset');
55is ($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
62my $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
72is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
73
74# attempts to delete a grouped rs should fail miserably
75throws_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
af668ad6 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' });
ff1234ad 88
89is_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
af668ad6 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
102is_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
ff1234ad 110$sub_rs->delete;
111
112is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');