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