File Coverage

blib/lib/Test/SQLite.pm
Criterion Covered Total %
statement 41 41 100.0
branch 20 24 83.3
condition 15 15 100.0
subroutine 10 10 100.0
pod 0 1 0.0
total 86 91 94.5


line stmt bran cond sub pod time code
1             package Test::SQLite;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: SQLite setup/teardown for tests
5              
6             our $VERSION = '0.0410';
7              
8 1     1   1116 use strictures 2;
  1         1662  
  1         40  
9 1     1   239 use DBI ();
  1         2  
  1         20  
10 1     1   473 use File::Copy qw(copy);
  1         2298  
  1         56  
11 1     1   691 use File::Temp ();
  1         17355  
  1         31  
12 1     1   505 use Moo;
  1         11709  
  1         5  
13 1     1   1970 use namespace::clean;
  1         11391  
  1         6  
14              
15              
16             has database => (
17             is => 'ro',
18             isa => sub { die 'database does not exist' unless -e $_[0] },
19             predicate => 'has_database',
20             );
21              
22              
23             has schema => (
24             is => 'ro',
25             isa => sub { die 'schema does not exist' unless -e $_[0] },
26             predicate => 'has_schema',
27             );
28              
29              
30             has memory => (
31             is => 'ro',
32             predicate => 'has_memory',
33             );
34              
35              
36             has db_attrs => (
37             is => 'ro',
38             default => sub { return { RaiseError => 1, AutoCommit => 1 } },
39             );
40              
41              
42             has dsn => (
43             is => 'lazy',
44             init_arg => undef,
45             );
46              
47             sub _build_dsn {
48 4     4   43 my ($self) = @_;
49 4 100       77 return 'dbi:SQLite:dbname=' . ( $self->has_memory ? $self->_database : $self->_database->filename );
50             }
51              
52              
53             has dbh => (
54             is => 'lazy',
55             init_arg => undef,
56             );
57              
58             sub _build_dbh {
59 4     4   824 my ($self) = @_;
60 4         73 return DBI->connect( $self->dsn, '', '', $self->db_attrs );
61             }
62              
63             has _database => (
64             is => 'lazy',
65             init_arg => undef,
66             );
67              
68             sub _build__database {
69 4     4   88 my ($self) = @_;
70              
71 4 100       38 my $tempfile = $self->has_memory
72             ? ':memory:'
73             : File::Temp->new( unlink => 1, suffix => '.db', EXLOCK => 0 );
74              
75 4 100       1814 if ( $self->has_database ) {
    100          
76 1 50       7 copy( $self->database, $tempfile->filename )
77             or die "Can't copy " . $self->database . ": $!";
78             }
79             elsif ( $self->has_schema ) {
80 1 50       45 open my $schema, '<', $self->schema
81             or die "Can't read " . $self->schema . ": $!";
82              
83 1 50       27 my $dbh = DBI->connect( "dbi:SQLite:dbname=$tempfile", '', '', { RaiseError => 1, AutoCommit => 0 } )
84             or die "Can't connect to $tempfile: " . $DBI::errstr;
85              
86 1         386 my $sql = '';
87 1         33 while ( my $line = readline($schema) ) {
88 18 100       48 next if $line =~ /^\s*--/;
89 17 100       45 next if $line =~ /^\s*$/;
90              
91 14         27 $sql .= $line;
92              
93 14 100       38 if ( $line =~ /;/ ) {
94 3 50       20 $dbh->do($sql)
95             or die 'Error executing SQL for ' . $self->schema . ': ' . $dbh->errstr;
96              
97 3         966 $sql = '';
98             }
99             }
100              
101 1         11572 $dbh->commit;
102              
103 1         153 $dbh->disconnect;
104             }
105              
106 4         541 return $tempfile;
107             }
108              
109              
110             sub BUILD {
111 7     7 0 65 my ( $self, $args ) = @_;
112 7 100 100     149 die 'The schema, database and memory arguments may not be used together.'
      100        
      100        
      100        
      100        
113             if ( $self->has_database and $self->has_schema )
114             or ( $self->has_database and $self->has_memory )
115             or ( $self->has_schema and $self->has_memory );
116             }
117              
118             1;
119              
120             __END__