9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
12 my $sdebug = $schema->storage->debug;
14 my $artist = $schema->resultset ('Artist')->find(1);
16 my $genre = $schema->resultset ('Genre')
17 ->create ({ name => 'par excellence' });
18 my $genre_cds = $genre->cds;
20 is ($genre_cds->count, 0, 'No cds yet');
23 $genre->update_or_create_related ('cds', {
26 title => 'the best thing since sliced bread',
29 # verify cd was inserted ok
30 is ($genre_cds->count, 1, 'One cd');
31 my $cd = $genre_cds->first;
33 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
35 artist => $artist->id,
37 title => 'the best thing since sliced bread',
39 'CD created correctly',
42 # expect a year update on the only related row
43 # (non-qunique column + unique column set as disambiguator)
44 $genre->update_or_create_related ('cds', {
46 title => 'the best thing since sliced bread',
50 # re-fetch the cd, verify update
51 is ($genre->search_related( 'cds' )->count, 1, 'Still one cd');
52 $cd = $genre_cds->first;
54 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
56 artist => $artist->id,
58 title => 'the best thing since sliced bread',
60 'CD year column updated correctly',
63 # expect a failing create:
64 # the unique constraint is not complete, and there is nothing
65 # in the database with such a year yet - insertion will fail due
66 # to missing artist fk
68 $genre->update_or_create_related ('cds', {
70 title => 'the best thing since sliced bread',
72 } qr/\Qcd.artist may not be NULL/, 'ambiguous find + create failed';
74 # expect a create, after a failed search using *only* the
75 # *current* relationship and the unique column constraints
78 $schema->storage->debugcb(sub { push @sql, $_[1] });
79 $schema->storage->debug (1);
81 $genre->update_or_create_related ('cds', {
82 title => 'the best thing since vertical toasters',
87 $schema->storage->debugcb(undef);
88 $schema->storage->debug ($sdebug);
90 my ($search_sql) = $sql[0] =~ /^(SELECT .+?)\:/;
93 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
95 WHERE ( me.artist = ? AND me.title = ? AND me.genreid = ? )
97 'expected select issued',
100 # a has_many search without a unique constraint makes no sense
101 # but I am not sure what to test for - leaving open