DBIx-RunSQL

 view release on metacpan or  search on metacpan

t/02-trigger.t  view on Meta::CPAN

#!perl -w
use strict;
use Test2::V0 '-no_srand';

use DBIx::RunSQL;

my $can_run = eval {
    require DBD::SQLite;
    1
};

if (not $can_run) {
    plan skip_all => "SQLite not installed";
}

my $sql = do { local (@ARGV,$/) = 't/trigger.sql'; <> };
my @statements;
while( defined( my $frag = DBIx::RunSQL->split_sql( $sql ))) {
    push @statements, $frag;
}
is \@statements, [
  '-- This commented-out statement will not get passed through',
  "-- SECRET PRAGMA #foo will get passed through with the next statement\r\n"
  . "create table test (\r\n"
  . "    id integer unique not null,\r\n"
  . "    descr text default '',\r\n"
  . "    ts text\r\n"
  . ")",
    "CREATE TRIGGER trg_test_1 AFTER INSERT ON test\r\n"
  . "     BEGIN\r\n"
  . "      UPDATE test SET ts = DATETIME('NOW')  WHERE rowid = new.rowid;\n"
  . "END",
    "CREATE TRIGGER trg_test_2 AFTER INSERT ON test BEGIN\r\n"
  . "      UPDATE test SET ts = DATETIME('NOW')  WHERE rowid = new.rowid;\n"
  . "END",
  , "-- Comments before triggers don't hide the trigger\r\n"
  . "CREATE TRIGGER trg_test_3 AFTER INSERT ON test BEGIN\r\n"
  . "      UPDATE test SET ts = DATETIME('NOW')  WHERE rowid = new.rowid;\n"
  . "END"
], "We split the statements in the expected fashion";

@statements = ();
my $lives = eval {
    my $test_dbh = DBIx::RunSQL->create(
        dsn     => 'dbi:SQLite:dbname=:memory:',
        sql     => 't/trigger.sql',
        verbose => 1,
        verbose_handler => sub { push @statements, @_ },
    );
    1;
};
my $err = $@;
ok $lives, "We can parse triggers"
    or diag $err;
is 0+@statements, 4, "We also split a file into 4 statements";

done_testing;



( run in 1.205 second using v1.01-cache-2.11-cpan-a9496e3eb41 )