Dancer2-Plugin-JsonApi/t/schema.t

146 lines
3.7 KiB
Perl
Raw Normal View History

2023-10-31 19:41:10 +00:00
use Test2::V0;
2023-11-13 18:38:56 +00:00
use Dancer2::Plugin::JsonApi::Schema;
2023-11-13 15:38:51 +00:00
use Dancer2::Plugin::JsonApi::Registry;
2023-10-31 19:41:10 +00:00
use experimental qw/ signatures /;
2023-11-14 15:24:09 +00:00
my $type = Dancer2::Plugin::JsonApi::Schema->new( 'type' => 'thing' );
2023-10-31 19:41:10 +00:00
2023-11-13 15:31:24 +00:00
like $type->serialize( { attr1 => 'a', id => '123' }, { foo => 1 } ) => {
2023-10-31 19:41:10 +00:00
jsonapi => { version => '1.0' },
2023-11-13 15:31:24 +00:00
data => { type => 'thing', id => '123' }
};
2023-10-31 19:41:10 +00:00
2023-11-13 18:38:56 +00:00
is( Dancer2::Plugin::JsonApi::Schema->new(
2023-10-31 19:41:10 +00:00
'type' => 'thing',
id => 'foo'
2023-11-13 15:31:24 +00:00
)->serialize( { foo => '123' } )->{data}{id} => '123',
2023-10-31 19:41:10 +00:00
'custom id'
2023-11-13 15:31:24 +00:00
);
2023-10-31 19:41:10 +00:00
my $serialized = schema_serialize(
{ 'type' => 'thing',
2023-11-13 18:12:48 +00:00
id => sub ( $data, @ ) { $data->{x} . $data->{y} },
2023-10-31 19:41:10 +00:00
links => { self => '/some/url' },
},
2023-11-13 15:31:24 +00:00
{ x => '1', y => '2' }
);
2023-10-31 19:41:10 +00:00
is( $serialized->{data}{id} => '12',
2023-10-31 19:41:10 +00:00
'custom id, function'
2023-11-13 15:31:24 +00:00
);
2023-10-31 19:41:10 +00:00
like $serialized->{data}, { links => { self => '/some/url' } }, "links";
2023-10-31 19:41:10 +00:00
sub schema_serialize ( $schema, $data ) {
2023-11-14 15:24:09 +00:00
return Dancer2::Plugin::JsonApi::Schema->new(%$schema)->serialize($data);
2023-10-31 19:41:10 +00:00
}
2023-10-31 20:09:05 +00:00
like(
2023-11-13 18:38:56 +00:00
Dancer2::Plugin::JsonApi::Schema->new(
2023-11-13 15:31:24 +00:00
type => 'thing',
2023-10-31 20:09:05 +00:00
top_level_meta => {
foo => 1,
bar => sub ( $data, $xtra ) {
$xtra->{bar};
}
}
2023-11-13 15:31:24 +00:00
)->serialize( {}, { bar => 'yup' } ),
{ meta => { foo => 1, bar => 'yup' } }
);
2023-10-31 20:45:42 +00:00
subtest 'attributes' => sub {
2023-11-13 15:31:24 +00:00
my $serialized =
2023-11-13 18:38:56 +00:00
Dancer2::Plugin::JsonApi::Schema->new( type => 'thing', )
2023-11-13 15:31:24 +00:00
->serialize( { id => 1, foo => 'bar' } );
2023-10-31 20:45:42 +00:00
is $serialized->{data} => {
2023-11-13 15:31:24 +00:00
type => 'thing',
id => 1,
attributes => { foo => 'bar', }
2023-10-31 20:45:42 +00:00
};
};
2023-11-01 19:21:06 +00:00
subtest 'a single scalar == id', sub {
my $serialized =
2023-11-13 18:38:56 +00:00
Dancer2::Plugin::JsonApi::Schema->new( type => 'thing' )
2023-11-13 15:31:24 +00:00
->serialize('blah');
2023-11-01 19:21:06 +00:00
is $serialized->{data} => {
2023-11-13 15:31:24 +00:00
type => 'thing',
id => 'blah',
2023-11-01 19:21:06 +00:00
};
};
2023-10-31 20:45:42 +00:00
2023-11-01 20:03:28 +00:00
subtest 'allowed_attributes', sub {
2023-11-13 18:38:56 +00:00
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
2023-11-01 20:03:28 +00:00
type => 'thing',
allowed_attributes => ['foo'],
)->serialize( { id => 1, foo => 2, bar => 3 } );
is $serialized->{data} => {
type => 'thing',
id => 1,
2023-11-13 15:31:24 +00:00
attributes => { foo => 2, }
};
};
subtest 'empty data', sub {
my $serialized =
2023-11-13 18:38:56 +00:00
Dancer2::Plugin::JsonApi::Schema->new( type => 'thing' )
2023-11-13 15:31:24 +00:00
->serialize(undef);
ok( !$serialized->{data}, "there is no data" );
2023-11-01 20:03:28 +00:00
};
2023-10-31 20:45:42 +00:00
2023-11-13 15:38:51 +00:00
package FakeRequest {
use Moo;
has path => ( is => 'ro', default => '/some/path' );
}
package FakeApp {
use Moo;
has request => (
is => 'ro',
default => sub {
FakeRequest->new;
}
);
}
subtest "add the self link if tied to the app" => sub {
2023-11-13 18:38:56 +00:00
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
2023-11-13 15:38:51 +00:00
type => 'thing',
registry =>
Dancer2::Plugin::JsonApi::Registry->new( app => FakeApp->new )
)->serialize(undef);
is $serialized->{links}{self} => '/some/path';
2023-11-13 17:29:37 +00:00
};
2023-11-13 15:38:51 +00:00
2023-11-13 17:29:37 +00:00
subtest 'attributes function' => sub {
2023-11-13 18:38:56 +00:00
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
2023-11-13 17:29:37 +00:00
type => 'thing',
attributes => sub ( $data, @ ) {
return +{ reverse %$data },;
},
)->serialize( { id => 1, 'a' .. 'd' } );
is $serialized->{data}{attributes} => { 1 => 'id', b => 'a', d => 'c' };
2023-11-13 15:38:51 +00:00
};
2023-11-13 17:29:37 +00:00
2023-11-14 15:24:09 +00:00
subtest 'before_serializer' => sub {
my $serialized = Dancer2::Plugin::JsonApi::Schema->new(
type => 'thing',
before_serialize => sub ( $data, @ ) {
return +{ %$data, nbr_attrs => scalar keys %$data };
},
)->serialize( { id => 1, a => 'b' } );
is $serialized->{data}{attributes}{nbr_attrs} => 2;
};
2023-10-31 19:41:10 +00:00
done_testing();