-- added support for belongs_to type relationships and better support for when the...
[dbsrgits/DBIx-Class.git] / t / 101populate_rs.t
index 040e076..c1f497f 100644 (file)
@@ -5,14 +5,14 @@ use Test::More;
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 25;
+plan tests => 53;
 
 my $schema = DBICTest->init_schema();
 my $rs = $schema->resultset('Artist');
 
-RETURN_RESULTSETS: {
+RETURN_RESULTSETS_HAS_MANY: {
 
-       my ($crap, $girl, $damn) = $rs->populate( [
+       my ($crap, $girl, $damn, $xxxaaa) = $rs->populate( [
          { artistid => 4, name => 'Manufactured Crap', cds => [ 
                  { title => 'My First CD', year => 2006 },
                  { title => 'Yet More Tweeny-Pop crap', year => 2007 },
@@ -37,17 +37,17 @@ RETURN_RESULTSETS: {
        isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
        isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
        isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");     
+       isa_ok( $xxxaaa, 'DBICTest::Artist', "Got 'Artist'");   
        
        ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
        ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
-       
-       use Data::Dump qw/dump/;
-       
+       ok( $xxxaaa->name eq 'bbbb', "Got Correct name for result object");
+               
        ok( $crap->cds->count == 2, "got Expected Number of Cds");
        ok( $girl->cds->count == 3, "got Expected Number of Cds");
 }
 
-RETURN_VOID: {
+RETURN_VOID_HAS_MANY: {
 
        $rs->populate( [
          { artistid => 8, name => 'Manufactured CrapB', cds => [ 
@@ -62,14 +62,14 @@ RETURN_VOID: {
 
                ]
          },
-         {artistid=>10,  name => 'XXXX' }
-
+         { artistid =>10,  name => 'XXXX' },
+         { artistid =>11, name => 'wart', cds =>[{ title => 'xxxaaa' ,year => 2005 }], },
        ] );
        
        my $artist = $rs->find(8);
 
        ok($artist, 'Found artist');
-       is($artist->name, 'Manufactured CrapB');
+       is($artist->name, 'Manufactured CrapB', "Got Correct Name");
        is($artist->cds->count, 2, 'Has CDs');
 
        my @cds = $artist->cds;
@@ -82,9 +82,9 @@ RETURN_VOID: {
 
        $artist = $rs->find(9);
        ok($artist, 'Found artist');
-       is($artist->name, 'Angsty-Whiny GirlB');
+       is($artist->name, 'Angsty-Whiny GirlB', "Another correct name");
        is($artist->cds->count, 3, 'Has CDs');
-
+       
        @cds = $artist->cds;
 
 
@@ -101,6 +101,123 @@ RETURN_VOID: {
        ok($artist, "Got Expected Artist Result");
 
        is($artist->cds->count, 0, 'No CDs');
+       
+       $artist = $rs->find(10);
+       is($artist->name, 'XXXX', "Got Correct Name");
+       is($artist->cds->count, 0, 'Has NO CDs');
+       
+       $artist = $rs->find(11);
+       is($artist->name, 'wart', "Got Correct Name");
+       is($artist->cds->count, 1, 'Has One CD');
+}
 
+RETURN_RESULTSETS_AUTOPK: {
+
+       my ($crap, $girl, $damn, $xxxaaa) = $rs->populate( [
+         { name => 'Manufactured CrapC', cds => [ 
+                 { title => 'My First CD', year => 2006 },
+                 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
+               ] 
+         },
+         { name => 'Angsty-Whiny GirlC', cds => [
+                 { title => 'My parents sold me to a record company' ,year => 2005 },
+                 { title => 'Why Am I So Ugly?', year => 2006 },
+                 { title => 'I Got Surgery and am now Popular', year => 2007 }
+
+               ]
+         },
+         { name => 'Like I Give a DamnC' },
+         
+         { name => 'bbbbC', cds => [
+                 { title => 'xxxaaa' ,year => 2005 },
+               ]
+         },
+
+       ] );
+       
+       isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
+       isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
+       isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");     
+       isa_ok( $xxxaaa, 'DBICTest::Artist', "Got 'Artist'");   
+       
+       ok( $crap->name eq 'Manufactured CrapC', "Got Correct name for result object");
+       ok( $girl->name eq 'Angsty-Whiny GirlC', "Got Correct name for result object");
+       ok( $xxxaaa->name eq 'bbbbC', "Got Correct name for result object");
+       
+       ok( $crap->cds->count == 2, "got Expected Number of Cds");
+       ok( $girl->cds->count == 3, "got Expected Number of Cds");
+}
+
+RETURN_RESULTSETS_BELONGS_TO: {
+
+       ## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
+       
+       my $cds = [
+               {
+                       title => 'Some CD1',
+                       year => '1997',
+                       artist => { name => 'Fred BloggsA'},
+               },
+               {
+                       title => 'Some CD2',
+                       year => '1997',
+                       artist => { name => 'Fred BloggsB'},
+               },              
+       ];
+       
+       my $cd_rs = $schema->resultset('CD');
+       
+       my @ret = $cd_rs->populate($cds);
+       
+       my $cdA = $schema->resultset('CD')->find({title => 'Some CD1'});
+
+       isa_ok($cdA, 'DBICTest::CD', 'Created CD');
+       isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
+       is($cdA->artist->name, 'Fred BloggsA', 'Set Artist to FredA');
+
+       my $cdB = $schema->resultset('CD')->find({title => 'Some CD2'});
+       
+       isa_ok($cdB, 'DBICTest::CD', 'Created CD');
+       isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
+       is($cdB->artist->name, 'Fred BloggsB', 'Set Artist to FredB');
 }
 
+RETURN_VOID_BELONGS_TO: {
+
+       ## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
+       
+       my $cds = [
+               {
+                       title => 'Some CD3',
+                       year => '1997',
+                       artist => { name => 'Fred BloggsC'},
+               },
+               {
+                       title => 'Some CD4',
+                       year => '1997',
+                       artist => { name => 'Fred BloggsD'},
+               },              
+       ];
+       
+       my $cd_rs = $schema->resultset('CD');
+       
+       ok( $cd_rs, 'Got Good CD Resultset');
+       
+       $cd_rs->populate($cds);
+       
+       my $cdA = $schema->resultset('CD')->find({title => 'Some CD3'});
+
+       isa_ok($cdA, 'DBICTest::CD', 'Created CD');
+       isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
+       is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
+
+       my $cdB = $schema->resultset('CD')->find({title => 'Some CD4'});
+       
+       isa_ok($cdB, 'DBICTest::CD', 'Created CD');
+       isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
+       is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
+}
+
+
+
+