Add failing multikey rs delete (and by implication update) test
[dbsrgits/DBIx-Class.git] / t / delete / related.t
CommitLineData
157fdb0c 1use Test::More;
2use strict;
3use warnings;
4use lib qw(t/lib);
5use DBICTest;
6
9e6af55c 7plan tests => 6;
157fdb0c 8
9my $schema = DBICTest->init_schema();
10
11my $ars = $schema->resultset('Artist');
12my $cdrs = $schema->resultset('CD');
13
14# create some custom entries
ecef7b5e 15$ars->populate ([
16 [qw/artistid name/],
17 [qw/71 a1/],
18 [qw/72 a2/],
19 [qw/73 a3/],
20]);
157fdb0c 21$cdrs->populate ([
22 [qw/cdid artist title year/],
ecef7b5e 23 [qw/70 71 delete0 2005/],
24 [qw/71 72 delete1 2005/],
25 [qw/72 72 delete2 2005/],
26 [qw/73 72 delete3 2006/],
27 [qw/74 72 delete4 2007/],
28 [qw/75 73 delete5 2008/],
157fdb0c 29]);
30
31my $total_cds = $cdrs->count;
32
33# test that delete_related w/o conditions deletes all related records only
ecef7b5e 34$ars->search ({name => 'a3' })->search_related ('cds')->delete;
157fdb0c 35is ($cdrs->count, $total_cds -= 1, 'related delete ok');
36
ecef7b5e 37my $a2_cds = $ars->search ({ name => 'a2' })->search_related ('cds');
157fdb0c 38
39# test that related deletion w/conditions deletes just the matched related records only
ecef7b5e 40$a2_cds->search ({ year => 2005 })->delete;
157fdb0c 41is ($cdrs->count, $total_cds -= 2, 'related + condition delete ok');
42
43# test that related deletion with limit condition works
ecef7b5e 44$a2_cds->search ({}, { rows => 1})->delete;
157fdb0c 45is ($cdrs->count, $total_cds -= 1, 'related + limit delete ok');
9e6af55c 46
47my $tkfk = $schema->resultset('FourKeys_to_TwoKeys');
48
49my ($fa, $fb) = $tkfk->related_resultset ('fourkeys')->populate ([
50 [qw/foo bar hello goodbye sensors/],
51 [qw/1 1 1 1 a /],
52 [qw/2 2 2 2 b /],
53]);
54
55# This is already provided by DBICTest
56#my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
57# [qw/artist cd /],
58# [qw/1 1 /],
59# [qw/2 2 /],
60#]);
61my ($ta, $tb) = $schema->resultset ('TwoKeys')
62 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
63 ->all;
64
65my $tkfk_cnt = $tkfk->count;
66
67my $non_void_ctx = $tkfk->populate ([
68 { autopilot => 'a', fourkeys => $fa, twokeys => $ta },
69 { autopilot => 'b', fourkeys => $fb, twokeys => $tb },
70 { autopilot => 'x', fourkeys => $fa, twokeys => $tb },
71 { autopilot => 'y', fourkeys => $fb, twokeys => $ta },
72]);
73is ($tkfk->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
74
75my $sub_rs = $tkfk->search (
76 [
77 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
78 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
79 ],
80 {
81 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
82 },
83);
84
85is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
86$sub_rs->delete;
87
88is ($tkfk->count, $tkfk_cnt -= 2, 'Only two rows deleted');