Skip find test rather than marking it TODO
[dbsrgits/DBIx-Class-InflateColumn-IP.git] / t / lib / DBICTest.pm
CommitLineData
3a889a03 1package # hide from PAUSE
2 DBICTest;
3
4use strict;
5use warnings;
6use DBICTest::Schema;
7
8=head1 NAME
9
10DBICTest - Library to be used by DBIx::Class test scripts.
11
12=head1 SYNOPSIS
13
14 use lib qw(t/lib);
15 use DBICTest;
16 use Test::More;
17
18 my $schema = DBICTest->init_schema();
19
20=head1 DESCRIPTION
21
22This module provides the basic utilities to write tests against
23DBIx::Class.
24
25=head1 METHODS
26
27=head2 init_schema
28
29 my $schema = DBICTest->init_schema(
30 no_deploy=>1,
31 no_populate=>1,
32 );
33
34This method removes the test SQLite database in t/var/DBIxClass.db
35and then creates a new, empty database.
36
37This method will call deploy_schema() by default, unless the
38no_deploy flag is set.
39
40Also, by default, this method will call populate_schema() by
41default, unless the no_deploy or no_populate flags are set.
42
43=cut
44
45sub init_schema {
46 my $self = shift;
47 my %args = @_;
48 my $db_file = "t/var/DBIxClass.db";
49
50 unlink($db_file) if -e $db_file;
51 unlink($db_file . "-journal") if -e $db_file . "-journal";
52 mkdir("t/var") unless -d "t/var";
53
54 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
55 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
56 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
57
3baacba6 58 my $schema = DBICTest::Schema->compose_namespace('DBICTest')
59 ->connect($dsn, $dbuser, $dbpass);
3a889a03 60 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
61 if ( !$args{no_deploy} ) {
62 __PACKAGE__->deploy_schema( $schema );
63 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
64 }
65 return $schema;
66}
67
68=head2 deploy_schema
69
70 DBICTest->deploy_schema( $schema );
71
72This method does one of two things to the schema. It can either call
73the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
74variable is set, otherwise the default is to read in the t/lib/sqlite.sql
75file and execute the SQL within. Either way you end up with a fresh set
76of tables for testing.
77
78=cut
79
80sub deploy_schema {
81 my $self = shift;
82 my $schema = shift;
83
84 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
85 return $schema->deploy();
86 } else {
87 open IN, "t/lib/sqlite.sql";
88 my $sql;
89 { local $/ = undef; $sql = <IN>; }
90 close IN;
91 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
92 }
93}
94
95=head2 populate_schema
96
97 DBICTest->populate_schema( $schema );
98
99After you deploy your schema you can use this method to populate
100the tables with test data.
101
102=cut
103
104sub populate_schema {
105 my $self = shift;
106 my $schema = shift;
107
108 $schema->populate('Host', [
109 [ qw/hostname address/ ],
110 [ 'localhost', 2130706433 ],
111 ]);
112
113 $schema->populate('Network', [
114 [ qw/netname address/ ],
115 [ qw{localnet 127.0.0.0/8} ],
116 ]);
117}
118
1191;