Announcing the initial release of DBIx::Class::Sims. This is a schema extension that allows you to sanely and easily generate good-looking and legal test data, only specifying the actual rows you care about in your test and letting DBIC figure out the rest. (Repo is https://github.com/ robkinyon/dbix-class-sims)
Let's assume you have a standard Artist->Album->Track schema where Artist->has_many(Albums) and Album->has_many(Tracks).
my $ids = $schema->load_sims({
Track => [
{ name => 'Hello, Dolly' },
{ name => 'Moonlight Sonata', album.artist.name => 'Beethoven' },
],
});
That will do exactly what you think it should do. It will generate a randomized artist with a randomized album that has a single track called "Hello, Dolly". It will also generate another artist with a name of Beethoven who has a randomized album that has a single track called "Moonlight Sonata". All other columns will be randomized.
The return value $ids will be a hashref that will look something like:
$ids = {
Track => [
{ id => 1 }, { id => 2 },
],
};
};
Corresponding to the rows in the tracks table that you requested to be created.
Now, if someone goes ahead and adds a Studio table and Studio->has_many(Albums) (because artists create albums in studios, but don't always stick to the same studio), then your test doesn't break and it doesn't change. At all. For each album that's created, a randomized studio will be generated. Because you didn't specify anything, the same studio will be used for every album. (Remember - your test is about tracks, so you don't care if the same studio is used.)
If, for some reason, your application requires that all artists have at least 2 albums to be legal, that's easily specified as well.
my $ids = $schema->load_sims(
{ Track => [ {} ] },
{ Artist => { albums => 2 } },
);
The randomization part comes from the sim types. You can add the type to each column and a reasonable-looking value will be generated instead of the default_value. Right now, the only type that has been written is us_zipcode. More will be forthcoming and you can release your own CPAN distribution with types if I'm too slow in adding them.
As with everything else DBIC-related, patches are welcome (as pull-requests on github) and discussion is on this mailing list or #dbix-class in IRC.
No comments:
Post a Comment