9 my $schema = DBICTest->init_schema();
11 # Test various new() invocations - this is all about backcompat, making
12 # sure that insert() still works as expected by legacy code.
14 # What we essentially do is multi-instantiate objects, making sure nothing
15 # gets inserted. Then we add some more objects to the mix either via
16 # new_related() or by setting an accessor directly (or both) - again
17 # expecting no inserts. Then after calling insert() on the starter object
18 # we expect everything supplied to new() to get inserted, as well as any
19 # relations whose PK's are necessary to complete the objects supplied
20 # to new(). All other objects should be insert()able afterwards too.
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});
28 $new_related_cd->insert;
30 is ($@, '', 'Staged insertion successful');
31 ok($new_artist->in_storage, 'artist inserted');
32 ok($new_related_cd->in_storage, 'new_related_cd inserted');
36 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
37 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave Slightly Noisily', 'year' => 1982});
39 $new_related_cd->insert;
41 is ($@, '', 'CD insertion survives by finding artist');
42 ok($new_artist->in_storage, 'artist inserted');
43 ok($new_related_cd->in_storage, 'new_related_cd inserted');
47 my $new_cd = $schema->resultset('CD')->new ({ 'title' => 'Leave Loudly While Singing Off Key', 'year' => 1982});
48 my $new_artist = $schema->resultset("Artist")->new ({ 'name' => 'Depeche Mode 2: Insertion Boogaloo' });
49 $new_cd->artist ($new_artist);
54 is ($@, '', 'CD insertion survives by inserting artist');
55 ok($new_cd->in_storage, 'new_related_cd inserted');
56 ok($new_artist->in_storage, 'artist inserted');
58 my $retrieved_cd = $schema->resultset('CD')->find ({ 'title' => 'Leave Loudly While Singing Off Key'});
59 ok ($retrieved_cd, 'CD found in db');
60 is ($retrieved_cd->artist->name, 'Depeche Mode 2: Insertion Boogaloo', 'Correct artist attached to cd');
64 my $new_cd = $schema->resultset("CD")->new_result({});
65 my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
68 $new_related_artist->insert;
69 $new_cd->title( 'Misplaced Childhood' );
70 $new_cd->year ( 1985 );
71 $new_cd->artist( $new_related_artist ); # For exact backward compatibility
74 'Reversed staged insertion successful'
76 ok($new_related_artist->in_storage, 'related artist inserted');
77 ok($new_cd->in_storage, 'cd inserted');