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