pod =head3 Arguments stuff
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Test / SQLite.pm
1 package DBIx::Class::Test::SQLite;
2
3 =head1 NAME
4
5 DBIx::Class::Test::SQLite - Base class for running Class::DBI tests against DBIx::Class compat layer, shamelessly ripped from Class::DBI::Test::SQLite
6
7 =head1 SYNOPSIS
8
9         use base 'DBIx::Class::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 DBIx::Class::CDBICompat tests using
26 SQLite.  Each class for the test should inherit from this, provide a
27 create_sql() method which returns a string representing the SQL used to
28 create the table for the class, and then call set_table() to create the
29 table, and tie it to the class.
30
31 =cut
32
33 use strict;
34
35 use base qw/DBIx::Class/;
36
37 __PACKAGE__->load_components(qw/PK::Auto::SQLite CDBICompat Core DB/);
38
39 use File::Temp qw/tempfile/;
40 my (undef, $DB) = tempfile();
41 END { unlink $DB if -e $DB }
42
43 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1, RaiseError => 1 });
44
45 __PACKAGE__->connection(@DSN);
46 __PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)');
47 __PACKAGE__->set_sql(_create_me    => 'CREATE TABLE __TABLE__ (%s)');
48 __PACKAGE__->storage->dbh->do("PRAGMA synchronous = OFF");
49
50 =head1 METHODS
51
52 =head2 set_table
53
54         __PACKAGE__->set_table('test');
55
56 This combines creating the table with the normal DBIx::Class table()
57 call.
58
59 =cut
60
61 sub set_table {
62         my ($class, $table) = @_;
63         $class->table($table);
64         $class->_create_test_table;
65 }
66
67 sub _create_test_table {
68         my $class = shift;
69         my @vals  = $class->sql__table_pragma->select_row;
70         $class->sql__create_me($class->create_sql)->execute unless @vals;
71 }
72
73 =head2 create_sql
74
75 This is an abstract method you must override.
76
77         sub create_sql { 
78                 return q{
79                         id     INTEGER PRIMARY KEY,
80                         name   CHAR(40),
81                         film   VARCHAR(255),
82                         salary INT
83                 }
84         }
85
86 This should return, as a text string, the schema for the table represented
87 by this class.
88
89 =cut
90
91 sub create_sql { die "create_sql() not implemented by $_[0]\n" }
92
93 1;