[Dbix-class] Dumping fixtures and loading in test script

Rob Kinyon rob.kinyon at gmail.com
Mon Nov 11 15:18:17 GMT 2013


Adam -

    Each test sets up the database exactly as it wants. In general, I
do something like:

$schema->deploy({
    add_drop_table => 1,
});
$schema->load_sims( $spec );

in each one of my test scripts that use the Sims. Presumably, I'm
working in some in-memory SQLite database or a scratch MySQL database
created for this test run.

    Remember - you want to be able to run your tests in any order and
(with appropriate configuration) in parallel. So, you never want to
have "the setup script" followed by "the tests that use it".

Rob

On Mon, Nov 11, 2013 at 10:07 AM, Adam Witney <awitney at sgul.ac.uk> wrote:
> Hi Rob,
>
> Thanks again for your help.
>
> The problem I am having is working out how to put it all together. The
> documentation often contains snippets of code, but not a  full (short)
> script, which would help when trying to see how it all works together.
>
> So with the ::Sims module, does each test script generate its own test data
> in the database which can then be used in the same script for testing, or do
> you have one script to load the simulated data for a set of test scripts to
> then work on?
>
> Thanks again
>
> Adam
>
>
> On 05/11/2013 16:44, Rob Kinyon wrote:
>>
>> Instead of dumping data from a reference database, ::Sims allows you
>> to generate reasonable-looking data directly. This way, if your schema
>> changes, your fixtures aren't out-of-date anymore. Also, each test now
>> describes exactly what it needs from the database without having to
>> depend on a large set of fixtures that have everything for many tests
>> all in one place.
>>
>> So, if you had the standard Artist->Album->Track and you had a test
>> that cared about dealing with tracks, you could say something like:
>>
>> my $rows = $schema->load_sims({
>>      Track => [
>>          { name => 'One' },
>>          {}, # Generate a second one - we don't care about the name.
>>      ],
>> });
>>
>> # Returned to you
>> is( $rows->{Track}[0]->name, 'One' );
>>
>> # And written into the database
>> is( $schema->resultset('Track')->search({}, { order_by => 'id'
>> })->first->name, 'One' );
>>
>> And an album and an artist will be created. But, you don't care about
>> that in your test, so you don't specify it. Then, later, if a Studio
>> concept comes along as a second parent of Album, your test never even
>> notices the difference. With fixtures, your test breaks until the
>> fixtures are updated.
>>
>> If the documentation isn't clear, please ask questions so I can clarify.
>>
>> On Tue, Nov 5, 2013 at 11:36 AM, Adam Witney <awitney at sgul.ac.uk> wrote:
>>>
>>> Thanks Rob, although I'm not sure how that makes it any easier, do you
>>> have
>>> an example script?
>>>
>>> Thanks
>>>
>>> Adam
>>>
>>>
>>> On 4. 11. 2013 15:53, Rob Kinyon wrote:
>>>>
>>>> This doesn't answer your question directly, but consider using
>>>> DBIx::Class::Sims instead for generating test data.
>>>>
>>>> On Mon, Nov 4, 2013 at 10:33 AM, Adam Witney <awitney at sgul.ac.uk> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I am having trouble getting fixtures dumped and then reloaded in my
>>>>> test
>>>>> scripts. The first problem is dumping with this script snippet:
>>>>>
>>>>> use DBIx::Class::Fixtures;
>>>>> use RCA::Schema;
>>>>> use lib qw(lib);
>>>>>
>>>>> my $schema = RCA::Schema->connect('dbi:Pg:dbname=dev', 'user', '');
>>>>>
>>>>> my $fixtures = DBIx::Class::Fixtures->new({
>>>>>        config_dir => 't/var/configs', debug => 1
>>>>> });
>>>>>
>>>>> $fixtures->dump({
>>>>>      all => 1,
>>>>>      schema => $schema,
>>>>>      directory => 't/var/fixtures'
>>>>> });
>>>>>
>>>>> This produces this output:
>>>>>
>>>>> generating  fixtures
>>>>> - creating /tmp/BbI5ISYRxw
>>>>> - dumping Episode
>>>>> - moving temp dir to t/var/fixtures
>>>>> - clearing tmp dir /tmp/BbI5ISYRxw
>>>>> done
>>>>>
>>>>> but something seems to have gone wrong as the fixtures are still in the
>>>>> tmp
>>>>> dir
>>>>>
>>>>> $ ls t/var/fixtures/
>>>>> _config_set  _dumper_version
>>>>>
>>>>> $ ls /tmp/BbI5ISYRxw/
>>>>> episode
>>>>>
>>>>> The next problem is that I can't yet figure out how to load the
>>>>> fixtures
>>>>> in
>>>>> my test scripts, I have a conf here:
>>>>>
>>>>> $ less t/etc/schema.conf
>>>>> schema_class RCA::Schema
>>>>> connect_info dbi:Pg:dbname=dev
>>>>> user user
>>>>> force_drop_table 1
>>>>> keep_db 1
>>>>>
>>>>> And a test script
>>>>>
>>>>> $ less t/model/Episode.t
>>>>> use Test::More;
>>>>> use Test::DBIx::Class qw(:resultsets);
>>>>>
>>>>> fixtures_ok 'basic'
>>>>>         => 'installed the basic fixtures from configuration files';
>>>>> ok my $episode_id = Episode->find(1) => 'Episode found ok';
>>>>>
>>>>> but of course, the test fails.
>>>>>
>>>>> Any pointers as to what I am missing?
>>>>>
>>>>> Thanks for any help
>>>>>
>>>>> Adam
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>>>>> IRC: irc.perl.org#dbix-class
>>>>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>>>>> Searchable Archive:
>>>>> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>>>>
>>>>
>>>>
>>>>
>>> _______________________________________________
>>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>>> IRC: irc.perl.org#dbix-class
>>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>>> Searchable Archive:
>>> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>>
>>
>>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk



-- 
Thanks,
Rob Kinyon
http://streamlined-book.blogspot.com/



More information about the DBIx-Class mailing list