made cdbi-t optional
[dbsrgits/DBIx-Class-Historic.git] / t / cdbi-t / 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
14use lib 't/testlib';
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 {
21 $_[0]->_attribute_set(numexplodingsheep => 1);
22 ::ok(1, "Running pre-update trigger");
23}
24sub pst_up_trigger { ::ok(1, "Running post-update trigger"); }
25
26sub default_rating { $_[0]->Rating(15); }
27
28Film->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
36ok(
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
48is $ver->Rating, 15, "Default rating";
49is $ver->NumExplodingSheep, 0, "Original sheep count";
50ok $ver->Rating('12') && $ver->update, "Change the rating";
51is $ver->NumExplodingSheep, 1, "Updated object's sheep count";
52is + (
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";
60ok $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}