Testing databases with DBIx::Class

Gareth Harper spansh+london at gmail.com
Tue Jan 10 10:28:56 GMT 2012


On 10 January 2012 10:12, Aaron Trevena <aaron.trevena at gmail.com> wrote:
> On 10 January 2012 09:56, Ian Knopke <ian.knopke at gmail.com> wrote:
>> Hi,
>>
>> I need to test some DBIx::Class code where the database may not be
>> available. I can set up something to generate a small, temporary
>> SQlite db, but I was wondering what approaches others are currently
>> using for this. DBD::Mock seems ok but not especially well suited for
>> use with DBIx. What does the rest of the community currently do?
>
> I'd have a look at DBIx::Class:Fixtures, I've also manually created
> fixture data in scripts in the past (and present) too.
>

I have a semi automated DBIx::Class::Factory class I wrote
specifically for the purposes of creating test data (you request the
object you want and it automatically resolves the dependant objects
and creates them for you with semi random data).  The main reason for
auto creation of the parent objects is often the size of your "create
test data" function/fixtures/sql file exceeds the size of the test.

You'd still need to be running SQlite or a database of some sort in
the background (and it assumes relationships are set up correctly such
that it can resolve parent objects).

I am intending to tidy it up and put it on CPAN at some point but have
been rather busy unfortunately.  If it scratches your itch I can spend
an hour or two tidying it up slightly and pass it along.

It works something like the following:

my $account = DBIx::Class::FactoryNew::create_Account(
   name => 'Test Account',
   active => 1,
);

my $user = DBIx::Class::FactoryNew::create_User(
   account => $account,
   name => 'Test user,
   active => 1,
);


Any field or relationship which is not passed in will be filled with
random data (such that you're not relying on anything you shouldn't
be).

If you simply required a User object you would:

 my $user = DBIx::Class:FactoryNew::create_User(
   name => 'Test user,
   active => 1,
);

After which the parent account object would be automatically created
for you and filled with random data.

Thanks

Gareth


More information about the london.pm mailing list