Reorganize CDBICompat tests - centralize prereq checks in one place
[dbsrgits/DBIx-Class.git] / t / cdbi / testlib / DBIC / Test / SQLite.pm
1 package # hide from PAUSE
2     DBIC::Test::SQLite;
3
4 =head1 NAME
5
6 DBIx::Class::Test::SQLite - Base class for running Class::DBI tests against DBIx::Class compat layer, shamelessly ripped from Class::DBI::Test::SQLite
7
8 =head1 SYNOPSIS
9
10   use base 'DBIx::Class::Test::SQLite';
11
12   __PACKAGE__->set_table('test');
13   __PACKAGE__->columns(All => qw/id name film salary/);
14
15   sub create_sql {
16       return q{
17           id     INTEGER PRIMARY KEY,
18           name   CHAR(40),
19           film   VARCHAR(255),
20           salary INT
21       }
22   }
23
24 =head1 DESCRIPTION
25
26 This provides a simple base class for DBIx::Class::CDBICompat tests using
27 SQLite.  Each class for the test should inherit from this, provide a
28 create_sql() method which returns a string representing the SQL used to
29 create the table for the class, and then call set_table() to create the
30 table, and tie it to the class.
31
32 =cut
33
34 use strict;
35 use warnings;
36
37 use Test::More;
38
39 use lib 't/lib';
40 use DBICTest;
41
42 BEGIN {
43   eval { require DBIx::Class::CDBICompat }
44     or plan skip_all => 'Class::DBI required for this test';
45 }
46
47 use base qw/DBIx::Class/;
48
49 __PACKAGE__->load_components(qw/CDBICompat Core DB/);
50
51 use File::Temp qw/tempfile/;
52 my (undef, $DB) = tempfile();
53 END { unlink $DB if -e $DB }
54
55 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1, RaiseError => 1 });
56
57 __PACKAGE__->connection(@DSN);
58 __PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)');
59 __PACKAGE__->set_sql(_create_me    => 'CREATE TABLE __TABLE__ (%s)');
60 __PACKAGE__->storage->dbh->do("PRAGMA synchronous = OFF");
61
62 =head1 METHODS
63
64 =head2 set_table
65
66     __PACKAGE__->set_table('test');
67
68 This combines creating the table with the normal DBIx::Class table()
69 call.
70
71 =cut
72
73 sub set_table {
74     my ($class, $table) = @_;
75     $class->table($table);
76     $class->_create_test_table;
77 }
78
79 sub _create_test_table {
80     my $class = shift;
81     my @vals  = $class->sql__table_pragma->select_row;
82     $class->sql__create_me($class->create_sql)->execute unless @vals;
83 }
84
85 =head2 create_sql
86
87 This is an abstract method you must override.
88
89   sub create_sql {
90       return q{
91           id     INTEGER PRIMARY KEY,
92           name   CHAR(40),
93           film   VARCHAR(255),
94           salary INT
95       }
96   }
97
98 This should return, as a text string, the schema for the table represented
99 by this class.
100
101 =cut
102
103 sub create_sql { die "create_sql() not implemented by $_[0]\n" }
104
105 1;