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