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