File Coverage

blib/lib/KiokuDB/Test/Fixture/ObjectGraph.pm
Criterion Covered Total %
statement 127 127 100.0
branch n/a
condition n/a
subroutine 15 15 100.0
pod 0 4 0.0
total 142 146 97.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::Test::Fixture::ObjectGraph;
4 4     4   4085 use Moose;
  4         6  
  4         26  
5              
6 4     4   19660 use Test::More;
  4         29  
  4         31  
7 4     4   1053 use Scalar::Util qw(weaken);
  4         12  
  4         216  
8              
9 4     4   756 use KiokuDB::Test::Person;
  4         8  
  4         884  
10              
11             sub p {
12             my @args = @_;
13             unshift @args, "name" if @args % 2;
14             KiokuDB::Test::Person->new(@args);
15             }
16              
17             sub married {
18             my ( $a, $b, @kids ) = @_;
19             $a->so($b);
20             $b->so($a);
21              
22             foreach my $parent ( $a, $b ) {
23             my @kids_copy = @kids;
24             weaken($_) for @kids_copy;
25             $parent->kids(\@kids_copy);
26             }
27              
28             foreach my $child ( @kids ) {
29             my @parents = ( $a, $b );
30             weaken($_) for @parents;
31             $child->parents(\@parents);
32             }
33             }
34              
35             sub clique {
36             my ( @buddies ) = @_;
37              
38             foreach my $member ( @buddies ) {
39             my @rest = grep { $_ != $member } @buddies;
40             $member->friends(\@rest);
41             weaken($_) for @rest;
42             }
43             }
44              
45 4     4   23 use namespace::clean -except => 'meta';
  4         8  
  4         38  
