From: John Napiorkowski Date: Wed, 2 Mar 2011 15:43:35 +0000 (-0500) Subject: refacted dump all a bit X-Git-Tag: 1.001012~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5cc47846578c11f8a940b0e5cdc8c3e1466a90e0;p=dbsrgits%2FDBIx-Class-Fixtures.git refacted dump all a bit --- diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 630e1f6..9c395f6 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -939,10 +939,10 @@ sub _read_sql { return \@data; } -=head2 dump_all_config_sets +=head2 dump_config_sets Works just like L but instead of specifying a single json config set -located in L we dump each set in turn to the specified directory. +located in L we dump each set named in the C parameter. The parameters are the same as for L except instead of a C parameter we have a C which is a coderef expected to return @@ -952,6 +952,7 @@ example: $fixture->dump_all_config_sets({ schema => $schema, + configs => [qw/one.json other.json/], directory_template => sub { my ($fixture, $params, $set) = @_; return File::Spec->catdir('var', 'fixtures', $params->{schema}->version, $set); @@ -960,19 +961,50 @@ example: =cut -sub dump_all_config_sets { +sub dump_config_sets { my ($self, $params) = @_; - my @available_config_sets = $self->available_config_sets; + my $available_config_sets = delete $params->{configs}; my $directory_template = delete $params->{directory_template} || DBIx::Class::Exception->throw("'directory_template is required parameter"); - for my $set (@available_config_sets) { - local($self,$params); - $params->{directory} = $directory_template->($self, $params, $set); - $self->dump($params) + for my $set (@$available_config_sets) { + warn "doing $set"; + my $localparams = $params; + $localparams->{directory} = $directory_template->($self, $localparams, $set); + $localparams->{config} = $set; + $self->dump($localparams); } } +=head2 dump_all_config_sets + +Works just like L but instead of specifying a single json config set +located in L we dump each set in turn to the specified directory. + +The parameters are the same as for L except instead of a C +parameter we have a C which is a coderef expected to return +a scalar that is a root directory where we will do the actual dumping. This +coderef get three arguments: C<$self>, C<$params> and C<$set_name>. For +example: + + $fixture->dump_all_config_sets({ + schema => $schema, + directory_template => sub { + my ($fixture, $params, $set) = @_; + return File::Spec->catdir('var', 'fixtures', $params->{schema}->version, $set); + }, + }); + +=cut + +sub dump_all_config_sets { + my ($self, $params) = @_; + $self->dump_config_sets({ + %$params, + configs=>[$self->available_config_sets], + }); +} + =head2 populate =over 4 diff --git a/t/17-dump_all_config_sets.t b/t/17-dump_all_config_sets.t new file mode 100644 index 0000000..f150cac --- /dev/null +++ b/t/17-dump_all_config_sets.t @@ -0,0 +1,49 @@ +use DBIx::Class::Fixtures; +use Test::More tests => 17; + +use lib qw(t/lib); +use DBICTest; +use Path::Class; +use Data::Dumper; +use File::Spec; + +ok my $config_dir = 't/var/configs'; +ok my $schema = DBICTest->init_schema(), 'got schema'; +ok my $fixtures = DBIx::Class::Fixtures->new({config_dir => $config_dir}), + 'object created with correct config dir'; + +ok( + $fixtures->dump_config_sets({ + configs => [qw/date.json rules.json/], + schema => $schema, + directory_template => sub { + my ($fixture, $params, $set) = @_; + File::Spec->catdir('t','var','fixtures','multi', $set); + }, + }), + 'simple dump executed okay', +); + +__END__ + + # check dump is okay + my $dir = dir('t/var/fixtures/artist'); + ok(-e 't/var/fixtures/artist', 'artist directory created'); + + my @children = $dir->children; + is(scalar(@children), 1, 'right number of fixtures created'); + + my $fix_file = $children[0]; + my $HASH1; eval($fix_file->slurp()); + is(ref $HASH1, 'HASH', 'fixture evals into hash'); + + is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys'); + + my $artist = $schema->resultset('Artist')->find($HASH1->{artistid}); + is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row'); + + $schema->resultset('Artist')->delete; # so we can create the row again on the next line + ok($schema->resultset('Artist')->create($HASH1), 'new dbic row created from fixture'); +} + +