Dancer2-Plugin-JsonApi/t/relationships.t

67 lines
1.5 KiB
Perl
Raw Normal View History

2023-11-01 17:16:34 +00:00
use Test2::V0;
use Dancer2::Plugin::JsonApi::Registry;
use experimental qw/ signatures /;
my $registry = Dancer2::Plugin::JsonApi::Registry->new;
$registry->add_type( 'thing',
{ relationships => { subthings => { type => 'subthing' }, } } );
$registry->add_type('subthing');
subtest basic => sub {
my $s = $registry->serialize(
'thing',
{ id => 1,
2023-11-13 14:57:37 +00:00
subthings => [ { id => 2, x => 10 }, { id => 3 } ]
}
);
2023-11-01 17:16:34 +00:00
ok not $s->{data}{attributes};
2023-11-13 15:00:55 +00:00
like $s => {
data => {
2023-11-01 17:16:34 +00:00
id => 1,
type => 'thing',
relationships =>
{ subthings => { data => [ { id => 2 }, { id => 3 } ] } }
},
included =>
2023-11-13 14:57:37 +00:00
[ { type => 'subthing', id => 2, attributes => { x => 10 } }, ]
2023-11-13 15:00:55 +00:00
};
2023-11-01 17:16:34 +00:00
};
subtest "don't repeat includes" => sub {
my $s = $registry->serialize(
2023-11-13 14:57:37 +00:00
'thing',
[
{ id => 1,
2023-11-13 15:00:55 +00:00
subthings => [
{ id => 2,
x => 10
},
{ id => 3,
y => 20
}
]
2023-11-13 14:57:37 +00:00
},
{ id => 2,
2023-11-13 15:00:55 +00:00
subthings => [
{ id => 3,
y => 20
},
{ id => 2,
x => 10
}
]
2023-11-13 14:57:37 +00:00
}
]
);
is $s->{included}->@* + 0, 2;
2023-11-01 17:16:34 +00:00
};
done_testing;