-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
280 lines (160 loc) · 6.31 KB
/
README
File metadata and controls
280 lines (160 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
NAME
Test::DB - Temporary Testing Databases
ABSTRACT
Temporary Databases for Testing
SYNOPSIS
use Test::DB;
my $tdb = Test::DB->new;
# my $tdbo = $tdb->create(database => 'sqlite');
# my $dbh = $tdbo->dbh;
DESCRIPTION
This package provides a framework for setting up and tearing down
temporary databases for testing purposes. This framework requires a
user (optionally with password) which has the ability to create new
databases and works by creating test-specific databases owned by the
user specified. Note: Test databases are not automatically destroyed
and should be cleaned up manually by call the destroy method on the
database-specific test database object.
process
on create, clone
#1
Establish a connection to the DB using some "initial" database.
my $tdbo = $tdb->postgres(initial => 'template0');
#2
Using the established connection, create the test/temporary database.
$tdbo->create;
#3
Establish a connection to the newly created test/temporary database.
$tdbo->create->dbh;
#4
Make the test database object immutable.
$tdbo->create->database('example'); # error
on destroy
#1
Establish a connection to the DB using the "initial" database.
# using the created test/temporary database object
#2
Using the established connection, drop the test/temporary database.
$tdbo->destroy;
usages
using DBI
#1
my $tdb = Test::DB->new;
my $dbh = $tdb->sqlite(%options)->dbh;
using DBIx::Class
#1
my $tdb = Test::DB->new;
my $tdbo = $tdb->postgres(%options)->create;
my $schema = DBIx::Class::Schema->connect(
dsn => $tdbo->dsn,
username => $tdbo->username,
password => $tdbo->password,
);
using Mojo::mysql
#1
my $tdb = Test::DB->new;
my $tdbo = $tdb->mysql(%options)->create;
my $mysql = Mojo::mysql->new($tdbo->uri);
using Mojo::Pg
#1
my $tdb = Test::DB->new;
my $tdbo = $tdb->postgres(%options)->create;
my $postgres = Mojo::Pg->new($tdbo->uri);
using Mojo::Pg (with cloning)
#1
my $tdb = Test::DB->new;
my $tdbo = $tdb->postgres(%options)->clone('template0');
my $postgres = Mojo::Pg->new($tdbo->uri);
using Mojo::SQLite
#1
my $tdb = Test::DB->new;
my $tdbo = $tdb->sqlite(%options)->create;
my $sqlite = Mojo::SQLite->new($tdbo->uri);
LIBRARIES
This package uses type constraints from:
Types::Standard
METHODS
This package implements the following methods:
clone
clone(Str :$database, Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The clone method generates a database based on the type and database
template specified and returns a Test::DB::Object with an active
connection, dbh and dsn. If the database specified doesn't have a
corresponding database driver this method will returned the undefined
value. The type of database can be omitted if the TESTDB_DATABASE
environment variable is set, if not the type of database must be either
sqlite, mysql, mssql or postgres. Any options provided are passed along
to the test database object class constructor.
clone example #1
# given: synopsis
$ENV{TESTDB_DATABASE} = 'postgres';
$tdb->clone(template => 'template0');
clone example #2
# given: synopsis
$ENV{TESTDB_DATABASE} = 'postgres';
$ENV{TESTDB_TEMPLATE} = 'template0';
$tdb->clone;
clone example #3
# given: synopsis
$ENV{TESTDB_TEMPLATE} = 'template0';
$tdb->clone(database => 'postgres');
create
create(Str :$database, Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The create method generates a database based on the type specified and
returns a Test::DB::Object with an active connection, dbh and dsn. If
the database specified doesn't have a corresponding database driver
this method will returned the undefined value. The type of database can
be omitted if the TESTDB_DATABASE environment variable is set, if not
the type of database must be either sqlite, mysql, mssql or postgres.
Any options provided are passed along to the test database object class
constructor.
create example #1
# given: synopsis
$tdb->create;
create example #2
# given: synopsis
$ENV{TESTDB_DATABASE} = 'sqlite';
$tdb->create;
create example #3
# given: synopsis
$tdb->create(database => 'sqlite');
mssql
mssql(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The mssql method builds and returns a Test::DB::Mssql object.
mssql example #1
# given: synopsis
$tdb->mssql;
mysql
mysql(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The mysql method builds and returns a Test::DB::Mysql object.
mysql example #1
# given: synopsis
$tdb->mysql;
postgres
postgres(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The postgres method builds and returns a Test::DB::Postgres object.
postgres example #1
# given: synopsis
$tdb->postgres;
sqlite
sqlite(Str %options) : Maybe[InstanceOf["Test::DB::Object"]]
The sqlite method builds and returns a Test::DB::Sqlite object.
sqlite example #1
# given: synopsis
$tdb->sqlite;
AUTHOR
Al Newkirk, awncorp@cpan.org
LICENSE
Copyright (C) 2011-2019, Al Newkirk, et al.
This is free software; you can redistribute it and/or modify it under
the terms of the The Apache License, Version 2.0, as elucidated in the
"license file"
<https://github.com/iamalnewkirk/test-db/blob/master/LICENSE>.
PROJECT
Wiki <https://github.com/iamalnewkirk/test-db/wiki>
Project <https://github.com/iamalnewkirk/test-db>
Initiatives <https://github.com/iamalnewkirk/test-db/projects>
Milestones <https://github.com/iamalnewkirk/test-db/milestones>
Contributing
<https://github.com/iamalnewkirk/test-db/blob/master/CONTRIBUTE.md>
Issues <https://github.com/iamalnewkirk/test-db/issues>