72 lines
1.6 KiB
Perl
72 lines
1.6 KiB
Perl
use Test2::V0;
|
|
|
|
use Dancer2::Plugin::JsonApi::Registry::Schema;
|
|
|
|
use experimental qw/ signatures /;
|
|
|
|
my $type =
|
|
Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' );
|
|
|
|
like $type->top_level_serialize( { attr1 => 'a', id => '123' },
|
|
{ foo => 1 } ) => {
|
|
jsonapi => { version => '1.0' },
|
|
data => { type => 'thing', id => '123' } };
|
|
|
|
is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
|
'type' => 'thing',
|
|
id => 'foo'
|
|
)->serialize( { foo => '123' } )->{id} => '123',
|
|
'custom id'
|
|
);
|
|
|
|
my $serialized = schema_serialize(
|
|
{ 'type' => 'thing',
|
|
id => sub ($data) { $data->{x} . $data->{y} },
|
|
links => { self => '/some/url' },
|
|
},
|
|
{ x => '1', y => '2' } );
|
|
|
|
is( $serialized->{id} => '12',
|
|
'custom id, function'
|
|
);
|
|
|
|
like $serialized, { links => { self => '/some/url' } }, "links";
|
|
|
|
sub schema_serialize ( $schema, $data ) {
|
|
return Dancer2::Plugin::JsonApi::Registry::Schema->new(%$schema)
|
|
->serialize($data);
|
|
}
|
|
|
|
like(
|
|
Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
|
type => 'thing',
|
|
top_level_meta => {
|
|
foo => 1,
|
|
bar => sub ( $data, $xtra ) {
|
|
$xtra->{bar};
|
|
}
|
|
}
|
|
)->top_level_serialize( {}, { bar => 'yup' } ),
|
|
{ meta => { foo => 1, bar => 'yup' } } );
|
|
|
|
|
|
subtest 'attributes' => sub {
|
|
my $serialized =
|
|
Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
|
type => 'thing',
|
|
)->serialize( { id => 1, foo => 'bar'});
|
|
|
|
is $serialized => {
|
|
type => 'thing',
|
|
id => 1,
|
|
attributes => {
|
|
foo => 'bar',
|
|
}
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
done_testing();
|