Avoid ResultSourceProxy calls whenever possible
[dbsrgits/DBIx-Class-Historic.git] / t / delete / related.t
CommitLineData
157fdb0c 1use strict;
2use warnings;
65d35121 3use Test::More;
4
157fdb0c 5use lib qw(t/lib);
6use DBICTest;
7
157fdb0c 8my $schema = DBICTest->init_schema();
9
10my $ars = $schema->resultset('Artist');
11my $cdrs = $schema->resultset('CD');
c2704243 12my $cd2pr_rs = $schema->resultset('CD_to_Producer');
157fdb0c 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]);
c2704243 21
157fdb0c 22$cdrs->populate ([
23 [qw/cdid artist title year/],
ecef7b5e 24 [qw/70 71 delete0 2005/],
25 [qw/71 72 delete1 2005/],
26 [qw/72 72 delete2 2005/],
27 [qw/73 72 delete3 2006/],
28 [qw/74 72 delete4 2007/],
29 [qw/75 73 delete5 2008/],
157fdb0c 30]);
31
c2704243 32my $prod = $schema->resultset('Producer')->create ({ name => 'deleter' });
33my $prod_cd = $cdrs->find (70);
34my $cd2pr = $cd2pr_rs->create ({
35 producer => $prod,
36 cd => $prod_cd,
37});
38
157fdb0c 39my $total_cds = $cdrs->count;
40
41# test that delete_related w/o conditions deletes all related records only
ecef7b5e 42$ars->search ({name => 'a3' })->search_related ('cds')->delete;
157fdb0c 43is ($cdrs->count, $total_cds -= 1, 'related delete ok');
44
ecef7b5e 45my $a2_cds = $ars->search ({ name => 'a2' })->search_related ('cds');
157fdb0c 46
47# test that related deletion w/conditions deletes just the matched related records only
ecef7b5e 48$a2_cds->search ({ year => 2005 })->delete;
157fdb0c 49is ($cdrs->count, $total_cds -= 2, 'related + condition delete ok');
50
51# test that related deletion with limit condition works
ecef7b5e 52$a2_cds->search ({}, { rows => 1})->delete;
157fdb0c 53is ($cdrs->count, $total_cds -= 1, 'related + limit delete ok');
c2704243 54
4ca1fd6f 55{
c2704243 56 local $TODO = 'delete_related is based on search_related which is based on search which does not understand object arguments';
632d1e0f 57 local $SIG{__WARN__} = sub {}; # trap the non-numeric warning, remove when the TODO is removed
58
c2704243 59 my $cd2pr_count = $cd2pr_rs->count;
60 $prod_cd->delete_related('cd_to_producer', { producer => $prod } );
61 is ($cd2pr_rs->count, $cd2pr_count -= 1, 'm2m link deleted succesfully');
65d35121 62
63 # see 187ec69a for why this is neccessary
64 $prod->result_source(undef);
c2704243 65}
65d35121 66
67done_testing;