finalize the serializing example
This commit is contained in:
parent
ea112b2bd5
commit
d758761f05
88
t/example.t
88
t/example.t
@ -64,6 +64,7 @@ $registry->add_type(
|
|||||||
relationships => {
|
relationships => {
|
||||||
'tags' => { type => 'tag' },
|
'tags' => { type => 'tag' },
|
||||||
'comments' => { type => 'comment' },
|
'comments' => { type => 'comment' },
|
||||||
|
photos => { type => 'photo' },
|
||||||
author => {
|
author => {
|
||||||
type => "people",
|
type => "people",
|
||||||
links => sub ( $data, @ ) {
|
links => sub ( $data, @ ) {
|
||||||
@ -80,16 +81,24 @@ $registry->add_type(
|
|||||||
);
|
);
|
||||||
|
|
||||||
$registry->add_type('tag');
|
$registry->add_type('tag');
|
||||||
|
$registry->add_type('photo');
|
||||||
$registry->add_type( 'comment',
|
$registry->add_type( 'comment',
|
||||||
{ id => '_id', allowed_attributes => ['body'] } );
|
{ id => '_id', allowed_attributes => ['body'] } );
|
||||||
$registry->add_type(
|
$registry->add_type(
|
||||||
'people',
|
'people',
|
||||||
{ links => sub ( $data, @ ) { '/peoples/' . $data->{id} }
|
{ links => sub ( $data, @ ) { +{ self => '/peoples/' . $data->{id} } }
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
my $output = $registry->serialize( 'article', $data, { count => 2 } );
|
my $output = $registry->serialize( 'article', $data, { count => 2 } );
|
||||||
|
|
||||||
|
like $output->{data}[0]{relationships}{author},
|
||||||
|
{ links => {
|
||||||
|
"self" => "/articles/1/relationships/author",
|
||||||
|
"related" => "/articles/1/author"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
like $output->{data}[0]{relationships}{author},
|
like $output->{data}[0]{relationships}{author},
|
||||||
{ links => {
|
{ links => {
|
||||||
"self" => "/articles/1/relationships/author",
|
"self" => "/articles/1/relationships/author",
|
||||||
@ -113,6 +122,16 @@ like $output => {
|
|||||||
"created" => "2015-05-22T14:56:29.000Z"
|
"created" => "2015-05-22T14:56:29.000Z"
|
||||||
},
|
},
|
||||||
"relationships" => {
|
"relationships" => {
|
||||||
|
"author" => {
|
||||||
|
"data" => {
|
||||||
|
"type" => "people",
|
||||||
|
"id" => "1"
|
||||||
|
},
|
||||||
|
"links" => {
|
||||||
|
"self" => "/articles/1/relationships/author",
|
||||||
|
"related" => "/articles/1/author"
|
||||||
|
}
|
||||||
|
},
|
||||||
"tags" => {
|
"tags" => {
|
||||||
"data" => [
|
"data" => [
|
||||||
{ "type" => "tag",
|
{ "type" => "tag",
|
||||||
@ -123,9 +142,74 @@ like $output => {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
}
|
"photos" => {
|
||||||
|
"data" => [
|
||||||
|
{ "type" => "photo",
|
||||||
|
"id" => "ed70cf44-9a34-4878-84e6-0c0e4a450cfe"
|
||||||
|
},
|
||||||
|
{ "type" => "photo",
|
||||||
|
"id" => "24ba3666-a593-498c-9f5d-55a4ee08c72e"
|
||||||
|
},
|
||||||
|
{ "type" => "photo",
|
||||||
|
"id" => "f386492d-df61-4573-b4e3-54f6f5d08acf"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"comments" => {
|
||||||
|
"data" => [
|
||||||
|
{ "type" => "comment",
|
||||||
|
"id" => "1"
|
||||||
|
},
|
||||||
|
{ "type" => "comment",
|
||||||
|
"id" => "2"
|
||||||
|
},
|
||||||
|
{ "type" => "comment",
|
||||||
|
"id" => "3"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"links" => { "self" => "/articles/1" }
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
is $output->{included}, bag {
|
||||||
|
item($_)
|
||||||
|
for (
|
||||||
|
{ "type" => "people",
|
||||||
|
"id" => "1",
|
||||||
|
"attributes" => {
|
||||||
|
"firstName" => "Kaley",
|
||||||
|
"lastName" => "Maggio",
|
||||||
|
"email" => "Kaley-Maggio\@example.com",
|
||||||
|
"age" => "80",
|
||||||
|
"gender" => "male"
|
||||||
|
},
|
||||||
|
"links" => { "self" => "/peoples/1" },
|
||||||
|
},
|
||||||
|
{ "type" => "comment",
|
||||||
|
"id" => "1",
|
||||||
|
"attributes" => { "body" => "First !" }
|
||||||
|
},
|
||||||
|
{ "type" => "comment",
|
||||||
|
"id" => "2",
|
||||||
|
"attributes" => { "body" => "I Like !" }
|
||||||
|
},
|
||||||
|
{ "type" => "comment",
|
||||||
|
"id" => "3",
|
||||||
|
"attributes" => { "body" => "Awesome" }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
subtest 'comments only have the body attribute' => sub {
|
||||||
|
for
|
||||||
|
my $comment ( grep { $_->{type} eq 'comment' } $output->{included}->@* )
|
||||||
|
{
|
||||||
|
my @attr = keys $comment->{attributes}->%*;
|
||||||
|
is( \@attr => ['body'], "only the body for comments" );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
done_testing;
|
done_testing;
|
||||||
|
Loading…
Reference in New Issue
Block a user