NAME

DBD::Mock::Session::GenerateFixtures - A module to generate fixtures for DBD::Mock::Session

SYNOPSIS

# Case 1: Providing a pre-existing DBI database handle for genereting a mocked data files
# with the test name
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ dbh => $dbh });
my $real_dbh = $mock_dumper->get_dbh();

# Case 2: Read data from the same file as current test
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new();
my $dbh = $mock_dumper->get_dbh();
# Your code using the mock DBD

# Case 3: Read data from a coustom file
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ file => 'path/to/fixture.json' });
my $dbh = $mock_dumper->get_dbh();
# Your code using the mock DBD

# Case 4: Providing an array reference containing mock data
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ data => \@mock_data });
my $dbh = $mock_dumper->get_dbh();
# Your code using the mock DBD

# or with Rose::DB

my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new();

my $override = Sub::Override->new();
my $dbh      = $mock_dumper->get_dbh();
$dbh->{mock_start_insert_id} = 3;

$override->replace('Rose::DB::dbh' => sub {return $dbh});
$override->inject('DBD::Mock::db::last_insert_rowid', sub {$dbh->{mock_last_insert_id}});

my $num_rows_updated = DB::Media::Manager->update_media(
	set => {
		location => '/data/music/claire_de_lune.ogg',
	},
	where => [
		id => 2,
	],
);

DESCRIPTION

When a real DBI database handle ($dbh) is provided, the module generates DBD::Mock::Session data and stores it in a JSON file. After the data is generated, remove the 'dbh' argument from the constructor, and it will use the previously generated data to create a 'DBD::Mock::Session' database handle. Mocked data can also be loaded from a custom file or as a data structure. This is not a part of the DBD::Mock::Session distribution; it's just a wrapper around it."

METHODS

new(\%args_for)

Constructor method to create a new DBD::Mock::Session::GenerateFixtures object.

Accepts an optional hash reference \%args_for with the following keys:

  • file: File path to the fixture file containing mocked data.

  • data: Reference to an array containing mock data.

  • dbh: Database handle used for reading the data required to genereate a mocked dbh. This should used first time you are runnig the tests.

get_dbh()

Returns the mocked database handle object.

get_override_object()

Returns the override object used for mocking DBI methods.

restore_all()

Restores all overridden DBI methods to their original implementations.

This method is used to revert all DBI method overrides set up for mocking database interactions back to their original implementations.

Returns the current object.

PRIVATE METHODS

These methods are not intended to be called directly from outside the module.

_initialize(\%args_for)

Initializes the DBD::Mock::Session::GenerateFixtures object with the provided arguments.

_set_mock_dbh(\@data)

Sets up the mocked database handle based on the provided data.

_override_dbi_methods()

Overrides various DBI methods for mocking database interactions.

_override_dbi_execute($dbi_execute)

Overrides the execute method of DBI::st in order to capture the sql statement, bound_params and column names.

_override_dbi_bind_param($bind_param)

Overrides the bind_param method of DBI::st in order to capture the bound params.

_override_dbi_fetchrow_hashref($fetchrow_hashref)

Overrides the fetchrow_hashref method of DBI::st in order to capture the rows returned.

_override_dbi_fetchrow_arrayref($fetchrow_arrayref)

Overrides the fetchrow_arrayref method of DBI::st in order to capture the rows returned.

_override_dbi_fetchrow_array($fetchrow_array)

Overrides the fetchrow_array method of DBI::st in order to capture the rows returned.

_override_dbi_selectall_arrayref($selectall_arrayref)

Overrides the selectall_arrayref method of DBI::db in order to capture the rows returned.

_override_dbi_selectall_hashref($selectall_hashref)

Overrides the selectall_hashref method of DBI::db in order to capture the rows returned.

_override_dbi_selectcol_arrayref($selectcol_arrayref)

Overrides the selectcol_arrayref method of DBI::db in order to capture the rows returned.

_override_dbi_selectrow_array($selectrow_array)

Overrides the selectrow_array method of DBI::db in order to capture the rows returned.

_override_dbi_selectrow_arrayref($selectrow_arrayref)

Overrides the selectrow_arrayref method of DBI::db in order to capture the rows returned.

_override_dbi_selectrow_hashref($selectrow_hashref)

Overrides the selectrow_hashref method of DBI::db in order to capture the rows returned.

_override_dbi_fecth($sth, @args)

Overrides the fetch method of DBI::st

_override_dbi_prepare

_override_dbi_prepare($prepare);

This method overrides the `DBI::db::prepare` method. It customizes how SQL statements are prepared for execution

_override_dbi_prepare_cached

_override_dbi_prepare_cached($prepare_cached);

This method overrides the `DBI::db::prepare_cached` method. It provides a mechanism for caching prepared statements to optimize repeated queries.

_override_dbi_mocked_prepare

_override_dbi_mocked_prepare($mocked_prepare);

This method overrides the `DBD::Mock::db::prepare` method. It is used for testing purposes to mock the behavior of statement preparation.

_normalize_sql

_normalize_sql($sql);

This method normalizes an SQL query string by removing extra whitespace and trimming leading or trailing spaces.

_get_current_record_column_names()

Returns the column names of the current record being processed.

_process_mock_data(\@data)

Processes the mock data before setting up the mocked database handle.

_set_fixtures_file($file)

Sets the file path for the fixture file containing mocked data.

_validate_args(\%args_for)

Validates the arguments passed to the constructor.

_write_to_file()

Writes the current results to the fixture file if override flag is set.

_set_hashref_response($sth, $retval)

Sets the response for hash references fetched from the database.

Support

Bugs should be reported via the CPAN bug tracker at

https://rt.cpan.org/Public/Bug/Report.html?Queue=DBD-Mock-Session-GenerateFixtures

For other issues, contact the author.

REPOSITORY

DBD-Fixtures

AUTHOR

Dragos Trif <drd.trif@gmail.com>

LICENSE

This library is free software and may be distributed under the same terms as perl itself.