Test::DBIx::Class::Schema - DBIx::Class schema sanity checking tests
version 1.0.12
Create a test script that looks like this:
#!/usr/bin/perl # vim: ts=8 sts=4 et sw=4 sr sta use strict; use warnings; # load the module that provides all of the common test functionality use Test::DBIx::Class::Schema; # create a new test object my $schematest = Test::DBIx::Class::Schema->new( { # required dsn => 'dbi:Pg:dbname=mydb', # or use schema option namespace => 'MyDB::Schema', moniker => 'SomeTable', # optional username => 'some_user', password => 'opensesame', glue => 'Result', # fix class name if needed # rather than calling diag will test that all columns/relationships # are accounted for in your test and fail the test if not test_missing => 1, } ); # tell it what to test $schematest->methods( { columns => [ qw[ id column1 column2 columnX foo_id ] ], relations => [ qw[ foo ] ], custom => [ qw[ some_method ] ], resultsets => [ qw[ ] ], } ); # run the tests $schematest->run_tests();
Run the test script:
prove -l t/schematest/xx.mydb.t
Either dsn (eg dbi:Pg:dbname=mydb) or schema (an already created schema object) must be set.
dsn
dbi:Pg:dbname=mydb
schema
If the database requires credentials, set username and password.
username
password
namespace, glue and moniker define the class being tested. For example, if your class is MyDB::Schema::Result::SomeTable then use:
namespace
glue
moniker
MyDB::Schema::Result::SomeTable
namespace => 'MyDB::Schema', glue => 'Result, moniker => 'SomeTable',
glue is not required if the combination of namespace and moniker is enough to define the class, e.g. MyDB::Schema::SomeTable.
MyDB::Schema::SomeTable
Under normal circumstances there is no need to add done_testing to your test script; it's automatically called at the end of run_tests() unless you are running tests under Test::Aggregate.
done_testing
run_tests()
If you are running aggregated tests you will need to add
done_testing;
to your top-level script.
If you are running under Test::Class::Moose or Test::Class you will need to disable this behaviour manually as there is no way to detect it. To do that, set $ENV{TEST_AGGREGATE} = 1 before calling run_tests or your test suite might blow up.
$ENV{TEST_AGGREGATE} = 1
run_tests
It's really useful to be able to test and confirm that DBIC classes have and support a known set of methods.
Testing these one-by-one is more than tedious and likely to discourage you from writing the relevant test scripts.
As a lazy person myself I don't want to write numerous near-identical scripts.
Test::DBIx::Class::Schema takes the copy-and-paste out of DBIC schema class testing.
DBIx::Class, Test::More, Test::Aggregate
Chisel Wright <chisel@chizography.net>
This software is copyright (c) 2017 by Chisel Wright.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Darius Jokilehto <darius.jokilehto@net-a-porter.com>
Dave Cross <davidc@broadbean.com>
Jason Tang <tang.jason.ch@gmail.com>
Rupert Lane <rupert@rupert-lane.org>
simbabque <simbabque@cpan.org>
To install Test::DBIx::Class::Schema, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::DBIx::Class::Schema
CPAN shell
perl -MCPAN -e shell install Test::DBIx::Class::Schema
For more information on module installation, please visit the detailed CPAN module installation guide.