Massively refactor and sanify condition collapsing
[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;
9use DBIC::SqlMakerTest;
10
11my $schema = DBICTest->init_schema();
a2287768 12my $sdebug = $schema->storage->debug;
2284af7e 13
fb88ca2c 14my $artist = $schema->resultset ('Artist')->find(1);
2284af7e 15
16my $genre = $schema->resultset ('Genre')
17 ->create ({ name => 'par excellence' });
b7743dab 18my $genre_cds = $genre->cds;
2284af7e 19
b7743dab 20is ($genre_cds->count, 0, 'No cds yet');
2284af7e 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
b7743dab 30is ($genre_cds->count, 1, 'One cd');
31my $cd = $genre_cds->first;
2284af7e 32is_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
b7743dab 43# (non-qunique column + unique column set as disambiguator)
2284af7e 44$genre->update_or_create_related ('cds', {
45 year => 2010,
46 title => 'the best thing since sliced bread',
b7743dab 47 artist => 1,
2284af7e 48});
49
50# re-fetch the cd, verify update
51is ($genre->search_related( 'cds' )->count, 1, 'Still one cd');
b7743dab 52$cd = $genre_cds->first;
2284af7e 53is_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
b7743dab 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
67throws_ok {
68 $genre->update_or_create_related ('cds', {
69 year => 2020,
70 title => 'the best thing since sliced bread',
71 })
ed5550d3 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;
2284af7e 78
79# expect a create, after a failed search using *only* the
80# *current* relationship and the unique column constraints
81# (so no year)
82my @sql;
83$schema->storage->debugcb(sub { push @sql, $_[1] });
84$schema->storage->debug (1);
85
86$genre->update_or_create_related ('cds', {
87 title => 'the best thing since vertical toasters',
88 artist => $artist,
89 year => 2012,
90});
91
92$schema->storage->debugcb(undef);
a2287768 93$schema->storage->debug ($sdebug);
2284af7e 94
0be2023f 95my ($search_sql) = $sql[0] =~ /^(SELECT .+?)\:/;
2284af7e 96is_same_sql (
0be2023f 97 $search_sql,
2284af7e 98 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
8273e845 99 FROM cd me
8d005ad9 100 WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? )
2284af7e 101 ',
102 'expected select issued',
103);
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;