46              
47             with qw(KiokuDB::Test::Fixture) => { excludes => [qw/populate sort/] };
48              
49             has [qw(homer dubya putin)] => (
50             isa => "Str",
51             is => "rw",
52             );
53              
54 22     22 0 34 sub sort { 100 }
55              
56             sub create {
57 35     35 0 106 my $self = shift;
58              
59 35         62 my @r;
60              
61 35         112 push @r, my $bart = p("Bart Simpson");
62 35         126 push @r, my $lisa = p("Lisa Simpson");
63 35         106 push @r, my $maggie = p("Maggie Simpson");
64 35         91 push @r, my $marge = p("Marge Simpson");
65 35         132 push @r, my $homer = p("Homer Simpson");
66 35         95 push @r, my $grandpa = p("Abe Simpson");
67 35         104 push @r, my $mona = p("Mona Simpson");
68 35         81 push @r, my $milhouse = p("Milhouse");
69 35         104 push @r, my $patty = p("Patty Bouvier");
70 35         113 push @r, my $selma = p("Selma Bouvier");
71 35         98 push @r, my $jaquelin = p("Jacqueline Bouvier");
72 35         119 push @r, my $clancy = p("Clancy Bouvier");
73              
74 35         150 married($marge, $homer, $bart, $lisa, $maggie);
75 35         96 married($grandpa, $mona, $homer);
76 35         106 married($jaquelin, $clancy, $marge, $selma, $patty);
77 35         162 clique($bart, $milhouse);
78              
79 35         107 push @r, my $junior = p("Geroge W. Bush");
80 35         119 push @r, my $laura = p("Laura Bush");
81 35         102 push @r, my $the_drunk = p("Jenna Bush");
82 35         103 push @r, my $other_one = p("Barbara Pierce Bush");
83 35         106 push @r, my $daddy = p("George H. W. Bush");
84 35         154 push @r, my $barb = p("Barbara Bush");
85 35         94 push @r, my $jeb = p("Jeb Bush");
86 35         97 push @r, my $dick = p("Dick Cheney");
87 35         111 push @r, my $condie = p("Condoleezza Rice");
88 35         108 push @r, my $putin = p("Vladimir Putin");
89              
90 35         112 married( $junior, $laura, $the_drunk, $other_one );
91 35         98 married( $daddy, $barb, $junior, $jeb );
92 35         124 clique( $junior, $condie, $dick );
93              
94 35         88 push @{ $junior->friends }, $putin;
  35         987  
95              
96 35         169 return ( \@r, $junior, $putin, $homer );
97             }
98              
99             sub populate {
100 34     34 0 134 my $self = shift;
101              
102 34         117 my $s = $self->new_scope;
103              
104 34         143 my ( $r, $junior, $putin, $homer, $retain ) = $self->create;
105              
106 34         222 my @roots = $self->store_ok( $junior, $putin, $homer );
107              
108 34         1612 $self->dubya($roots[0]);
109 34         1125 $self->putin($roots[1]);
110 34         1102 $self->homer($roots[2]);
111             }
112              
113             sub verify {
114 34     34 0 90 my $self = shift;
115              
116 34         193 $self->no_live_objects;
117              
118             $self->txn_lives(sub {
119 34     34   1078 my $junior = $self->lookup_obj_ok( $self->dubya, "KiokuDB::Test::Person" );
120              
121 34         1332 is( $junior->so->name, "Laura Bush", "ref to other object" );
122 34         11934 is( $junior->so->so, $junior, "mututal ref" );
123              
124 68         1690 is_deeply(
125 34         10713 [ map { $_->name } @{ $junior->parents } ],
  34         1310  
126             [ "George H. W. Bush", "Barbara Bush" ],
127             "ref in auxillary structure",
128             );
129              
130 68         260 is_deeply(
131 34         18577 [ grep { $_ == $junior } @{ $junior->parents->[0]->kids } ],
  34         1234  
132             [ $junior ],
133             "mutual ref in auxillary structure"
134             );
135              
136 34         23698 is( $junior->parents->[0]->so, $junior->parents->[1], "mutual refs in nested structure" );
137              
138 34         11622 is_deeply(
139             $junior->kids->[0]->parents,
140             [ $junior, $junior->so ],
141             "mutual refs in nested and non nested structure",
142             );
143              
144 102         2434 is_deeply(
145 34         26115 [ map { $_->name } @{ $junior->friends } ],
  34         1247  
146             [ "Condoleezza Rice", "Dick Cheney", "Vladimir Putin" ],
147             "mutual refs in nested and non nested structure",
148             );
149              
150 34         19875 is_deeply(
151             $junior->friends->[-1]->friends,
152             [],
153             "Putin is paranoid",
154             );
155              
156 34         16863 pop @{ $junior->friends };
  34         1262  
157              
158 34         223 $self->update_ok($junior);
159 34         342 });
160              
161 34         14409 $self->no_live_objects();
162              
163             $self->txn_lives(sub {
164 34     34   1146 my $junior = $self->lookup_obj_ok( $self->dubya, "KiokuDB::Test::Person" );
165              
166 68         1706 is_deeply(
167 34         103 [ map { $_->name } @{ $junior->friends } ],
  34         1326  
168             [ "Condoleezza Rice", "Dick Cheney" ],
169             "Georgia got plastered",
170             );
171              
172 34         900 $self->live_objects_are(
173             $junior,
174             $junior->so,
175 34         936 @{ $junior->friends },
176 34         954 @{ $junior->kids },
177 34         19402 @{ $junior->parents },
178             $junior->parents->[0]->kids->[-1], # jeb
179             );
180              
181 306         479 is(
182 34         58278 scalar(grep { /Putin/ } map { $_->name } $self->live_objects),
  306         7076  
183             0,
184             "Putin is a dead object",
185             );
186              
187 34         12469 $junior->job("Warlord");
188 34         929 $junior->parents->[0]->job("Puppet Master");
189 34         886 $junior->friends->[0]->job("Secretary of State");
190 34         881 $junior->so->job("Prima Donna, Author, Teacher, Librarian");
191              
192 34         198 $self->update_live_objects;
193 34         362 });
194              
195 34         18933 $self->no_live_objects;
196              
197             $self->txn_lives(sub {
198 34     34   1132 my $homer = $self->lookup_obj_ok( $self->homer, "KiokuDB::Test::Person" );
199              
200             {
201 34         91 my $marge = $homer->so;
  34         1281  
202              
203 34         991 $homer->name("Homer J. Simpson");
204              
205 34         925 is( $marge->so->name, "Homer J. Simpson", "inter object rels" );
206             }
207              
208 34         12019 $homer->job("Safety Inspector, Sector 7-G");
209              
210 34         192 $self->update_ok($homer);
211 34         319 });
212              
213 34         13674 $self->no_live_objects;
214              
215             $self->txn_lives(sub {
216 34     34   141 my $s = $self->new_scope;
217              
218 34         1072 my $homer = $self->lookup_obj_ok( $self->homer, "KiokuDB::Test::Person" );
219              
220 34         1280 is( $homer->name, "Homer J. Simpson", "name" );
221 34         337 });
222              
223 34         12979 $self->no_live_objects;
224              
225             $self->txn_lives(sub {
226 34     34   165 my $s = $self->new_scope;
227              
228 34         1223 my $putin = $self->lookup_obj_ok($self->putin);
229              
230 34         190 $self->live_objects_are( $putin );
231              
232 34         25444 foreach my $job ("President", "Prime Minister", "BDFL", "DFL") {
233 136         4988 $putin->job($job);
234 136         491 $self->update_ok($putin);
235             }
236 34         297 });
237              
238 34         15570 $self->no_live_objects;
239              
240             $self->txn_lives(sub {
241 34     34   1130 my $putin = $self->lookup_obj_ok($self->putin);
242              
243 34         1257 is( $putin->job, "DFL", "updated in storage" );
244              
245 34         11350 $self->delete_ok($putin);
246              
247 34         1443 $self->deleted_ok($self->putin);
248              
249 34         12287 is( $self->lookup($self->putin), undef, "lookup no longer returns object" );
250 34         304 });
251              
252 34         11973 $self->no_live_objects;
253              
254 34         1174 $self->deleted_ok( $self->putin );
255             }
256              
257             __PACKAGE__->meta->make_immutable;
258              
259             __PACKAGE__
260              
261             __END__