Merge 'trunk' into 'sqla_1.50_compat'
[dbsrgits/DBIx-Class-Historic.git] / t / cdbi-t / 23-cascade.t
CommitLineData
37702ea5 1use strict;
2use Test::More;
dec1bfe0 3use Data::Dumper;
37702ea5 4
5BEGIN {
deff792e 6 eval "use DBIx::Class::CDBICompat;";
7 if ($@) {
8 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
9 next;
10 }
11 eval "use DBD::SQLite";
12 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 12);
37702ea5 13}
14
deff792e 15INIT {
16 use lib 't/testlib';
17 use Film;
18 use Director;
37702ea5 19}
deff792e 20
21{ # Cascade on delete
22 Director->has_many(nasties => 'Film');
23
24 my $dir = Director->insert({
25 name => "Lewis Teague",
26 });
27 my $kk = $dir->add_to_nasties({
28 Title => 'Alligator'
29 });
30 is $kk->director, $dir, "Director set OK";
31 is $dir->nasties, 1, "We have one nasty";
32
33 ok $dir->delete;
34 ok !Film->retrieve("Alligator"), "has_many cascade deletes by default";
35}
36
37
38# Two ways of saying not to cascade
39for my $args ({ no_cascade_delete => 1 }, { cascade => "None" }) {
40 Director->has_many(nasties => 'Film', $args);
41
42 my $dir = Director->insert({
43 name => "Lewis Teague",
44 });
45 my $kk = $dir->add_to_nasties({
46 Title => 'Alligator'
47 });
48 is $kk->director, $dir, "Director set OK";
49 is $dir->nasties, 1, "We have one nasty";
50
51 ok $dir->delete;
dec1bfe0 52 local $Data::Dumper::Terse = 1;
53 ok +Film->retrieve("Alligator"), 'has_many with ' . Dumper ($args);;
deff792e 54 $kk->delete;
55}
56
57
58#{ # Fail on cascade
59# local $TODO = 'cascade => "Fail" unimplemented';
60#
61# Director->has_many(nasties => Film => { cascade => 'Fail' });
62#
63# my $dir = Director->insert({ name => "Nasty Noddy" });
64# my $kk = $dir->add_to_nasties({ Title => 'Killer Killers' });
65# is $kk->director, $dir, "Director set OK";
66# is $dir->nasties, 1, "We have one nasty";
67#
68# ok !eval { $dir->delete };
69# like $@, qr/1/, "Can't delete while films exist";
70#
71# my $rr = $dir->add_to_nasties({ Title => 'Revenge of the Revengers' });
72# ok !eval { $dir->delete };
73# like $@, qr/2/, "Still can't delete";
74#
75# $dir->nasties->delete_all;
76# ok eval { $dir->delete };
77# is $@, '', "Can delete once films are gone";
78#}