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