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 | |
50891152 |
14 | use lib 't/cdbi/testlib'; |
510ca912 |
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 { |
6a3bf251 |
21 | $_[0]->_attribute_set(numexplodingsheep => 1); |
22 | ::ok(1, "Running pre-update trigger"); |
510ca912 |
23 | } |
24 | sub pst_up_trigger { ::ok(1, "Running post-update trigger"); } |
25 | |
26 | sub default_rating { $_[0]->Rating(15); } |
27 | |
1543db24 |
28 | Film->add_trigger(before_create => \&default_rating); |
29 | Film->add_trigger(after_create => \&create_trigger2); |
30 | Film->add_trigger(after_delete => \&delete_trigger); |
31 | Film->add_trigger(before_update => \&pre_up_trigger); |
32 | Film->add_trigger(after_update => \&pst_up_trigger); |
510ca912 |
33 | |
34 | ok( |
6a3bf251 |
35 | my $ver = Film->create({ |
36 | title => 'La Double Vie De Veronique', |
37 | director => 'Kryzstof Kieslowski', |
510ca912 |
38 | |
6a3bf251 |
39 | # rating => '15', |
40 | numexplodingsheep => 0, |
41 | } |
42 | ), |
43 | "Create Veronique" |
510ca912 |
44 | ); |
45 | |
46 | is $ver->Rating, 15, "Default rating"; |
47 | is $ver->NumExplodingSheep, 0, "Original sheep count"; |
48 | ok $ver->Rating('12') && $ver->update, "Change the rating"; |
49 | is $ver->NumExplodingSheep, 1, "Updated object's sheep count"; |
50 | is + ( |
6a3bf251 |
51 | $ver->db_Main->selectall_arrayref( |
52 | 'SELECT numexplodingsheep FROM ' |
53 | . $ver->table |
54 | . ' WHERE ' |
55 | . $ver->primary_column . ' = ' |
56 | . $ver->db_Main->quote($ver->id)) |
510ca912 |
57 | )->[0]->[0], 1, "Updated database's sheep count"; |
58 | ok $ver->delete, "Delete"; |
59 | |
60 | { |
6a3bf251 |
61 | Film->add_trigger(before_create => sub { |
62 | my $self = shift; |
63 | ok !$self->_attribute_exists('title'), "PK doesn't auto-vivify"; |
64 | }); |
65 | Film->create({director => "Me"}); |
510ca912 |
66 | } |