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