From: John Napiorkowski Date: Fri, 6 Mar 2015 00:17:29 +0000 (-0600) Subject: testing for when resources are nested X-Git-Tag: v1.001_030~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-Fixtures.git;a=commitdiff_plain;h=c7aececc25235b590e39a63b7aa9ea98a3f4a10e testing for when resources are nested --- diff --git a/t/07-dump-all.t b/t/07-dump-all.t index 6a070c4..498a4db 100644 --- a/t/07-dump-all.t +++ b/t/07-dump-all.t @@ -8,7 +8,7 @@ use Path::Class; use Data::Dumper; use IO::All; use if $^O eq 'MSWin32','Devel::Confess'; -plan tests => 16; +plan tests => 18; # set up and populate schema ok(my $schema = DBICTest->init_schema( ), 'got schema'); diff --git a/t/19-complex-hierarchy.t b/t/19-complex-hierarchy.t new file mode 100644 index 0000000..4da70a5 --- /dev/null +++ b/t/19-complex-hierarchy.t @@ -0,0 +1,88 @@ +#!perl + +use DBIx::Class::Fixtures; +use Test::More tests => 4; +use lib qw(t/lib); +use DBICTest; +use Path::Class; +use Data::Dumper; +use IO::All; + +use if $^O eq 'MSWin32','Devel::Confess'; +# set up and populate schema +ok(my $schema = DBICTest->init_schema(), 'got schema'); + +my $config_dir = io->catfile(qw't var configs')->name; + +# Add washedup + +ok my $artist = $schema->resultset("Artist")->find(1); +ok my $washed_up = $artist->create_related('washed_up', +{}); +ok $washed_up->fk_artistid; + +__END__ + +{ + # do dump + ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir'); + ok($fixtures->dump({ config => 'simple.json', schema => $schema, directory => io->catfile(qw't var fixtures')->name }), 'simple dump executed okay'); + + # check dump is okay + my $dir = dir(io->catfile(qw't var fixtures artist')->name); + ok(-e io->catfile(qw't var fixtures artist')->name, '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'); +} + +{ + # do dump with hashref config + ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir'); + ok($fixtures->dump({ + config => { + "might_have" => { + "fetch" => 0 + }, + "has_many" => { + "fetch" => 0 + }, + "sets" => [{ + "class" => "Artist", + "quantity" => 1 + }] + }, + schema => $schema, + directory => io->catfile(qw't var fixtures')->name, + }), 'simple dump executed okay'); + + # check dump is okay + my $dir = dir(io->catfile(qw't var fixtures artist')->name); + ok(-e io->catfile(qw't var fixtures artist')->name, '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'); +} diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index 08c0e27..8f5dca3 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -152,6 +152,11 @@ sub populate_schema { [ 32948, 'Big PK' ], ]); + $schema->populate('Artist::WashedUp', [ + [ qw/fk_artistid/ ], + [ 2 ], + ]); + $schema->populate('CD', [ [ qw/cdid artist title year/ ], [ 1, 1, "Spoonful of bees", 1999 ], diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm index 350eecc..23e03d6 100644 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@ -5,6 +5,6 @@ use base qw/DBIx::Class::Schema/; no warnings qw/qw/; -__PACKAGE__->load_classes(qw/Artist CD Track Tag Producer CD_to_Producer/); +__PACKAGE__->load_classes(qw/Artist Artist::WashedUp CD Track Tag Producer CD_to_Producer/); 1; diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index f6d8180..3925cd6 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -22,6 +22,11 @@ __PACKAGE__->has_many( { order_by => 'year' }, ); +__PACKAGE__->might_have( + washed_up => 'DBICTest::Schema::Artist::WashedUp', + {'foreign.fk_artistid' => 'self.artistid'}, +); + sub new { my ( $class, $args ) = @_; diff --git a/t/lib/DBICTest/Schema/Artist/WashedUp.pm b/t/lib/DBICTest/Schema/Artist/WashedUp.pm index 1bbc218..998139f 100644 --- a/t/lib/DBICTest/Schema/Artist/WashedUp.pm +++ b/t/lib/DBICTest/Schema/Artist/WashedUp.pm @@ -1,5 +1,5 @@ package # hide from PAUSE - DBICTest::Schema::Artist; + DBICTest::Schema::Artist::WashedUp; use base 'DBIx::Class::Core'; @@ -7,7 +7,6 @@ __PACKAGE__->table('artist_washed_up'); __PACKAGE__->add_columns( 'fk_artistid' => { data_type => 'integer', - is_auto_increment => 1, }, ); diff --git a/t/lib/mysql.sql b/t/lib/mysql.sql index ca1b034..47bad04 100644 --- a/t/lib/mysql.sql +++ b/t/lib/mysql.sql @@ -17,6 +17,13 @@ CREATE TABLE artist ( name varchar(100) ); +-- +-- Table: artist_washed_up +-- +DROP TABLE IF EXISTS artist_washed_up; +CREATE TABLE artist_washed_up ( + fk_artistid INTEGER PRIMARY KEY NOT NULL +); -- -- Table: cd diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql index 1e21627..f863b03 100644 --- a/t/lib/sqlite.sql +++ b/t/lib/sqlite.sql @@ -21,6 +21,12 @@ CREATE TABLE artist ( name varchar(100) ); +-- +-- Table: artist_washed_up +-- +CREATE TABLE artist_washed_up ( + fk_artistid INTEGER PRIMARY KEY NOT NULL +); -- -- Table: cd