Make $SIG{__WARN__} overrides more Carp::Always friendly
[dbsrgits/DBIx-Class.git] / t / multi_create / insert_defaults.t
CommitLineData
def17c59 1use strict;
2use warnings;
3
4use Test::More;
def17c59 5use lib qw(t/lib);
6use DBICTest;
052a832c 7use DBIx::Class::_Util 'sigwarn_silencer';
def17c59 8
9my $schema = DBICTest->init_schema();
10
11# Attempt sequential nested find_or_create with autoinc
12# As a side effect re-test nested default create (both the main object and the relation are {})
13my $bookmark_rs = $schema->resultset('Bookmark');
14my $last_bookmark = $bookmark_rs->search ({}, { order_by => { -desc => 'id' }, rows => 1})->single;
15my $last_link = $bookmark_rs->search_related ('link', {}, { order_by => { -desc => 'link.id' }, rows => 1})->single;
16
17# find_or_create a bookmark-link combo with data for a non-existing link
18my $o1 = $bookmark_rs->find_or_create ({ link => { url => 'something-weird' } });
19is ($o1->id, $last_bookmark->id + 1, '1st bookmark ID');
20is ($o1->link->id, $last_link->id + 1, '1st related link ID');
21
22# find_or_create a bookmark-link combo without any data at all (default insert)
23# should extend this test to all available Storage's, and fix them accordingly
24my $o2 = $bookmark_rs->find_or_create ({ link => {} });
25is ($o2->id, $last_bookmark->id + 2, '2nd bookmark ID');
26is ($o2->link->id, $last_link->id + 2, '2nd related link ID');
27
28# make sure the pre-existing link has only one related bookmark
29is ($last_link->bookmarks->count, 1, 'Expecting only 1 bookmark and 1 link, someone mucked with the table!');
30
31# find_or_create a bookmark withouyt any data, but supplying an existing link object
32# should return $last_bookmark
33my $o0 = $bookmark_rs->find_or_create ({ link => $last_link });
34is_deeply ({ $o0->columns}, {$last_bookmark->columns}, 'Correctly identify a row given a relationship');
35
36# inject an additional bookmark and repeat the test
37# should warn and return the first row
38my $o3 = $last_link->create_related ('bookmarks', {});
39is ($o3->id, $last_bookmark->id + 3, '3rd bookmark ID');
40
052a832c 41local $SIG{__WARN__} = sigwarn_silencer( qr/Query returned more than one row/ );
def17c59 42my $oX = $bookmark_rs->find_or_create ({ link => $last_link });
43is_deeply ({ $oX->columns}, {$last_bookmark->columns}, 'Correctly identify a row given a relationship');
052a832c 44
45done_testing;