Add a new testfile to check deferred multicreate
[dbsrgits/DBIx-Class.git] / t / 96multi_create_new.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 plan 'no_plan';
10
11 my $schema = DBICTest->init_schema();
12
13 # Test various new() invocations - this is all about backcompat, making 
14 # sure that insert() still works as expected by legacy code.
15 #
16 # What we essentially do is multi-instantiate objects, making sure nothing
17 # gets inserted. Then we add some more objects to the mix either via
18 # new_related() or by setting an accessor directly (or both) - again
19 # expecting no inserts. Then after calling insert() on the starter object
20 # we expect everything to get inserted _except_ the externally set
21 # objects - those should be insert()able afterwards
22
23
24 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
25 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982});
26 eval {
27        $new_artist->insert;
28        $new_related_cd->insert;
29 };
30 is ($@, '', 'Staged insertion successful');
31 ok($new_artist->in_storage, 'artist inserted');
32 ok($new_related_cd->in_storage, 'new_related_cd inserted');
33
34
35 my $new_cd = $schema->resultset("CD")->new_result({});
36 my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
37 lives_ok (
38     sub {
39        $new_related_artist->insert;
40        $new_cd->title( 'Misplaced Childhood' );
41        $new_cd->year ( 1985 );
42        $new_cd->artist( $new_related_artist );  # For exact backward compatibility
43        $new_cd->insert;
44     },
45     'Reversed staged insertion successful'
46 );
47 ok($new_related_artist->in_storage, 'related artist inserted');
48 ok($new_cd->in_storage, 'cd inserted');