enter xt/ (RT#56698)
[dbsrgits/DBIx-Class.git] / t / cdbi / 11-triggers.t
CommitLineData
510ca912 1use strict;
2use Test::More;
3
4BEGIN {
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 14use lib 't/cdbi/testlib';
510ca912 15use Film;
16
17sub create_trigger2 { ::ok(1, "Running create trigger 2"); }
18sub delete_trigger { ::ok(1, "Deleting " . shift->Title) }
19
20sub pre_up_trigger {
6a3bf251 21 $_[0]->_attribute_set(numexplodingsheep => 1);
22 ::ok(1, "Running pre-update trigger");
510ca912 23}
24sub pst_up_trigger { ::ok(1, "Running post-update trigger"); }
25
26sub default_rating { $_[0]->Rating(15); }
27
1543db24 28Film->add_trigger(before_create => \&default_rating);
29Film->add_trigger(after_create => \&create_trigger2);
30Film->add_trigger(after_delete => \&delete_trigger);
31Film->add_trigger(before_update => \&pre_up_trigger);
32Film->add_trigger(after_update => \&pst_up_trigger);
510ca912 33
34ok(
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
46is $ver->Rating, 15, "Default rating";
47is $ver->NumExplodingSheep, 0, "Original sheep count";
48ok $ver->Rating('12') && $ver->update, "Change the rating";
49is $ver->NumExplodingSheep, 1, "Updated object's sheep count";
50is + (
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";
58ok $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}