more dt docs reorg
[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
82$sub_rs->search ({}, { group_by => [ reverse $sub_rs->result_source->primary_columns ] }) # reverse to make sure the comaprison works
83 ->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
84
85is_deeply (
86 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
87 ->get_column ('pilot_sequence')->all
88 ],
89 [qw/11 21 30 40/],
90 'Only two rows incremented',
91);
92
93$sub_rs->delete;
94
95is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');