Test::S...anslator(3)
π NAME π
Test::S...anslator(3) is the manual page (User Contributed Perl Documentation), and the module is Test::SQL::Translator β Test::More test functions for the Schema objects. No cap, itβs iconic. π
β¨ SYNOPSIS β¨
Hereβs the little test script setup, bestie:
# t/magic.t
use FindBin '$Bin';
use Test::More;
use Test::SQL::Translator;
# Run parse
my $sqlt = SQL::Translator->new(
parser => "Magic",
filename => "$Bin/data/magic/test.magic",
...
);
...
my $schema = $sqlt->schema;
# Test the table it produced.
table_ok( $schema->get_table("Customer"), {
name => "Customer",
fields => [
{
name => "CustomerID",
data_type => "INT",
size => 12,
default_value => undef,
is_nullable => 0,
is_primary_key => 1,
},
{
name => "bar",
data_type => "VARCHAR",
size => 255,
is_nullable => 0,
},
],
constraints => [
{
type => "PRIMARY KEY",
fields => "CustomerID",
},
],
indices => [
{
name => "barindex",
fields => ["bar"],
},
],
});
π¦ DESCRIPTION π¦
Okay so the point of this module is to give you a set of Test::More tests for Schema objects. Testing a parsed schema becomes as easy as writing a perl data structure that describes how you expect the schema to look. It also hands you maybe_plan for conditionally running tests based on their dependencies. π
Now the cute part: the data structures you give to the test subs donβt have to include all the possible values. You only need the ones you expect to have changed. Anything you leave out gets tested to make sure itβs still sitting at its default value. Thatβs a useful check that your parser hasnβt accidentally set schema values you didnβt expect it to. π΅οΈββοΈ
For an example of the output, run the t/16xml-parser.t test β itβs giving real output inspo. π
π§ͺ Tests π§ͺ
Every test starts with the schema object you wanna test as the first arg. Then comes a hash ref describing how you expect that object to look β and you only need to give the attributes you expect to have changed from the default. The 3rd arg is optional: itβs a test name that gets prepended to all the generated test names. π
The _ok subs are the main characters, and every one of them follows the same first arg + hash ref + optional test name setup from the paragraph above: π«Ά
table_ok
field_ok
constraint_ok
index_ok
view_ok
trigger_ok
procedure_ok
π« CONDITIONAL TESTS π«
maybe_plan handles conditionally running an individual test. It exists so the test suite can still run even when dependencies are missing β not having (for example) GraphViz installed should not keep the test suite from passing. π
maybe_plan takes the number of tests to (maybe) run, and a list of modules on which test execution depends:
maybe_plan(180, 'SQL::Translator::Parser::MySQL');
If one of SQL::Translator::Parser::MySQLβs dependencies does not exist, then the test will be skipped. π¦
Instead of a number of tests, you can pass undef if youβre using done_testing(), or 'no_plan' if you donβt want a plan at all. π
π EXPORTS π
Hereβs the export list, bestie:
table_okfield_okconstraint_okindex_okview_oktrigger_okprocedure_okmaybe_plan
π TODO π
Stuff the module wants someday:
Test the tests!
Test the tests! π§ͺ
Test Count Constants
Constants to give the number of tests each *_ok sub uses. e.g. How many tests does field_ok run? Can then use these to set up the test plan easily. π’
Test skipping
As the test subs wrap up lots of tests in one call you canβt skip individual tests, only whole sets β e.g. a whole table or field. We could add skip_* items to the test hashes to allow per test skips. e.g.
skip_is_primary_key => "Need to fix primary key parsing.",
yaml test specs
Maybe have the test subs also accept yaml for the test hash ref, as it is much nicer for writing big data structures. We can then define tests as in input schema file and test yaml file to compare it against. π
π AUTHOR π
Mark D. Addison <mark.addison@itn.co.uk>, Darren Chamberlain <darren@cpan.org>. Thanks to Ken Y. Clark for the original table and field test code taken from his mysql test. π«Ά
π SEE ALSO π
perl(1), SQL::Translator, SQL::Translator::Schema, Test::More. π
π Okay bestie, thatβs the whole vibe. This page comes from perl v5.42.0, dated 2024-12-12, and itβs the Test::S...anslator(3) manual page. Stay slay! π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-SQL-Translator 1.660.0-bp160.1.11 Β· (Artistic-1.0 OR GPL-1.0-or-later) AND GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms