NAME¶
LedgerSMB::DBTest - LedgerSMB commit filter for test cases.
SYOPSIS¶
This module creates a DBI-like interface but ensures autocommit is off, and
filters commit statements such that they don't do anything. This can be used
for making API test cases which involve DB commits safe for production
environments.
USAGE¶
Both LedgerSMB.pm and LedgerSMB/Form.pm assign a global database handler for all
database access within a script in the dbh property (for example,
$request->{dbh} or $form->{dbh}). By setting this early to a
LedgerSMB::DBTest (instead of a DBI object), the tests can be made safe.
However, there are a few limitations to be aware of. One cannot run tests
through the standard request handler and use this module. Hence this is
limited to unit tests of files in the LedgerSMB, scripts, and bin directories.
Here is an example of how this could be done:
my $lsmb = LedgerSMB->new();
$lsmb->merge($testdata);
my $dbh = LedgerSMB::DBTest->connect("dbi:Pg:dbname=$company", "$username",
"$password",)
$lsmb->{dbh} = $dbh;
METHODS¶
- connect($dsn, $user, $pass)
- Connects to the database and returns a LedgerSMB::DBTest
object
- commit()
- Tests the current transaction (issues a 'SELECT 1;' to the
database). If this is successful returns 1, if not, rolls back and returns
false.
Note that this means all past tests are rolled back and this is inconsistent
with normal transactional behavior.
- prepare()
- Returns a statement handle, via the private DBI database
handle.
- do()
- passes this statement on to the private database
handle
- errstr()
- passes this call on to the private database handle
- err()
- passes this call on to the private database handle
- quote()
- passes this call on to the private database handle
- quote_identifier()
- passes this call on to the private database handle
- rollback()
- passes this call on to the private database handle. Note
that this will roll back all statements issues through this object.