File Coverage

blib/lib/Class/DBI/Test/SQLite.pm
Criterion Covered Total %
statement 15 16 93.8
branch 1 2 50.0
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 23 26 88.5


line stmt bran cond sub pod time code
1             package Class::DBI::Test::SQLite;
2              
3             =head1 NAME
4            
5             Class::DBI::Test::SQLite - Base class for Class::DBI tests
6            
7             =head1 SYNOPSIS
8            
9             use base 'Class::DBI::Test::SQLite';
10            
11             __PACKAGE__->set_table('test');
12             __PACKAGE__->columns(All => qw/id name film salary/);
13            
14             sub create_sql {
15             return q{
16             id INTEGER PRIMARY KEY,
17             name CHAR(40),
18             film VARCHAR(255),
19             salary INT
20             }
21             }
22            
23             =head1 DESCRIPTION
24            
25             This provides a simple base class for Class::DBI tests using SQLite.
26             Each class for the test should inherit from this, provide a create_sql()
27             method which returns a string representing the SQL used to create the
28             table for the class, and then call set_table() to create the table, and
29             tie it to the class.
30            
31             =cut
32              
33 19     19   424 use strict;
  19         384  
  19         417  
34              
35 19     19   337 use base 'Class::DBI';
  19         169  
  19         403  
36 19     19   776 use File::Temp qw/tempfile/;
  19         193  
  19         476  
37             my (undef, $DB) = tempfile();
38             END { unlink $DB if -e $DB }
39              
40             my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1 });
41              
42             __PACKAGE__->connection(@DSN);
43             __PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)');
44             __PACKAGE__->set_sql(_create_me    => 'CREATE TABLE __TABLE__ (%s)');
45              
46             =head1 METHODS
47            
48             =head2 set_table
49            
50             __PACKAGE__->set_table('test');
51            
52             This combines creating the table with the normal Class::DBI table()
53             call.
54            
55             =cut
56              
57             sub set_table {
58 30     30 1 1086 my ($class, $table) = @_;
59 30         641 $class->table($table);
60 30         542 $class->_create_test_table;
61             }
62              
63             sub _create_test_table {
64 30     30   295 my $class = shift;
65 30         493 my @vals = $class->sql__table_pragma->select_row;
66 30 50       2437 $class->sql__create_me($class->create_sql)->execute unless @vals;
67             }
68              
69             =head2 create_sql (abstract)
70            
71             sub create_sql {
72             return q{
73             id INTEGER PRIMARY KEY,
74             name CHAR(40),
75             film VARCHAR(255),
76             salary INT
77             }
78             }
79            
80             This should return, as a text string, the schema for the table represented
81             by this class.
82            
83             =cut
84              
85 0     0 1   sub create_sql { die "create_sql() not implemented by $_[0]\n" }
86              
87             1;
88