1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
use Mojo::Base -strict;
use Test::More;
use DBD::SQLite::Constants ':dbd_sqlite_string_mode';
use Mojo::SQLite;
use URI::file;
subtest 'Defaults' => sub {
my $sql = Mojo::SQLite->new;
like $sql->dsn, qr/^dbi:SQLite:dbname=/, 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 0,
RaiseError => 1,
sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Minimal connection string with file' => sub {
my $sql = Mojo::SQLite->new('test.db');
is $sql->dsn, 'dbi:SQLite:dbname=test.db', 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 0,
RaiseError => 1,
sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Minimal connection string with in-memory database and option' => sub {
my $sql = Mojo::SQLite->new('sqlite::memory:?PrintError=1');
is $sql->dsn, 'dbi:SQLite:dbname=:memory:', 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 1,
RaiseError => 1,
sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Connection string with absolute filename and options' => sub {
my $uri = URI::file->new('/tmp/sqlite.db?#', 'unix')
->Mojo::Base::tap(query_form => {PrintError => 1, RaiseError => 0});
my $sql;
{
# Force unix interpretation
local %URI::file::OS_CLASS = ();
$sql = Mojo::SQLite->new($uri);
}
is $sql->dsn, 'dbi:SQLite:dbname=/tmp/sqlite.db?#', 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 1,
RaiseError => 0,
sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Connection string with lots of zeros' => sub {
my $sql = Mojo::SQLite->new('0?RaiseError=0');
is $sql->dsn, 'dbi:SQLite:dbname=0', 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 0,
RaiseError => 0,
sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Parse filename' => sub {
my $sql = Mojo::SQLite->new->from_filename('/foo#/bar?.db', {PrintError => 1});
is $sql->dsn, 'dbi:SQLite:dbname=/foo#/bar?.db', 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 1,
RaiseError => 1,
sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Invalid connection string' => sub {
eval { Mojo::SQLite->new('http://localhost:3000/test') };
like $@, qr/Invalid SQLite connection string/, 'right error';
};
subtest 'Legacy sqlite_unicode enabled' => sub {
my $sql = Mojo::SQLite->new('test.db?sqlite_unicode=1');
like $sql->dsn, qr/^dbi:SQLite:dbname=/, 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 0,
RaiseError => 1,
sqlite_unicode => 1,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
subtest 'Legacy sqlite_unicode disabled' => sub {
my $sql = Mojo::SQLite->new('test.db?sqlite_unicode=0');
like $sql->dsn, qr/^dbi:SQLite:dbname=/, 'right data source';
my $options = {
AutoCommit => 1,
AutoInactiveDestroy => 1,
PrintError => 0,
RaiseError => 1,
sqlite_unicode => 0,
wal_mode => 1,
};
is_deeply $sql->options, $options, 'right options';
};
done_testing();
|