Skip to content

Instantly share code, notes, and snippets.

@nrdvana
Last active May 10, 2020 17:07
Show Gist options
  • Save nrdvana/f00fa89ac6c1bb18eae6e22c8323e699 to your computer and use it in GitHub Desktop.
Save nrdvana/f00fa89ac6c1bb18eae6e22c8323e699 to your computer and use it in GitHub Desktop.
package Mock::RelationalData;
use Moo 2;
=head1 SYNOPSIS
my $reldata= Mock::RelationalData->new;
# Define custom mock data generators
$reldata->add_generator("words" => ...);
# Define relational schema
$reldata->define_schema(
# you can import a whole DBIC schema
$dbic_schema,
# or import specific tables from a DBIC schema
$dbic_result_source,
# or define tables on your own
artist => [
id => { type => 'integer', default => 'auto_inc', pk => 1 },
name => { type => 'varchar(99)', default => 'words' },
formed => { type => 'datetime' },
disbanded => { type => 'datetime', null => 1 },
],
album => [
id => { type => 'integer', default => 'auto_inc', pk => 1 },
artist_id => { type => 'integer', fk => [artist => 'id'] },
name => { type => 'varchar(99)', default => 'words' },
released => { type => 'datetime', null => 1 },
],
);
# Then create a record
$reldata->add_records(album => { name => 'Test' });
# has the effect of:
# push @artist, [ my $artist_id= auto_inc(...), words(...), datetime(...), undef ];
# push @album, [ auto_inc(...), $artist_id, varchar(...), undef ];
# Then use the data:
for my $dataset ($reldata->get_populate_sequence(as_array => 1)) {
$dbic_schema->resultset($dataset->{table_name})->populate($dataset->{rows});
}
=head1 DESCRIPTION
This module assists you with creating data that adheres to a relational schema
with a minimal amount of manually-specified data. This is primarily useful for
generating test cases of complicated schemas where you only want to declare the
few fields that are applicable to the test, but need to have many fields filled
out in order for the code to run correctly.
It is primarily meant to be used with L<DBIx::Class>, but does not depend on that
ecosystem.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment