All common helpers in one place.
[blog.pm-common-perl-mods.git] / Common-RDBO-Helper / t / get_master_object.t
blob5a798708a94639f6fee411f27d80d2e63da2d62e
1 #! /usr/bin/perl
3 use lib 't/lib';
5 use Test::More 'tests' => 7;
7 use NewDB;
8 use Post;
9 use Post::Manager;
10 use Article;
11 use Article::Manager;
12 use Comment;
13 use Comment::Manager;
14 use DateTime;
16 my $db = NewDB->new();
18 $db->init();
20 my @posts = (
22 addtime => time,
23 title => 'Linux',
24 content => 'blah-blah',
25 comments => [
27 addtime => time,
28 type => 'post',
29 name => 'anon',
30 content => 'hallo'
36 my @articles = (
38 addtime => time,
39 title => 'FreeBSD',
40 content => 'blah-blah',
41 comments => [
43 addtime => time,
44 type => 'article',
45 name => 'bar',
46 content => 'foo'
52 foreach my $post ( @posts ) {
53 my $comments = delete $post->{ comments };
55 my $post = Post->new( %$post );
57 $post->comments( $comments );
59 $post->save( cascade => 1 );
62 foreach my $article ( @articles ) {
63 my $comments = delete $article->{ comments };
65 my $article = Article->new( %$article );
67 $article->comments( $comments );
69 $article->save( cascade => 1 );
72 my $comments = Comment::Manager->get_objects();
74 is( scalar @$comments, 2 );
76 my $article_comments =
77 Comment::Manager->get_objects( query => [ type => 'article' ] );
79 is( scalar @$article_comments, 1 );
81 my $comment = $article_comments->[ 0 ];
82 is( $comment->type, 'article' );
84 my $master = $comment->get_master_object();
85 ok( $master );
87 is( $master->title, 'FreeBSD' );
89 $comment = Comment->new(master_id => 1, type => 'article');
90 $comment->load();
91 is($comment->type, 'article');
92 is($comment->content, 'foo');
94 my $articles = Article::Manager->get_objects();
95 $_->delete( cascade => 1 ) foreach @$articles;
97 my $posts = Post::Manager->get_objects();
98 $_->delete( cascade => 1 ) foreach @$posts;