More reshuffling of the m2m helper code - no functional changes intended
[dbsrgits/DBIx-Class.git] / t / relationship / update_or_create_multi.t
CommitLineData
2284af7e 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
b7743dab 6use Test::Warn;
2284af7e 7use lib qw(t/lib);
8use DBICTest;
2284af7e 9
10my $schema = DBICTest->init_schema();
11
fb88ca2c 12my $artist = $schema->resultset ('Artist')->find(1);
2284af7e 13
14my $genre = $schema->resultset ('Genre')
15 ->create ({ name => 'par excellence' });
b7743dab 16my $genre_cds = $genre->cds;
2284af7e 17
b7743dab 18is ($genre_cds->count, 0, 'No cds yet');
2284af7e 19
20# expect a create
21$genre->update_or_create_related ('cds', {
22 artist => $artist,
23 year => 2009,
24 title => 'the best thing since sliced bread',
25});
26
27# verify cd was inserted ok
b7743dab 28is ($genre_cds->count, 1, 'One cd');
29my $cd = $genre_cds->first;
2284af7e 30is_deeply (
31 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
32 {
33 artist => $artist->id,
34 year => 2009,
35 title => 'the best thing since sliced bread',
36 },
37 'CD created correctly',
38);
39
40# expect a year update on the only related row
b7743dab 41# (non-qunique column + unique column set as disambiguator)
2284af7e 42$genre->update_or_create_related ('cds', {
43 year => 2010,
44 title => 'the best thing since sliced bread',
b7743dab 45 artist => 1,
2284af7e 46});
47
48# re-fetch the cd, verify update
49is ($genre->search_related( 'cds' )->count, 1, 'Still one cd');
b7743dab 50$cd = $genre_cds->first;
2284af7e 51is_deeply (
52 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
53 {
54 artist => $artist->id,
55 year => 2010,
56 title => 'the best thing since sliced bread',
57 },
58 'CD year column updated correctly',
59);
60
b7743dab 61# expect a failing create:
62# the unique constraint is not complete, and there is nothing
63# in the database with such a year yet - insertion will fail due
64# to missing artist fk
65throws_ok {
66 $genre->update_or_create_related ('cds', {
67 year => 2020,
68 title => 'the best thing since sliced bread',
69 })
ed5550d3 70} qr/DBI Exception.+(?x:
71 \QNOT NULL constraint failed: cd.artist\E
72 |
73 \Qcd.artist may not be NULL\E
74)/s, 'ambiguous find + create failed'
75;
2284af7e 76
77# expect a create, after a failed search using *only* the
78# *current* relationship and the unique column constraints
79# (so no year)
49eeb48d 80$schema->is_executed_sql_bind( sub {
81 $genre->update_or_create_related ('cds', {
82 title => 'the best thing since vertical toasters',
83 artist => $artist,
84 year => 2012,
85 });
86}, [
87 [
88 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
89 FROM cd me
90 WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? )
91 ',
92 1,
93 2,
94 "the best thing since vertical toasters",
95 ],
96 [
97 'INSERT INTO cd ( artist, genreid, title, year) VALUES ( ?, ?, ?, ? )',
98 1,
99 2,
100 "the best thing since vertical toasters",
101 2012,
102 ],
103], 'expected select issued' );
2284af7e 104
105# a has_many search without a unique constraint makes no sense
106# but I am not sure what to test for - leaving open
b7743dab 107
108done_testing;