12 my $schema = DBICTest->init_schema();
14 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
16 my ($fa, $fb) = $tkfks->related_resultset ('fourkeys')->populate ([
17 [qw/foo bar hello goodbye sensors read_count/],
22 # This is already provided by DBICTest
23 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
28 my ($ta, $tb) = $schema->resultset ('TwoKeys')
29 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
32 my $tkfk_cnt = $tkfks->count;
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 },
40 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
43 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
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' });
50 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
51 $fks->update ({ read_count => \ 'read_count + 1' });
52 $_->discard_changes for ($fa, $fb);
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');
59 # Make sure multicolumn in or the equivalen functions correctly
62 my $sub_rs = $tkfks->search (
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/ },
68 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
72 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
74 # attempts to delete a grouped rs should fail miserably
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',
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' });
86 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
87 ->get_column ('pilot_sequence')->all
90 'Only two rows incremented',
95 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');