File: 09-handle-dsn.t

package info (click to toggle)
libtest-database-perl 1.112-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 272 kB
  • ctags: 83
  • sloc: perl: 637; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,110 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use Test::More;
use Test::Database::Handle;

use DBI;
use File::Spec;
use File::Temp qw( tempdir );

my $dir = tempdir( CLEANUP => 1 );
my $db = File::Spec->catfile( $dir, 'db.sqlite' );

my $dsn = "dbi:SQLite:$db";
my $dbh;
eval { $dbh = DBI->connect($dsn) }
    or plan skip_all => 'DBD::SQLite needed for this test';

# some SQL statements to try out
my @sql = (
    q{CREATE TABLE users (id INTEGER, name VARCHAR(64))},
    q{INSERT INTO users (id, name) VALUES (1, 'book')},
    q{INSERT INTO users (id, name) VALUES (2, 'echo')},
);
my $select = "SELECT id, name FROM users";

plan tests => @sql + 4;

# create some information
ok( $dbh->do($_), $_ ) for @sql;

# create handle
my $handle = Test::Database::Handle->new( dsn => $dsn );

is_deeply(
    [ $handle->connection_info() ],
    [ $dsn, undef, undef ],
    'connection_info()'
);
isa_ok( my $dbh2 = $handle->dbh(), 'DBI::db' );
cmp_ok( $handle->dbh(), 'eq', $dbh2, 'cached dbh' );

# check the data is there
my $lines = $dbh->selectall_arrayref($select);
is_deeply( $lines, [ [ 1, 'book' ], [ 2, 'echo' ] ], $select );