use lib qw(t/lib);
use DBICTest;
-plan tests => 70;
+plan tests => 93;
my $schema = DBICTest->init_schema();
-# simple create + parent (the stuff $rs belongs_to)
-eval {
+lives_ok ( sub {
my $cd = $schema->resultset('CD')->create({
artist => {
name => 'Fred Bloggs'
isa_ok($cd, 'DBICTest::CD', 'Created CD object');
isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
-};
-diag $@ if $@;
+}, 'simple create + parent (the stuff $rs belongs_to) ok');
-# same as above but the child and parent have no values,
-# except for an explicit parent pk
-eval {
+lives_ok ( sub {
my $bm_rs = $schema->resultset('Bookmark');
my $bookmark = $bm_rs->create({
link => {
1,
'Bookmark and link made it to the DB',
);
-};
-diag $@ if $@;
+}, 'simple create where the child and parent have no values, except for an explicit parent pk ok');
-# create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
-eval {
+lives_ok ( sub {
my $artist = $schema->resultset('Artist')->first;
my $cd = $artist->create_related (cds => {
title => 'Music to code by',
is($cd->tags->count, 1, 'One tag created for CD');
is($cd->tags->first->tag, 'rock', 'Tag created correctly');
-};
-diag $@ if $@;
+}, 'create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )');
throws_ok (
sub {
'create via update of multi relationships throws an exception'
);
-# Create m2m while originating in the linker table
-eval {
+lives_ok ( sub {
my $artist = $schema->resultset('Artist')->first;
my $c2p = $schema->resultset('CD_to_Producer')->create ({
cd => {
title => 'Bad investment',
year => 2008,
tracks => [
- { position => 1, title => 'Just buy' },
- { position => 2, title => 'Why did we do it' },
- { position => 3, title => 'Burn baby burn' },
+ { title => 'Just buy' },
+ { title => 'Why did we do it' },
+ { title => 'Burn baby burn' },
],
},
producer => {
my $cd = $prod->cds->first;
is ($cd->title, 'Bad investment', 'CD created correctly');
is ($cd->tracks->count, 3, 'CD has 3 tracks');
+}, 'Create m2m while originating in the linker table');
-};
-diag $@ if $@;
-# create over > 1 levels of might_have (A => { might_have => { B => has_many => C } } )
-eval {
+#CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
+# \
+# \-> has_many \
+# --> CD2Producer
+# /-> has_many /
+# /
+# Producer
+lives_ok ( sub {
my $artist = $schema->resultset('Artist')->first;
my $cd = $schema->resultset('CD')->create ({
artist => $artist,
year => 2008,
tracks => [
{
- position => 1,
title => 'Off by one again',
},
{
- position => 2,
title => 'The dereferencer',
cd_single => {
artist => $artist,
year => 2008,
title => 'Was that a null (Single)',
tracks => [
- { title => 'The dereferencer', position => 1 },
- { title => 'The dereferencer II', position => 2 },
+ { title => 'The dereferencer' },
+ { title => 'The dereferencer II' },
],
cd_to_producer => [
{
producer => {
name => 'K&R',
}
- }
+ },
+ {
+ producer => {
+ name => 'Don Knuth',
+ }
+ },
]
},
},
is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
- is ($single->cd_to_producer->count, 1, 'One producer created with the single cd');
- is ($single->cd_to_producer->first->producer->name, 'K&R', 'Producer name correct');
-};
-diag $@ if $@;
+ is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
+ is_deeply (
+ [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
+ ['Don Knuth', 'K&R'],
+ 'Producers named correctly',
+ );
+}, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at a has_many level');
+
+#Track -> might have -> Single -> has_many -> Tracks
+# \
+# \-> has_many \
+# --> CD2Producer
+# /-> has_many /
+# /
+# Producer
+lives_ok ( sub {
+ my $cd = $schema->resultset('CD')->first;
+ my $track = $schema->resultset('Track')->create ({
+ cd => $cd,
+ title => 'Multicreate rocks',
+ cd_single => {
+ artist => $cd->artist,
+ year => 2008,
+ title => 'Disemboweling MultiCreate',
+ tracks => [
+ { title => 'Why does mst write this way' },
+ { title => 'Chainsaw celebration' },
+ { title => 'Purl cleans up' },
+ ],
+ cd_to_producer => [
+ {
+ producer => {
+ name => 'mst',
+ }
+ },
+ {
+ producer => {
+ name => 'castaway',
+ }
+ },
+ {
+ producer => {
+ name => 'theorbtwo',
+ }
+ },
+ ]
+ },
+ });
+
+ isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
+ is ($track->title, 'Multicreate rocks', 'Correct Track title');
-TODO: {
-local $TODO = "Todoify for multicreate branch";
-# test might_have again but with a PK == FK in the middle (obviously not specified)
-eval {
+ my $single = $track->cd_single;
+ isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
+ is ($single->tracks->count, 3, '3 tracks on single CD');
+ is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
+ is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
+ is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
+
+ is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
+ is_deeply (
+ [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
+ ['castaway', 'mst', 'theorbtwo'],
+ 'Producers named correctly',
+ );
+}, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at the might_have directly');
+
+lives_ok ( sub {
my $artist = $schema->resultset('Artist')->first;
my $cd = $schema->resultset('CD')->create ({
artist => $artist,
'Images named correctly in objects',
);
-
my $artwork = $schema->resultset('Artwork')->search (
{ 'cd.title' => 'Music to code by at twilight' },
{ join => 'cd' },
[ 'recursive descent', 'tail packing' ],
'Images named correctly after search',
);
-};
-diag $@ if $@;
+}, 'Test might_have again but with a PK == FK in the middle (obviously not specified)');
-# test might_have again but with just a PK and FK (neither specified) in the mid-table
-eval {
+lives_ok ( sub {
my $cd = $schema->resultset('CD')->first;
my $track = $schema->resultset ('Track')->create ({
cd => $cd,
- position => 66,
title => 'Black',
lyrics => {
lyric_versions => [
[ 'The color black', 'The colour black' ],
'Lyrics text via search matches',
);
-};
-diag $@ if $@;
-}
+}, 'Test might_have again but with just a PK and FK (neither specified) in the mid-table');
-# nested find_or_create
-eval {
+lives_ok ( sub {
my $newartist2 = $schema->resultset('Artist')->find_or_create({
name => 'Fred 3',
cds => [
],
});
is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
-};
-diag $@ if $@;
+}, 'Nested find_or_create');
-# multiple same level has_many create
-eval {
+lives_ok ( sub {
my $artist2 = $schema->resultset('Artist')->create({
name => 'Fred 4',
cds => [
});
is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
-};
-diag $@ if $@;
+}, 'Multiple same level has_many create');
-# first create_related pass
-eval {
+lives_ok ( sub {
my $artist = $schema->resultset('Artist')->first;
my $cd_result = $artist->create_related('cds', {
title => 'TestOneCD1',
year => 2007,
tracks => [
-
- { position=>111,
- title => 'TrackOne',
- },
- { position=>112,
- title => 'TrackTwo',
- }
+ { title => 'TrackOne' },
+ { title => 'TrackTwo' },
],
});
{
ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
}
-};
-diag $@ if $@;
+}, 'First create_related pass');
-# second create_related with same arguments
-eval {
+lives_ok ( sub {
my $artist = $schema->resultset('Artist')->first;
my $cd_result = $artist->create_related('cds', {
title => 'TestOneCD2',
year => 2007,
tracks => [
-
- { position=>111,
- title => 'TrackOne',
- },
- { position=>112,
- title => 'TrackTwo',
- }
+ { title => 'TrackOne' },
+ { title => 'TrackTwo' },
],
liner_notes => { notes => 'I can haz liner notes?' },
{
ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
}
-};
-diag $@ if $@;
+}, 'second create_related with same arguments');
-# create of parents of a record linker table
-eval {
+lives_ok ( sub {
my $cdp = $schema->resultset('CD_to_Producer')->create({
cd => { artist => 1, title => 'foo', year => 2000 },
producer => { name => 'jorge' }
});
ok($cdp, 'join table record created ok');
-};
-diag $@ if $@;
+}, 'create of parents of a record linker table');
-#SPECIAL_CASE
-eval {
+lives_ok ( sub {
my $kurt_cobain = { name => 'Kurt Cobain' };
my $in_utero = $schema->resultset('CD')->new({
is($a->name, 'Kurt Cobain', 'Artist insertion ok');
is($a->cds && $a->cds->first && $a->cds->first->title,
'In Utero', 'CD insertion ok');
-};
-diag $@ if $@;
-
-#SPECIAL_CASE2
-eval {
- my $pink_floyd = { name => 'Pink Floyd' };
-
- my $the_wall = { title => 'The Wall', year => 1979 };
-
- $pink_floyd->{cds} = [ $the_wall ];
-
-
- $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
- $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
-
- is($a->name, 'Pink Floyd', 'Artist insertion ok');
- is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
-};
-diag $@ if $@;
+}, 'populate');
## Create foreign key col obj including PK
## See test 20 in 66relationships.t
-eval {
+lives_ok ( sub {
my $new_cd_hashref = {
cdid => 27,
title => 'Boogie Woogie',
my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
is($new_cd->artist->id, 17, 'new id retained okay');
-};
-diag $@ if $@;
+}, 'Create foreign key col obj including PK');
-eval {
+lives_ok ( sub {
$schema->resultset("CD")->create({
cdid => 28,
title => 'Boogie Wiggle',
year => '2007',
artist => { artistid => 18, name => 'larry' }
});
-};
-is($@, '', 'new cd created without clash on related artist');
+}, 'new cd created without clash on related artist');
-# Make sure exceptions from errors in created rels propogate
-eval {
+throws_ok ( sub {
my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
#$t->cd($t->new_related('cd', { artist => undef } ) );
#$t->{_rel_in_storage} = 0;
$t->insert;
-};
-like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
+}, qr/cd.artist may not be NULL/, "Exception propogated properly");
-# Test multi create over many_to_many
-eval {
+lives_ok ( sub {
$schema->resultset('CD')->create ({
artist => {
name => 'larry', # should already exist
is ($m2m_cd->count, 1, 'One CD row created via M2M create');
is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
-};
+}, 'Test multi create over many_to_many');
1;