This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vti
committed
Jan 9, 2015
1 parent
c4637dd
commit 75cf08b
Showing
11 changed files
with
226 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package Notification; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use base 'TestDB'; | ||
|
||
__PACKAGE__->meta( | ||
table => 'notification', | ||
columns => [qw/id reply_id/], | ||
primary_key => 'id', | ||
auto_increment => 'id', | ||
relationships => { | ||
reply => { | ||
type => 'many to one', | ||
class => 'Reply', | ||
map => {reply_id => 'id'} | ||
}, | ||
}, | ||
); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package Reply; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use base 'TestDB'; | ||
|
||
__PACKAGE__->meta( | ||
table => 'reply', | ||
columns => [qw/id author_id thread_id parent_id content/], | ||
primary_key => 'id', | ||
auto_increment => 'id', | ||
relationships => { | ||
thread => { | ||
type => 'many to one', | ||
class => 'Thread', | ||
map => {thread_id => 'id'} | ||
}, | ||
author => { | ||
type => 'many to one', | ||
class => 'Author', | ||
map => {author_id => 'id'} | ||
}, | ||
parent => { | ||
type => 'many to one', | ||
class => 'Reply', | ||
map => {parent_id => 'id'} | ||
}, | ||
}, | ||
); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package Thread; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use base 'TestDB'; | ||
|
||
__PACKAGE__->meta( | ||
table => 'thread', | ||
columns => [qw/id author_id title/], | ||
primary_key => 'id', | ||
auto_increment => 'id', | ||
relationships => { | ||
author => { | ||
type => 'many to one', | ||
class => 'Author', | ||
map => {author_id => 'id'} | ||
} | ||
} | ||
); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use Test::Spec; | ||
use Test::Fatal; | ||
|
||
use lib 't/lib'; | ||
|
||
use TestDBH; | ||
use TestEnv; | ||
use Author; | ||
use Thread; | ||
use Reply; | ||
use Notification; | ||
|
||
describe 'one to many' => sub { | ||
|
||
before each => sub { | ||
TestEnv->prepare_table('author'); | ||
TestEnv->prepare_table('thread'); | ||
TestEnv->prepare_table('reply'); | ||
TestEnv->prepare_table('notification'); | ||
}; | ||
|
||
it '123' => sub { | ||
my $author1 = Author->new(name => 'vti')->create; | ||
my $author2 = Author->new(name => 'bar')->create; | ||
my $thread = | ||
Thread->new(title => 'foo', author_id => $author1->get_column('id')) | ||
->create; | ||
my $reply1 = Reply->new( | ||
content => 'foo', | ||
author_id => $author1->get_column('id'), | ||
thread_id => $thread->get_column('id') | ||
)->create; | ||
my $reply2 = Reply->new( | ||
content => 'foo2', | ||
parent_id => $reply1->get_column('id'), | ||
author_id => $author2->get_column('id'), | ||
thread_id => $thread->get_column('id') | ||
)->create; | ||
|
||
Notification->new(reply_id => $reply1->get_column('id'))->create; | ||
Notification->new(reply_id => $reply2->get_column('id'))->create; | ||
|
||
my @notifications = Notification->find( | ||
with => [ | ||
qw/ | ||
reply.author | ||
reply.thread.author | ||
reply.parent.author | ||
/ | ||
] | ||
); | ||
|
||
is @notifications, 2; | ||
|
||
is $notifications[0]->related('reply')->get_column('content'), 'foo'; | ||
is $notifications[0]->related('reply')->related('author')->get_column('name'), 'vti'; | ||
is $notifications[0]->related('reply')->related('thread')->get_column('title'), 'foo'; | ||
ok !$notifications[0]->related('reply')->related('parent'); | ||
|
||
is $notifications[1]->related('reply')->get_column('content'), 'foo2'; | ||
is $notifications[1]->related('reply')->related('author')->get_column('name'), 'bar'; | ||
is $notifications[1]->related('reply')->related('thread')->get_column('title'), 'foo'; | ||
is $notifications[1]->related('reply')->related('parent')->get_column('content'), 'foo'; | ||
is $notifications[1]->related('reply')->related('parent')->related('author')->get_column('name'), 'vti'; | ||
|
||
#use Data::Dumper; warn Dumper($notifications[1]->to_hash); | ||
}; | ||
|
||
}; | ||
|
||
runtests unless caller; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters