Added multi-create object support to Schema->populate and created a test for this...
John Napiorkowski [Mon, 25 Jun 2007 22:55:24 +0000 (22:55 +0000)]
lib/DBIx/Class.pm
lib/DBIx/Class/Schema.pm
t/101populate_rs.t

index 42742f2..09953b1 100644 (file)
@@ -222,6 +222,8 @@ jesper: Jesper Krogh
 
 jguenther: Justin Guenther <jguenther@cpan.org>
 
+jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
+
 jshirley: J. Shirley <jshirley@gmail.com>
 
 konobi: Scott McWhirter
index 99b7f28..db2a8f6 100644 (file)
@@ -851,7 +851,15 @@ sub populate {
     }
     return @created;
   }
-  $self->storage->insert_bulk($self->source($name), \@names, $data);
+  my @results_to_create;
+  foreach my $datum (@$data) {
+    my %result_to_create;
+    foreach my $index (0..$#names) {
+      $result_to_create{$names[$index]} = $$datum[$index];
+    }
+    push @results_to_create, \%result_to_create;
+  }
+  $rs->populate(\@results_to_create);
 }
 
 =head2 exception_action
index 5ea3a55..9bbe60f 100644 (file)
@@ -15,7 +15,7 @@ use Test::More;
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 98;
+plan tests => 120;
 
 
 ## ----------------------------------------------------------------------------
@@ -32,6 +32,76 @@ ok( $cd_rs, 'Got Good CD Resultset');
 
 
 ## ----------------------------------------------------------------------------
+## Schema populate Tests
+## ----------------------------------------------------------------------------
+
+SCHEMA: {
+
+       ## Test to make sure that the old $schema->populate is using the new method
+       ## for $resultset->populate when in void context and with sub objects.
+       
+       $schema->populate('Artist', [
+       
+               [qw/name cds/],
+               ["001First Artist", [
+                       {title=>"001Title1", year=>2000},
+                       {title=>"001Title2", year=>2001},
+                       {title=>"001Title3", year=>2002},
+               ]],
+               ["002Second Artist", []],
+               ["003Third Artist", [
+                       {title=>"003Title1", year=>2005},
+               ]],
+       ]);
+       
+       isa_ok $schema, 'DBIx::Class::Schema';
+       
+       my ($artist1, $artist2, $artist3) = $schema->resultset('Artist')->search({
+               name=>["001First Artist","002Second Artist","003Third Artist"]},
+               {order_by=>'name ASC'})->all;
+       
+       isa_ok  $artist1, 'DBICTest::Artist';
+       isa_ok  $artist2, 'DBICTest::Artist';
+       isa_ok  $artist3, 'DBICTest::Artist';
+       
+       ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
+       ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
+       ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
+       
+       ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
+       ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
+       ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
+       
+       ARTIST1CDS: {
+       
+               my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
+               
+               isa_ok $cd1, 'DBICTest::CD';
+               isa_ok $cd2, 'DBICTest::CD';
+               isa_ok $cd3, 'DBICTest::CD';
+               
+               ok $cd1->year == 2000;
+               ok $cd2->year == 2001;
+               ok $cd3->year == 2002;
+               
+               ok $cd1->title eq '001Title1';
+               ok $cd2->title eq '001Title2';
+               ok $cd3->title eq '001Title3';
+       }
+       
+       ARTIST3CDS: {
+       
+               my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
+               
+               isa_ok $cd1, 'DBICTest::CD';
+
+               ok $cd1->year == 2005;
+               ok $cd1->title eq '003Title1';
+       }       
+}
+
+
+## ----------------------------------------------------------------------------
 ## Array context tests
 ## ----------------------------------------------------------------------------