Another heavy refactor of the rel resolver prototype (sequel to 03f6d1f7)
[dbsrgits/DBIx-Class.git] / t / cdbi / 11-triggers.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use lib 't/cdbi/testlib';
6 use Film;
7
8 sub create_trigger2 { ::ok(1, "Running create trigger 2"); }
9 sub delete_trigger  { ::ok(1, "Deleting " . shift->Title) }
10
11 sub pre_up_trigger {
12   $_[0]->_attribute_set(numexplodingsheep => 1);
13   ::ok(1, "Running pre-update trigger");
14 }
15 sub pst_up_trigger { ::ok(1, "Running post-update trigger"); }
16
17 sub default_rating { $_[0]->Rating(15); }
18
19 Film->add_trigger(before_create => \&default_rating);
20 Film->add_trigger(after_create  => \&create_trigger2);
21 Film->add_trigger(after_delete  => \&delete_trigger);
22 Film->add_trigger(before_update => \&pre_up_trigger);
23 Film->add_trigger(after_update  => \&pst_up_trigger);
24
25 ok(
26   my $ver = Film->create({
27       title    => 'La Double Vie De Veronique',
28       director => 'Kryzstof Kieslowski',
29
30       # rating           => '15',
31       numexplodingsheep => 0,
32     }
33   ),
34   "Create Veronique"
35 );
36
37 is $ver->Rating,            15, "Default rating";
38 is $ver->NumExplodingSheep, 0,  "Original sheep count";
39 ok $ver->Rating('12') && $ver->update, "Change the rating";
40 is $ver->NumExplodingSheep, 1, "Updated object's sheep count";
41 is + (
42   $ver->db_Main->selectall_arrayref(
43         'SELECT numexplodingsheep FROM '
44       . $ver->table
45       . ' WHERE '
46       . $ver->primary_column . ' = '
47       . $ver->db_Main->quote($ver->id))
48 )->[0]->[0], 1, "Updated database's sheep count";
49 ok $ver->delete, "Delete";
50
51 {
52   Film->add_trigger(before_create => sub {
53     my $self = shift;
54     ok !$self->_attribute_exists('title'), "PK doesn't auto-vivify";
55   });
56   Film->create({director => "Me"});
57 }
58
59 done_testing;