Commit | Line | Data |
510ca912 |
1 | use strict; |
4a233f30 |
2 | use warnings; |
510ca912 |
3 | use Test::More; |
4 | |
50891152 |
5 | use lib 't/cdbi/testlib'; |
510ca912 |
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 { |
6a3bf251 |
12 | $_[0]->_attribute_set(numexplodingsheep => 1); |
13 | ::ok(1, "Running pre-update trigger"); |
510ca912 |
14 | } |
15 | sub pst_up_trigger { ::ok(1, "Running post-update trigger"); } |
16 | |
17 | sub default_rating { $_[0]->Rating(15); } |
18 | |
1543db24 |
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); |
510ca912 |
24 | |
25 | ok( |
6a3bf251 |
26 | my $ver = Film->create({ |
27 | title => 'La Double Vie De Veronique', |
28 | director => 'Kryzstof Kieslowski', |
510ca912 |
29 | |
6a3bf251 |
30 | # rating => '15', |
31 | numexplodingsheep => 0, |
32 | } |
33 | ), |
34 | "Create Veronique" |
510ca912 |
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 + ( |
6a3bf251 |
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)) |
510ca912 |
48 | )->[0]->[0], 1, "Updated database's sheep count"; |
49 | ok $ver->delete, "Delete"; |
50 | |
51 | { |
8273e845 |
52 | Film->add_trigger(before_create => sub { |
6a3bf251 |
53 | my $self = shift; |
54 | ok !$self->_attribute_exists('title'), "PK doesn't auto-vivify"; |
55 | }); |
56 | Film->create({director => "Me"}); |
510ca912 |
57 | } |
d9bd5195 |
58 | |
59 | done_testing; |