Debian Codegraph @

t/cdbi-t-ocache/06-hasa.t

raw
1 use strict;
2 use Test::More;
3 use Class::DBI::Sweet;
4 Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 });
5 Class::DBI::Sweet->cache(Cache::MemoryCache->new(
6 { namespace => "SweetTest", default_expires_in => 60 } ) );
7
8 BEGIN {
9 eval "use Cache::MemoryCache";
10 plan skip_all => "needs Cache::Cache for testing" if $@;
11 eval "use DBD::SQLite";
12 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 24);
13 }
14
15 @YA::Film::ISA = 'Film';
16
17 local $SIG{__WARN__} = sub { };
18
19 INIT {
20 use lib 't/cdbi-t/testlib';
21 use Film;
22 use Director;
23 }
24
25 Film->create_test_film;
26 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
27 ok(my $pj = $btaste->Director, "Bad taste hasa() director");
28 ok(!ref($pj), ' ... which is not an object');
29
30 ok(Film->has_a('Director' => 'Director'), "Link Director table");
31 ok(
32 Director->insert(
33 {
34 Name => 'Peter Jackson',
35 Birthday => -300000000,
36 IsInsane => 1
37 }
38 ),
39 'insert Director'
40 );
41
42 $btaste = Film->retrieve('Bad Taste');
43
44 ok($pj = $btaste->Director, "Bad taste now hasa() director");
45 isa_ok($pj => 'Director');
46 is($pj->id, 'Peter Jackson', ' ... and is the correct director');
47
48 # Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
49 my $sj = Director->insert(
50 {
51 Name => 'Skippy Jackson',
52 Birthday => (-300000000 + 60),
53 IsInsane => 1,
54 }
55 );
56
57 is($sj->id, 'Skippy Jackson', 'We have a new director');
58
59 Film->has_a(CoDirector => 'Director');
60
61 $btaste->CoDirector($sj);
62 $btaste->update;
63 is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
64 is(
65 $btaste->Director->Name,
66 'Peter Jackson',
67 "Didnt interfere with each other"
68 );
69
70 { # Ensure search can take an object
71 my @films = Film->search(Director => $pj);
72 is @films, 1, "1 Film directed by $pj";
73 is $films[0]->id, "Bad Taste", "Bad Taste";
74 }
75
76 inheriting_hasa();
77
78 {
79
80 # Skippy directs a film and Peter helps!
81 $sj = Director->retrieve('Skippy Jackson');
82 $pj = Director->retrieve('Peter Jackson');
83
84 fail_with_bad_object($sj, $btaste);
85 taste_bad($sj, $pj);
86 }
87
88 sub inheriting_hasa {
89 my $btaste = YA::Film->retrieve('Bad Taste');
90 is(ref($btaste->Director), 'Director', 'inheriting hasa()');
91 is(ref($btaste->CoDirector), 'Director', 'inheriting hasa()');
92 is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
93 }
94
95 sub taste_bad {
96 my ($dir, $codir) = @_;
97 my $tastes_bad = YA::Film->insert(
98 {
99 Title => 'Tastes Bad',
100 Director => $dir,
101 CoDirector => $codir,
102 Rating => 'R',
103 NumExplodingSheep => 23
104 }
105 );
106 is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
107 is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
108 is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
109 is(
110 $tastes_bad->_CoDirector_accessor,
111 'Peter Jackson',
112 'CoDirector_accessor'
113 );
114 }
115
116 sub fail_with_bad_object {
117 my ($dir, $codir) = @_;
118 eval {
119 YA::Film->insert(
120 {
121 Title => 'Tastes Bad',
122 Director => $dir,
123 CoDirector => $codir,
124 Rating => 'R',
125 NumExplodingSheep => 23
126 }
127 );
128 };
129 ok $@, $@;
130 }
131
132 package Foo;
133 use base 'CDBase';
134 __PACKAGE__->table('foo');
135 __PACKAGE__->columns('All' => qw/ id fav /);
136 # fav is a film
137 __PACKAGE__->db_Main->do( qq{
138 CREATE TABLE foo (
139 id INTEGER,
140 fav VARCHAR(255)
141 )
142 });
143
144
145 package Bar;
146 use base 'CDBase';
147 __PACKAGE__->table('bar');
148 __PACKAGE__->columns('All' => qw/ id fav /);
149 # fav is a foo
150 __PACKAGE__->db_Main->do( qq{
151 CREATE TABLE bar (
152 id INTEGER,
153 fav INTEGER
154 )
155 });
156
157 package main;
158 Foo->has_a("fav" => "Film");
159 Bar->has_a("fav" => "Foo");
160 my $foo = Foo->insert({ id => 6, fav => 'Bad Taste' });
161 my $bar = Bar->insert({ id => 2, fav => 6 });
162 isa_ok($bar->fav, "Foo");
163 isa_ok($foo->fav, "Film");
164
165 {
166 my $foo;
167 Foo->add_trigger(after_create => sub { $foo = shift->fav });
168 my $gwh = Foo->insert({ id => 93, fav => 'Good Will Hunting' });
169 isa_ok $foo, "Film", "Object in after_create trigger";
170 }
171