11 my $schema = DBICTest->init_schema();
13 # simple create + parent (the stuff $rs belongs_to)
15 my $cd = $schema->resultset('CD')->create({
23 isa_ok($cd, 'DBICTest::CD', 'Created CD object');
24 isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
25 is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
29 # same as above but the child and parent have no values,
30 # except for an explicit parent pk
32 my $bm_rs = $schema->resultset('Bookmark');
33 my $bookmark = $bm_rs->create({
39 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
40 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
43 { 'link.title' => $bookmark->link->title },
47 'Bookmark and link made it to the DB',
52 # create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
54 my $artist = $schema->resultset('Artist')->create(
57 { title => 'Music to code by',
67 isa_ok($artist, 'DBICTest::Artist', 'Created Artist');
68 is($artist->name, 'Fred 2', 'Artist created correctly');
69 is($artist->cds->count, 1, 'One CD created for artist');
70 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
71 is($artist->cds->first->tags->count, 1, 'One tag created for CD');
72 is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly');
79 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
80 $schema->resultset('Artist')->first->update({
82 { title => 'Yet another CD',
88 qr/Recursive update is not supported over relationships of type multi/,
89 'create via update of multi relationships throws an exception'
92 # create over > 1 levels of might_have (A => { might_have => { B => has_many => C } } )
94 my $artist = $schema->resultset('Artist')->first;
95 my $cd = $schema->resultset('CD')->create ({
97 title => 'Music to code by at night',
102 title => 'Off by one again',
106 title => 'The dereferencer',
110 title => 'Was that a null (Single)',
112 { title => 'The dereferencer', position => 1 },
113 { title => 'The dereferencer II', position => 2 },
127 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
128 is ($cd->title, 'Music to code by at night', 'Correct CD title');
129 is ($cd->tracks->count, 2, 'Two tracks on main CD');
131 my ($t1, $t2) = $cd->tracks->all;
132 is ($t1->title, 'Off by one again', 'Correct 1st track name');
133 is ($t1->cd_single, undef, 'No single for 1st track');
134 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
135 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
137 my $single = $t2->cd_single;
138 is ($single->tracks->count, 2, 'Two tracks on single CD');
139 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
140 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
142 is ($single->cd_to_producer->count, 1, 'One producer created with the single cd');
143 is ($single->cd_to_producer->first->producer->name, 'K&R', 'Producer name correct');
147 # nested find_or_create
149 my $newartist2 = $schema->resultset('Artist')->find_or_create({
158 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
162 # multiple same level has_many create
164 my $artist2 = $schema->resultset('Artist')->create({
168 title => 'Music to code by',
174 title => 'Music to code by',
180 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
184 # first create_related pass
186 my $artist = $schema->resultset('Artist')->first;
188 my $cd_result = $artist->create_related('cds', {
190 title => 'TestOneCD1',
204 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
205 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
207 my $tracks = $cd_result->tracks;
209 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
211 foreach my $track ($tracks->all)
213 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
218 # second create_related with same arguments
220 my $artist = $schema->resultset('Artist')->first;
222 my $cd_result = $artist->create_related('cds', {
224 title => 'TestOneCD2',
236 liner_notes => { notes => 'I can haz liner notes?' },
240 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
241 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
242 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
244 my $tracks = $cd_result->tracks;
246 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
248 foreach my $track ($tracks->all)
250 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
255 # create of parents of a record linker table
257 my $cdp = $schema->resultset('CD_to_Producer')->create({
258 cd => { artist => 1, title => 'foo', year => 2000 },
259 producer => { name => 'jorge' }
261 ok($cdp, 'join table record created ok');
267 my $kurt_cobain = { name => 'Kurt Cobain' };
269 my $in_utero = $schema->resultset('CD')->new({
274 $kurt_cobain->{cds} = [ $in_utero ];
277 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
278 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
280 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
281 is($a->cds && $a->cds->first && $a->cds->first->title,
282 'In Utero', 'CD insertion ok');
288 my $pink_floyd = { name => 'Pink Floyd' };
290 my $the_wall = { title => 'The Wall', year => 1979 };
292 $pink_floyd->{cds} = [ $the_wall ];
295 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
296 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
298 is($a->name, 'Pink Floyd', 'Artist insertion ok');
299 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
303 ## Create foreign key col obj including PK
304 ## See test 20 in 66relationships.t
306 my $new_cd_hashref = {
308 title => 'Boogie Woogie',
310 artist => { artistid => 17, name => 'king luke' }
313 my $cd = $schema->resultset("CD")->find(1);
315 is($cd->artist->id, 1, 'rel okay');
317 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
318 is($new_cd->artist->id, 17, 'new id retained okay');
323 $schema->resultset("CD")->create({
325 title => 'Boogie Wiggle',
327 artist => { artistid => 18, name => 'larry' }
330 is($@, '', 'new cd created without clash on related artist');
332 # Make sure exceptions from errors in created rels propogate
334 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
335 #$t->cd($t->new_related('cd', { artist => undef } ) );
336 #$t->{_rel_in_storage} = 0;
339 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
341 # Test multi create over many_to_many
343 $schema->resultset('CD')->create ({
345 name => 'larry', # should already exist
347 title => 'Warble Marble',
350 { producer => { name => 'Cowboy Neal' } },
354 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
355 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
356 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
357 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
360 # and some insane multicreate
361 # (should work, despite the fact that no one will probably use it this way)
363 # first count how many rows do we initially have
365 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
367 # do the crazy create
369 $schema->resultset('CD')->create ({
373 title => 'Greatest hits 1',
376 name => '"Greatest" collections',
393 title => 'Greatest hits 2',
396 name => '"Greatest" collections',
402 # This cd is created via artist so it doesn't know about producers
404 # if we specify 'bob' here things bomb
405 # as the producer attached to Greatest Hits 1 is
406 # already created, but not yet inserted.
407 # Maybe this can be fixed, but things are hairy
410 #{ producer => { name => 'bob' } },
411 { producer => { name => 'paul' } },
420 title => 'Greatest hits 3',
423 name => '"Greatest" collections',
431 title => 'Greatest hits 4',
434 name => '"Greatest" collections2',
443 title => 'Greatest hits 5',
446 name => '"Greatest" collections2',
455 title => 'Greatest hits 6',
458 name => '"Greatest" collections',
469 name => 'lars', # should already exist
470 # even though the artist 'name' is not uniquely constrained
471 # find_or_create will arguably DWIM
473 title => 'Greatest hits 7',
483 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
484 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
485 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
486 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
487 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
489 my $cd_rs = $schema->resultset ('CD')
490 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
491 is ($cd_rs->count, 7, '7 greatest hits created');
493 my $cds_2012 = $cd_rs->search ({ year => 2012});
494 is ($cds_2012->count, 5, '5 CDs created in 2012');
498 { 'tags.tag' => { -in => [qw/A B/] } },
499 { join => 'tags', group_by => 'me.cdid' }
502 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
505 my $paul_prod = $cd_rs->search (
506 { 'producer.name' => 'paul'},
507 { join => { cd_to_producer => 'producer' } }
509 is ($paul_prod->count, 1, 'Paul had 1 production');
510 my $pauls_cd = $paul_prod->single;
511 is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
513 $pauls_cd->search_related ('cd_to_producer',
514 { 'producer.name' => 'flemming'},
515 { join => 'producer' }
518 'The second producer is flemming',
521 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
522 is ($kirk_cds, 3, 'Kirk had 3 CDs');
525 { 'cd_to_producer.cd' => { '!=', undef } },
526 { join => 'cd_to_producer' },
529 'Kirk had a producer only on one cd',
532 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
533 is ($lars_cds->count, 3, 'Lars had 3 CDs');
536 { 'cd_to_producer.cd' => undef },
537 { join => 'cd_to_producer' },
540 'Lars always had a producer',
543 $lars_cds->search_related ('cd_to_producer',
544 { 'producer.name' => 'flemming'},
545 { join => 'producer' }
548 'Lars produced 1 CD with flemming',
551 $lars_cds->search_related ('cd_to_producer',
552 { 'producer.name' => 'bob'},
553 { join => 'producer' }
556 'Lars produced 2 CDs with bob',
559 my $bob_prod = $cd_rs->search (
560 { 'producer.name' => 'bob'},
561 { join => { cd_to_producer => 'producer' } }
563 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
566 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
568 "Bob produced james' only CD",