Commit | Line | Data |
510ca912 |
1 | use strict; |
2 | use Test::More; |
3 | |
4 | BEGIN { |
289ba852 |
5 | eval "use DBIx::Class::CDBICompat;"; |
6 | if ($@) { |
7 | plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required'); |
8 | next; |
9 | } |
10 | eval "use DBD::SQLite"; |
11 | plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 13); |
510ca912 |
12 | } |
13 | |
14 | use lib 't/testlib'; |
15 | use Film; |
16 | |
17 | sub create_trigger2 { ::ok(1, "Running create trigger 2"); } |
18 | sub delete_trigger { ::ok(1, "Deleting " . shift->Title) } |
19 | |
20 | sub pre_up_trigger { |
21 | $_[0]->_attribute_set(numexplodingsheep => 1); |
22 | ::ok(1, "Running pre-update trigger"); |
23 | } |
24 | sub pst_up_trigger { ::ok(1, "Running post-update trigger"); } |
25 | |
26 | sub default_rating { $_[0]->Rating(15); } |
27 | |
28 | Film->add_trigger( |
29 | before_create => \&default_rating, |
30 | after_create => \&create_trigger2, |
31 | after_delete => \&delete_trigger, |
32 | before_update => \&pre_up_trigger, |
33 | after_update => \&pst_up_trigger, |
34 | ); |
35 | |
36 | ok( |
37 | my $ver = Film->create({ |
38 | title => 'La Double Vie De Veronique', |
39 | director => 'Kryzstof Kieslowski', |
40 | |
41 | # rating => '15', |
42 | numexplodingsheep => 0, |
43 | } |
44 | ), |
45 | "Create Veronique" |
46 | ); |
47 | |
48 | is $ver->Rating, 15, "Default rating"; |
49 | is $ver->NumExplodingSheep, 0, "Original sheep count"; |
50 | ok $ver->Rating('12') && $ver->update, "Change the rating"; |
51 | is $ver->NumExplodingSheep, 1, "Updated object's sheep count"; |
52 | is + ( |
53 | $ver->db_Main->selectall_arrayref( |
54 | 'SELECT numexplodingsheep FROM ' |
55 | . $ver->table |
56 | . ' WHERE ' |
57 | . $ver->primary_column . ' = ' |
58 | . $ver->db_Main->quote($ver->id)) |
59 | )->[0]->[0], 1, "Updated database's sheep count"; |
60 | ok $ver->delete, "Delete"; |
61 | |
62 | { |
63 | Film->add_trigger(before_create => sub { |
64 | my $self = shift; |
65 | ok !$self->_attribute_exists('title'), "PK doesn't auto-vivify"; |
66 | }); |
67 | Film->create({director => "Me"}); |
68 | } |