e17c40810af7eb897bec6fe869968272b9a72bbc
[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 # adding implicit search criteria to the iterator will alter the test
40 # mechanics - leave everything as-is instead, and hope SQLite won't
41 # change too much
42 BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
43
44 BEGIN {
45   require DBIx::Class::Optional::Dependencies;
46   DBIx::Class::Optional::Dependencies->req_ok_for($_)
47     or plan skip_all => DBIx::Class::Optional::Dependencies->req_missing_for($_) . ' required for this test'
48       for qw(cdbicompat test_cdbicompat);
49 }
50
51 use lib 't/lib';
52 use DBICTest;
53
54 use base qw/DBIx::Class/;
55
56 __PACKAGE__->load_components(qw/CDBICompat Core DB/);
57
58 my $DB = DBICTest->_sqlite_dbfilename;
59 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1, RaiseError => 1 });
60
61 __PACKAGE__->connection(@DSN);
62 __PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)');
63 __PACKAGE__->set_sql(_create_me    => 'CREATE TABLE __TABLE__ (%s)');
64 __PACKAGE__->storage->dbh->do("PRAGMA synchronous = OFF");
65
66 =head1 METHODS
67
68 =head2 set_table
69
70     __PACKAGE__->set_table('test');
71
72 This combines creating the table with the normal DBIx::Class table()
73 call.
74
75 =cut
76
77 sub set_table {
78     my ($class, $table) = @_;
79     $class->table($table);
80     $class->_create_test_table;
81 }
82
83 sub _create_test_table {
84     my $class = shift;
85     my @vals  = $class->sql__table_pragma->select_row;
86     $class->sql__create_me($class->create_sql)->execute unless @vals;
87 }
88
89 =head2 create_sql
90
91 This is an abstract method you must override.
92
93   sub create_sql {
94       return q{
95           id     INTEGER PRIMARY KEY,
96           name   CHAR(40),
97           film   VARCHAR(255),
98           salary INT
99       }
100   }
101
102 This should return, as a text string, the schema for the table represented
103 by this class.
104
105 =cut
106
107 sub create_sql { die "create_sql() not implemented by $_[0]\n" }
108
109 1;