Introducing DBIx::Class::Schema::SanityChecker
[dbsrgits/DBIx-Class.git] / t / cdbi / testlib / DBIC / Test / SQLite.pm
1 package # hide from PAUSE
2     DBIC::Test::SQLite;
3
4 use strict;
5 use warnings;
6
7 =head1 NAME
8
9 DBIx::Class::Test::SQLite - Base class for running Class::DBI tests against DBIx::Class compat layer, shamelessly ripped from Class::DBI::Test::SQLite
10
11 =head1 SYNOPSIS
12
13   use base 'DBIx::Class::Test::SQLite';
14
15   __PACKAGE__->set_table('test');
16   __PACKAGE__->columns(All => qw/id name film salary/);
17
18   sub create_sql {
19       return q{
20           id     INTEGER PRIMARY KEY,
21           name   CHAR(40),
22           film   VARCHAR(255),
23           salary INT
24       }
25   }
26
27 =head1 DESCRIPTION
28
29 This provides a simple base class for DBIx::Class::CDBICompat tests using
30 SQLite.  Each class for the test should inherit from this, provide a
31 create_sql() method which returns a string representing the SQL used to
32 create the table for the class, and then call set_table() to create the
33 table, and tie it to the class.
34
35 =cut
36
37 # adding implicit search criteria to the iterator will alter the test
38 # mechanics - leave everything as-is instead, and hope SQLite won't
39 # change too much
40 BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
41
42 use DBICTest;
43
44 use base qw/DBIx::Class/;
45
46 BEGIN {
47   # offset the warning from DBIx::Class::Schema on 5.8
48   # keep the ::Schema default as-is otherwise
49   DBIx::Class::_ENV_::OLD_MRO
50     and
51   ( eval <<'EOS' or die $@ );
52
53   sub setup_schema_instance {
54     my $s = shift->next::method(@_);
55     $s->schema_sanity_checker('');
56     $s;
57   }
58
59   1;
60 EOS
61 }
62
63 __PACKAGE__->load_components(qw/CDBICompat Core DB/);
64
65 my $DB = DBICTest->_sqlite_dbfilename;
66 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1, RaiseError => 1 });
67
68 __PACKAGE__->connection(@DSN);
69 __PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)');
70 __PACKAGE__->set_sql(_create_me    => 'CREATE TABLE __TABLE__ (%s)');
71 __PACKAGE__->storage->dbh->do("PRAGMA synchronous = OFF");
72
73 =head1 METHODS
74
75 =head2 set_table
76
77     __PACKAGE__->set_table('test');
78
79 This combines creating the table with the normal DBIx::Class table()
80 call.
81
82 =cut
83
84 sub set_table {
85     my ($class, $table) = @_;
86     $class->table($table);
87     $class->_create_test_table;
88 }
89
90 sub _create_test_table {
91     my $class = shift;
92     my @vals  = $class->sql__table_pragma->select_row;
93     $class->sql__create_me($class->create_sql)->execute unless @vals;
94 }
95
96 =head2 create_sql
97
98 This is an abstract method you must override.
99
100   sub create_sql {
101       return q{
102           id     INTEGER PRIMARY KEY,
103           name   CHAR(40),
104           film   VARCHAR(255),
105           salary INT
106       }
107   }
108
109 This should return, as a text string, the schema for the table represented
110 by this class.
111
112 =cut
113
114 sub create_sql { die "create_sql() not implemented by $_[0]\n" }
115
116 1;