93 lines
2.3 KiB
Perl
93 lines
2.3 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->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' } )->{data}{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->{data}{id} => '12',
|
|
'custom id, function'
|
|
);
|
|
|
|
like $serialized->{data}, { 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};
|
|
}
|
|
}
|
|
)->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->{data} => {
|
|
type => 'thing',
|
|
id => 1,
|
|
attributes => {
|
|
foo => 'bar',
|
|
}
|
|
};
|
|
|
|
};
|
|
|
|
subtest 'a single scalar == id', sub {
|
|
my $serialized =
|
|
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
|
|
->serialize('blah');
|
|
|
|
is $serialized->{data} => {
|
|
type => 'thing',
|
|
id => 'blah',
|
|
};
|
|
};
|
|
|
|
subtest 'allowed_attributes', sub {
|
|
my $serialized = Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
|
type => 'thing',
|
|
allowed_attributes => ['foo'],
|
|
)->serialize( { id => 1, foo => 2, bar => 3 } );
|
|
|
|
is $serialized->{data} => {
|
|
type => 'thing',
|
|
id => 1,
|
|
attributes => { foo => 2, } };
|
|
};
|
|
|
|
done_testing();
|