DBIx-ThinSQL-SQLite

 view release on metacpan or  search on metacpan

t/DBIx/ThinSQL/SQLite.t  view on Meta::CPAN

use strict;
use warnings;
use Log::Any::Test;
use DBIx::ThinSQL;
use DBIx::ThinSQL::SQLite
  qw/create_sqlite_sequence create_functions create_methods/;
use File::chdir;
use Log::Any '$log';
use Path::Tiny;
use Test::Fatal qw/exception/;
use Test::More;

sub run_in_tempdir (&) {
    my $sub = shift;
    my $cwd = $CWD;
    my $tmp = Path::Tiny->tempdir( CLEANUP => 1 );

    local $CWD = $tmp;
    $sub->();

    $CWD = $cwd;
}

subtest "create_functions", sub {
    isa_ok \&create_functions, 'CODE';

    run_in_tempdir {

        my $db = DBIx::ThinSQL->connect( 'dbi:SQLite:dbname=test.sqlite3',
            undef, undef, { RaiseError => 1, PrintError => 0 } );

        my @funcs = (
            qw/debug create_sequence currval nextval sha1
              sha1_hex sha1_base64 agg_sha1 agg_sha1_hex agg_sha1_base64/
        );
        foreach my $func (@funcs) {
            ok exception { $db->do("select $func()") },
              "no existing func $func";
        }

        like exception { create_functions() }, qr/usage:/, 'usage';

        like exception { create_functions( 1, 2 ) },
          qr/handle has no sqlite_create_function/,
          'usage no handle';

        like exception { create_functions( $db, 'unknown' ) },
          qr/unknown function/,
          'unknown function';

        create_functions( $db, qw/debug/ );

        my $str = 'RaNdOm';    # just a random string
        $db->do("select debug('$str', '$str', 1)");
        $log->contains_ok( qr/$str $str 1/, 'debug logged all args' );

        $db->do(q{select debug("select ? || ? || ?", 'lazy','fox','jump')});
        $log->contains_ok( qr/lazyfoxjump/, 'debug select with bind values' );

        $db->do(
            q{select debug("
            select 1 || 2 || 1 || 4")}
        );
        $log->contains_ok( qr/1214/, 'debug select with leading space' );

        create_functions( $db, qw/create_sequence currval nextval/ );

        # manually create sqlite_sequence
        $db->do('create table x(id integer primary key autoincrement)');
        $db->do('drop table x');



( run in 1.234 second using v1.01-cache-2.11-cpan-54e63673c56 )