\Q-uote column/alias names in regexes in _resolve_aliastypes_from_select_args
[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
10 my $schema = DBICTest->init_schema();
11
12 my $artist = $schema->resultset ('Artist')->find(1);
13
14 my $genre = $schema->resultset ('Genre')
15             ->create ({ name => 'par excellence' });
16 my $genre_cds = $genre->cds;
17
18 is ($genre_cds->count, 0, 'No cds yet');
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
28 is ($genre_cds->count, 1, 'One cd');
29 my $cd = $genre_cds->first;
30 is_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
41 # (non-qunique column + unique column set as disambiguator)
42 $genre->update_or_create_related ('cds', {
43   year => 2010,
44   title => 'the best thing since sliced bread',
45   artist => 1,
46 });
47
48 # re-fetch the cd, verify update
49 is ($genre->search_related( 'cds' )->count, 1, 'Still one cd');
50 $cd = $genre_cds->first;
51 is_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
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
65 throws_ok {
66   $genre->update_or_create_related ('cds', {
67     year => 2020,
68     title => 'the best thing since sliced bread',
69   })
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 ;
76
77 # expect a create, after a failed search using *only* the
78 # *current* relationship and the unique column constraints
79 # (so no year)
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' );
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
107
108 done_testing;