The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Environment::Plugin::PostgreSQL - PostreSQL psql function for testing

SYNOPSIS

        use Test::Environment qw{
                PostgreSQL
        };
        
        # set database credentials
        psql(
                'database' => $config->{'db'}->{'database'},
                'hostname' => $config->{'db'}->{'hostname'},
                'username' => $config->{'db'}->{'username'},
                'password' => $config->{'db'}->{'password'},
                # or skip hostname and database and set them via
                #'dbi_dsn' => 'dbi:Pg:dbname=dsn_test;host=localhost',

        );
        
        # execute sql query
        my @output = psql(
                'switches' => '--expanded',
                'command'  => 'SELECT * FROM Table',
                # ..., see psql function description for more
        )

DESCRIPTION

This plugin will export 'psql' function that can be used to execute PostreSQL psql command with lot of options for testing.

Module will prepare %ENV for postgres:

        'username' => 'PGUSER',
        'password' => 'PGPASSWORD',
        'database' => 'PGDATABASE',
        'hostname' => 'PGHOST',
        'port'     => 'PGPORT',

Any postgres connection settings not listed or undef will be deleted from the %ENV hash.

FUNCTIONS

import

All functions are exported 2 levels up. That is to the use Test::Environment caller.

psql()

psql command executed easily. Here is the list of options that can be used.

Option related to the connection to the database.

        username
        password
        database
        hostname
        port

By setting this PostreSQL %ENV variables will be set. So for psql command to the same databse you need to set them only once.

The rest of the option related to the psql command.

        command         - scalar or array ref of sql commands
        switches        - scalar or array of additional psql switches
        output_filename - the output will be written to this file (-o)
        execution_path  - before executing psql change to that folder
        stderr_redirect - will redirect stderr to stdout so that also error appears in the return value
        debug           - turn on debug mode, it can be also done globaly by setting "$ENV{'IN_DEBUG_MODE'} = 1" 

execute

Executes command as system and return output.

In debug mode prints command to stderr.

SEE ALSO

Test::Environment http://search.cpan.org/perldoc?Test::Environment

AUTHOR

Jozef Kutej - <jozef@kutej.net>