9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
13 my $artist = $schema->resultset ('Artist')->find(1);
15 my $genre = $schema->resultset ('Genre')
16 ->create ({ name => 'par excellence' });
17 my $genre_cds = $genre->cds;
19 is ($genre_cds->count, 0, 'No cds yet');
22 $genre->update_or_create_related ('cds', {
25 title => 'the best thing since sliced bread',
28 # verify cd was inserted ok
29 is ($genre_cds->count, 1, 'One cd');
30 my $cd = $genre_cds->first;
32 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
34 artist => $artist->id,
36 title => 'the best thing since sliced bread',
38 'CD created correctly',
41 # expect a year update on the only related row
42 # (non-qunique column + unique column set as disambiguator)
43 $genre->update_or_create_related ('cds', {
45 title => 'the best thing since sliced bread',
49 # re-fetch the cd, verify update
50 is ($genre->search_related( 'cds' )->count, 1, 'Still one cd');
51 $cd = $genre_cds->first;
53 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
55 artist => $artist->id,
57 title => 'the best thing since sliced bread',
59 'CD year column updated correctly',
62 # expect a failing create:
63 # the unique constraint is not complete, and there is nothing
64 # in the database with such a year yet - insertion will fail due
65 # to missing artist fk
67 $genre->update_or_create_related ('cds', {
69 title => 'the best thing since sliced bread',
71 } qr/DBI Exception.+(?x:
72 \QNOT NULL constraint failed: cd.artist\E
74 \Qcd.artist may not be NULL\E
75 )/s, 'ambiguous find + create failed'
78 # expect a create, after a failed search using *only* the
79 # *current* relationship and the unique column constraints
81 $schema->is_executed_sql_bind( sub {
82 $genre->update_or_create_related ('cds', {
83 title => 'the best thing since vertical toasters',
89 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
91 WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? )
95 "the best thing since vertical toasters",
98 'INSERT INTO cd ( artist, genreid, title, year) VALUES ( ?, ?, ?, ? )',
101 "the best thing since vertical toasters",
104 ], 'expected select issued' );
106 # a has_many search without a unique constraint makes no sense
107 # but I am not sure what to test for - leaving open