Yuki Shibazaki > DBIx-DataFactory-0.0.5 > DBIx::DataFactory

Download:
DBIx-DataFactory-0.0.5.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.0.5   Source  

NAME ^

DBIx::DataFactory - factory method maker for inserting test data

SYNOPSIS ^

    # schema
    CREATE TABLE test_factory (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `int` int,
      `double` double,
      `string` varchar(255),
      `text` text DEFAULT NULL,

      PRIMARY KEY (id)
    ) DEFAULT CHARSET=binary;

    # in your t/*.t
    use DBIx::DataFactory;
    my $factory_maker = DBIx::DataFactory->new({
        username => 'nobody',
        password => 'nobody',
        dsn      => 'dbi:mysql:dbname=test_factory;host=localhost',
    });
    $factory_maker->create_factory_method(
        method   => 'create_factory_data',
        table    => 'test_factory',
        auto_inserted_columns => {
            int => {
                type => 'Int',
                size => 8,
            },
            double => sub { rand(100) },
            string => {
                type => 'Str',
                size => 10,
            },
        },
    );

    my $values = $factory_maker->create_factory_data(
        text => 'test text',
    );
    # or you can use DBIx::DataFactory->create_factory_data()
    my $int  = $values->{int};
    my $text = $values->{text};

    # will insert following data
    # +----+----------+------------------+------------+-----------+
    # | id | int      | double           | string     | text      |
    # +----+----------+------------------+------------+-----------+
    # |  1 | 60194256 | 3.03977754238112 | fHt4X0JDr9 | test text |
    # +----+----------+------------------+------------+-----------+

    $values = $factory_maker->create_factory_data(
        int    => 1,
        string => 'test',
    );

    # will insert following data
    # +----+------+-----------------+--------+------+
    # | id | int  | double          | string | text |
    # +----+------+-----------------+--------+------+
    # |  2 |    1 | 71.159467713824 | test   | NULL |
    # +----+------+-----------------+--------+------+

DESCRIPTION ^

This module helps you to make factory method for inserting data into database. You can use this as fixture replacement.

METHODS ^

$class->new(%args)

Create a new DBIx::DataFactory object.

    # set up by username, password, and dsn
    my $factory_maker = DBIx::DataFactory->new({
        username => 'nobody',
        password => 'nobody',
        dsn      => 'dbi:mysql:dbname=test_factory;host=localhost',
    });

    # or set up by db handler
    my $dbh = DBI->connect(
        'dbi:mysql:dbname=test_factory;host=localhost',
        'nobody',
        'nobody',
    );
    my $factory_maker = DBIx::DataFactory->new({
        dbh => $dbh,
    });

Set up initial state by following parameters.

$self->create_factory_method(%args)

This installs the method, which helps inserting data into database, in the DBIx::DataFactory package by default.

    $factory_maker->create_factory_method(
        method   => 'create_factory_data',
        table    => 'test_factory',
        auto_inserted_columns => {
            int => {
                type => 'Int',
                size => 8,
            },
            string => {
                type => 'Str',
                size => 10,
            },
        },
    );

if this is the case, this make the method named 'create_factory_data'. you can pass all columns value you defined in schema.

    my $values = $factory_maker->create_factory_data(
        int    => 5,
        string => 'string',
        text   => 'test text',
    );

    # this makes following data.
    +----+-----+--------+-----------+
    | id | int | string | text      |
    +----+-----+--------+-----------+
    |  1 |  5  | string | test text |
    +----+-----+--------+-----------+


    my $values = $factory_maker->create_factory_data;

    # this makes following data
    +----+----------+------------+------+
    | id | int      | string     | text |
    +----+----------+------------+------+
    |  2 | 59483011 | 9svzODgYyz | NULL |
    +----+----------+------------+------+

Parameters

add_type

you can add type class which define the rule of inserting data. See also DBIx::DataFactory::Type.

    DBIx::DataFactory->add_type('DBIx::DataFactory::Type::Test');

REPOSITORY ^

https://github.com/shibayu36/p5-DBIx-DataFactory

AUTHOR ^

  C<< <shibayu36 {at} gmail.com> >>

LICENCE AND COPYRIGHT ^

Copyright (c) 2011, Yuki Shibazaki <shibayu36 {at} gmail.com>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

syntax highlighting: