[Bast-commits] r3322 - in branches/DBIx-Class/bulk_create: lib/DBIx/Class t

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Fri May 18 01:05:08 GMT 2007


Author: jnapiorkowski
Date: 2007-05-18 01:05:07 +0100 (Fri, 18 May 2007)
New Revision: 3322

Modified:
   branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
   branches/DBIx-Class/bulk_create/t/101populate_rs.t
Log:
-- some additional tests for edge cases
-- code cleanups
-- more intuitive handling of single row sub populates

Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-17 23:05:59 UTC (rev 3321)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-18 00:05:07 UTC (rev 3322)
@@ -1288,41 +1288,32 @@
       push(@created, $self->create($item));
     }
     return @created;
-  } 
-  else
-  {
+  } else {
     my ($first, @rest) = @$data;
 	
-	## We assume for now that the first item is required to name all the columns
-	## and relationships similarly to how schema->populate requires a first item
-	## of all the column names.
-	
     my @names = grep { !ref $first->{$_} } keys %$first;
-	my @values = map { [ map {defined $_ ? $_ : $self->throw_exception("Undefined value for column!")} @$_{@names} ] } @$data;
+	
+	my @values = map {
+		[ map {
+			defined $_ ? $_ : $self->throw_exception("Undefined value for column!")
+		} @$_{@names} ]
+	} @$data;
 		
     $self->result_source->storage->insert_bulk(
 		$self->result_source, 
 		\@names, 
 		\@values,
 	);
-	
-	## Again we assume the first row has to define all the related resultsets
+
 	my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
 	my @pks = $self->result_source->primary_columns;
-	
-	## Must have PKs to use this!!!
 
-	foreach my $item (@$data)
-	{
-		## First we need to get a result for each
-		## We need to call populate for each relationship.
+	foreach my $item (@$data) {
 
-		foreach my $rel (@rels)
-		{
+		foreach my $rel (@rels) {
 			next unless $item->{$rel};
-			my $parent	= $self->find(map {{$_=>$item->{$_}} } @pks)
-			 || next;
 			
+			my $parent	= $self->find(map {{$_=>$item->{$_}} } @pks) || next;
 			my $child	= $parent->$rel;
 
 			my $related = $child->result_source->resolve_condition(
@@ -1330,12 +1321,12 @@
 				$parent->result_source->relationship_info($rel)->{cond},
 				$child,
 				$parent,
-				
 			);
 			
-			my $populate = [map {  {%$_, %$related} } @{$item->{$rel}}];
+			my @rows_to_add = ref $item->{$rel} eq 'ARRAY' ? @{$item->{$rel}} : ($item->{$rel});
+			my @populate = map { {%$_, %$related} } @rows_to_add;
 
-			$child->populate( $populate );
+			$child->populate( \@populate );
 		}
 	}
 	

Modified: branches/DBIx-Class/bulk_create/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-17 23:05:59 UTC (rev 3321)
+++ branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-18 00:05:07 UTC (rev 3322)
@@ -5,14 +5,14 @@
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 25;
+plan tests => 31;
 
 my $schema = DBICTest->init_schema();
 my $rs = $schema->resultset('Artist');
 
 RETURN_RESULTSETS: {
 
-	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,9 +37,11 @@
 	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");
+	ok( $xxxaaa->name eq 'bbbb', "Got Correct name for result object");
 	
 	use Data::Dump qw/dump/;
 	
@@ -62,14 +64,14 @@
 
 		]
 	  },
-	  {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 +84,9 @@
 
 	$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 +103,13 @@
 	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');
 }
 




More information about the Bast-commits mailing list