1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
11 my $schema = DBICTest->init_schema();
13 # Test various new() invocations - this is all about backcompat, making
14 # sure that insert() still works as expected by legacy code.
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 supplied to new() to get inserted, as well as any
21 # relations whose PK's are necessary to complete the objects supplied
22 # to new(). All other objects should be insert()able afterwards too.
26 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
27 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982});
30 $new_related_cd->insert;
31 } 'Staged insertion successful';
32 ok($new_artist->in_storage, 'artist inserted');
33 ok($new_related_cd->in_storage, 'new_related_cd inserted');
37 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Mode Depeche' });
38 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave Slightly Noisily', 'year' => 1982});
40 $new_related_cd->insert;
41 } '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);
53 } 'CD insertion survives by inserting artist';
54 ok($new_cd->in_storage, 'new_related_cd inserted');
55 ok($new_artist->in_storage, 'artist inserted');
57 my $retrieved_cd = $schema->resultset('CD')->find ({ 'title' => 'Leave Loudly While Singing Off Key'});
58 ok ($retrieved_cd, 'CD found in db');
59 is ($retrieved_cd->artist->name, 'Depeche Mode 2: Insertion Boogaloo', 'Correct artist attached to cd');
63 my $new_cd = $schema->resultset('CD')->new ({ 'title' => 'Leave screaming Off Key in the nude', 'year' => 1982});
64 my $new_related_artist = $new_cd->new_related( artist => { 'name' => 'Depeche Mode 3: Insertion Boogaloo' });
66 $new_related_artist->insert;
68 } 'CD insertion survives after inserting artist';
69 ok($new_cd->in_storage, 'cd inserted');
70 ok($new_related_artist->in_storage, 'artist inserted');
72 my $retrieved_cd = $schema->resultset('CD')->find ({ 'title' => 'Leave screaming Off Key in the nude'});
73 ok ($retrieved_cd, 'CD found in db');
74 is ($retrieved_cd->artist->name, 'Depeche Mode 3: Insertion Boogaloo', 'Correct artist attached to cd');
77 # test both sides of a 1:(1|0)
79 for my $reldir ('might_have', 'belongs_to') {
80 my $artist = $schema->resultset('Artist')->find(1);
82 my $new_track = $schema->resultset('Track')->new ({
83 title => "$reldir: First track of latest cd",
85 title => "$reldir: Latest cd",
91 my $new_single = $schema->resultset('CD')->new ({
93 title => "$reldir: Awesome first single",
97 if ($reldir eq 'might_have') {
98 $new_track->cd_single ($new_single);
102 $new_single->single_track ($new_track);
106 ok ($new_single->in_storage, "$reldir single inserted");
107 ok ($new_track->in_storage, "$reldir track inserted");
109 my $new_cds = $artist->search_related ('cds',
111 { prefetch => 'tracks', order_by => 'cdid' }
115 [$new_cds->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator'})->all ],
121 single_track => undef,
122 title => "$reldir: Latest cd",
126 last_updated_at => undef,
127 last_updated_on => undef,
129 title => "$reldir: First track of latest cd",
140 title => "$reldir: Awesome first single",
145 'Expected rows created in database',
148 $new_cds->delete_all;
153 my $new_cd = $schema->resultset("CD")->new_result({});
154 my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
157 $new_related_artist->insert;
158 $new_cd->title( 'Misplaced Childhood' );
159 $new_cd->year ( 1985 );
160 $new_cd->artist( $new_related_artist ); # For exact backward compatibility
163 'Reversed staged insertion successful'
165 ok($new_related_artist->in_storage, 'related artist inserted');
166 ok($new_cd->in_storage, 'cd inserted');