stepamil has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm looking for a better way to test my modules that are using DBIx::Class models by setting up mock data (in large number of tables) and then running test scripts.

So far, I've been using this code (per module):

#! /usr/bin/perl -w use strict; use Data::Dumper; BEGIN { use DBIx::RunSQL; open(FH,'>','/tmp/create.sql') or die('Could not create a file'); print FH <<EOX; CREATE TABLE `module_test_table` ( `id` int(10) NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `description` varchar(255) default NULL, ); INSERT INTO module_test_table VALUES (1, 'Name1', NULL); INSERT INTO module_test_table VALUES (2, 'Name2', NULL); EOX close(FH); my $dbh = DBIx::RunSQL->create( dsn => 'dbi:SQLite:dbname=:memory:', sql => '/tmp/create.sql', force => 1, #verbose => 1, ); # override the config file where database connection string is # located, instead setup database handle use Internal::Config; $Internal::Config::c->{dbh} = $dbh; $Internal::Config::c->{db_dsn} = undef; $ENV{DBIC_TRACE} = 1; } use Test::More 'no_plan'; # module using DBIx::Class model use ModuleTest; my $module_test = ModuleTest->new({ id => 1 }); is($t->id, 1, 'Value of id'); is($t->name, 'Name1', 'Value of name'); is($t->description, undef, 'Value of description');

This works fine, but I would like to use the actual DBIx::Class models to create tables and set data, and also to extract the boilerplate code to one file used by test scripts.

Thanks

Replies are listed 'Best First'.
Re: in-memory database, testing modules using DBIx::Class models
by stepamil (Acolyte) on May 21, 2014 at 09:37 UTC

    Found a solution that works for me:

    package TestModules; use MyApp::Schema; MyApp::Schema->connection( 'dbi:SQLite:dbname=:memory:', '', '' ); MyApp::Schema->load_namespaces; MyApp::Schema->deploy; # set data MyApp::Schema->resultset('TestModule')->create({ id => 1, name => 'Test', }); 1;

    So, just load this module before tests (on using modules that query DBIx::Class models) and it works.