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