my
$schema
= DBICTest->init_schema();
my
$artist
=
$schema
->resultset (
'Artist'
)->find(1);
my
$genre
=
$schema
->resultset (
'Genre'
)
->create ({
name
=>
'par excellence'
});
my
$genre_cds
=
$genre
->cds;
is (
$genre_cds
->count, 0,
'No cds yet'
);
$genre
->update_or_create_related (
'cds'
, {
artist
=>
$artist
,
year
=> 2009,
title
=>
'the best thing since sliced bread'
,
});
is (
$genre_cds
->count, 1,
'One cd'
);
my
$cd
=
$genre_cds
->first;
is_deeply (
{
map
{
$_
,
$cd
->get_column (
$_
) }
qw/artist year title/
},
{
artist
=>
$artist
->id,
year
=> 2009,
title
=>
'the best thing since sliced bread'
,
},
'CD created correctly'
,
);
$genre
->update_or_create_related (
'cds'
, {
year
=> 2010,
title
=>
'the best thing since sliced bread'
,
artist
=> 1,
});
is (
$genre
->search_related(
'cds'
)->count, 1,
'Still one cd'
);
$cd
=
$genre_cds
->first;
is_deeply (
{
map
{
$_
,
$cd
->get_column (
$_
) }
qw/artist year title/
},
{
artist
=>
$artist
->id,
year
=> 2010,
title
=>
'the best thing since sliced bread'
,
},
'CD year column updated correctly'
,
);
throws_ok {
$genre
->update_or_create_related (
'cds'
, {
year
=> 2020,
title
=>
'the best thing since sliced bread'
,
})
}
qr/DBI Exception.+(?x:
\QNOT NULL constraint failed: cd.artist\E
|
\Qcd.artist may not be NULL\E
)/
s,
'ambiguous find + create failed'
;
$schema
->is_executed_sql_bind(
sub
{
$genre
->update_or_create_related (
'cds'
, {
title
=>
'the best thing since vertical toasters'
,
artist
=>
$artist
,
year
=> 2012,
});
}, [
[
'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
FROM cd me
WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? )
',
1,
2,
"the best thing since vertical toasters"
,
],
[
'INSERT INTO cd ( artist, genreid, title, year) VALUES ( ?, ?, ?, ? )'
,
1,
2,
"the best thing since vertical toasters"
,
2012,
],
],
'expected select issued'
);
done_testing;