blib/lib/DBIx/Class/Sims/REST.pm

filter: Statementstmt 94.3% 84 / 89 Branchbran 62.5% 5 / 8 Conditioncond 46.3% 25 / 54 MC/DCmcdc 7.1% 2 / 28 Subroutinesub 86.9% 20 / 23 Podpod 69.2% 9 / 13 totaltotal 67.4% 145 / 215 CC 31 SCAR 27.4
CC
31
Cov
75%
do_sims
36.4
get_root_connection
6.3
get_schema
5.5
CRAP 15.5
↑ Summary blib/lib/DBIx/Class/Sims/REST/MySQL.pm »
1 package DBIx::Class::Sims::REST;
2  
3 1 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.000010';
9  
10 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $base_defaults = {
14 database => {
15 username => '',
16 password => '',
17 root => {
18 username => '',
19 password => '',
20 },
21 },
22 create => 1,
23 deploy => 1,
24 };
25  
26 sub get_defaults {
27 2 ▶ return;
Subroutine get_defaults: called CC=1 SCAR=0.0
Pod documented
28 }
29  
30 sub get_root_connection {
31 1 ▶ my $class = shift;
Subroutine get_root_connection: called CC=5 SCAR=18.4
Pod documented
32 1 my ($item, $defaults) = @_;
33  
34 1 ▶ my $user = $item->{database}{root}{username} // $defaults->{database}{root}{username} // '';
Condition40%2/5
($item->{"database"}{"root"}{"username"} // $defaults->{"database"}{"root"}{"username"}) // ""
l!l
10
$item->{"database"}{"root"}{"username"} // $defaults->{"database"}{"root"}{"username"}
l!l&&r!l&&!r
010
MC/DC0%0/2
($item->{"database"}{"root"}{"username"} // $defaults->{"database"}{"root"}{"username"}) // ""
$item->{"database"}{"root"}{"username"} $defaults->{"database"}{"root"}{"username"}
Truth table33%1/3
A // B
A= $item->{"database"}{"root"}{"username"}B= $defaults->{"database"}{"root"}{"username"}
ABresult
1X1
011
000
35 1 ▶ my $pass = $item->{database}{root}{password} // $defaults->{database}{root}{password} // '';
Condition40%2/5
($item->{"database"}{"root"}{"password"} // $defaults->{"database"}{"root"}{"password"}) // ""
l!l
10
$item->{"database"}{"root"}{"password"} // $defaults->{"database"}{"root"}{"password"}
l!l&&r!l&&!r
010
MC/DC0%0/2
($item->{"database"}{"root"}{"password"} // $defaults->{"database"}{"root"}{"password"}) // ""
$item->{"database"}{"root"}{"password"} $defaults->{"database"}{"root"}{"password"}
Truth table33%1/3
A // B
A= $item->{"database"}{"root"}{"password"}B= $defaults->{"database"}{"root"}{"password"}
ABresult
1X1
011
000
36  
37 1 my $connect_string = $class->get_connect_string(
38 $item, $defaults, { no_name => 1 },
39 );
40 1 return DBI->connect($connect_string, $user, $pass);
41 }
42  
43 sub get_create_commands {
44 0 ▶ die "Must override get_create_commands\n";
Subroutine get_create_commands: not called CC=1 SCAR=6.9
Pod documented
45 }
46  
47 sub get_schema_class {
48 0 ▶ die "Must override get_schema_class\n";
Subroutine get_schema_class: not called CC=1 SCAR=6.9
Pod documented
49 }
50  
51 sub get_connect_string {
52 0 ▶ die "Must override get_connect_string\n";
Subroutine get_connect_string: not called CC=1 SCAR=6.9
Pod documented
53 }
54  
55 sub get_username {
56 1 ▶ my $class = shift;
Subroutine get_username: called CC=2 SCAR=7.6
Pod documented
57 1 my ($item, $defaults) = @_;
58  
59 1 ▶ return $item->{database}{username} // $defaults->{database}{username};
Condition33%1/3
$item->{"database"}{"username"} // $defaults->{"database"}{"username"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$item->{"database"}{"username"} // $defaults->{"database"}{"username"}
$item->{"database"}{"username"} $defaults->{"database"}{"username"}
Truth table33%1/3
A // B
A= $item->{"database"}{"username"}B= $defaults->{"database"}{"username"}
ABresult
1X1
011
000
60 }
61  
62 sub get_password {
63 1 ▶ my $class = shift;
Subroutine get_password: called CC=2 SCAR=7.6
Pod documented
64 1 my ($item, $defaults) = @_;
65  
66 1 ▶ return $item->{database}{password} // $defaults->{database}{password};
Condition33%1/3
$item->{"database"}{"password"} // $defaults->{"database"}{"password"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$item->{"database"}{"password"} // $defaults->{"database"}{"password"}
$item->{"database"}{"password"} $defaults->{"database"}{"password"}
Truth table33%1/3
A // B
A= $item->{"database"}{"password"}B= $defaults->{"database"}{"password"}
ABresult
1X1
011
000
67 }
68  
69 sub get_schema {
70 1 ▶ my $class = shift;
Subroutine get_schema: called CC=5 SCAR=17.0
Pod undocumented
71 1 my ($item, $defaults) = @_;
72  
73 1 ▶ my $schema_class = $class->get_schema_class( $item, $defaults ) // return;
Condition50%1/2
$class->get_schema_class($item, $defaults) // (return)
l!l
10
MC/DC0%0/1
$class->get_schema_class($item, $defaults) // (return)
$class->get_schema_class($item, $defaults)
Truth table50%1/2
A
A= $class->get_schema_class($item, $defaults)
Aresult
11
00
74 1 ▶ my $connect_string = $class->get_connect_string( $item, $defaults ) // return;
Condition50%1/2
$class->get_connect_string($item, $defaults) // (return)
l!l
10
MC/DC0%0/1
$class->get_connect_string($item, $defaults) // (return)
$class->get_connect_string($item, $defaults)
Truth table50%1/2
A
A= $class->get_connect_string($item, $defaults)
Aresult
11
00
75  
76 1 ▶ my $user = $class->get_username($item, $defaults) // '';
Condition50%1/2
$class->get_username($item, $defaults) // ""
l!l
10
MC/DC0%0/1
$class->get_username($item, $defaults) // ""
$class->get_username($item, $defaults)
Truth table50%1/2
A
A= $class->get_username($item, $defaults)
Aresult
11
00
77 1 ▶ my $pass = $class->get_password($item, $defaults) // '';
Condition50%1/2
$class->get_password($item, $defaults) // ""
l!l
10
MC/DC0%0/1
$class->get_password($item, $defaults) // ""
$class->get_password($item, $defaults)
Truth table50%1/2
A
A= $class->get_password($item, $defaults)
Aresult
11
00
78  
79 1 return $schema_class->connect(
80 $connect_string, $user, $pass, {
81 PrintError => 0,
82 RaiseError => 1,
83 },
84 );
85 }
86  
87 sub populate_default_data {
88 1 ▶ return;
Subroutine populate_default_data: called CC=1 SCAR=0.0
Pod documented
89 }
90  
91 sub sims_options {
92 1 ▶ return;
Subroutine sims_options: called CC=1 SCAR=0.0
Pod documented
93 }
94  
95 sub flatten_for_transport {
96 1 ▶ my $class = shift;
Subroutine flatten_for_transport: called CC=2 SCAR=6.9
Pod undocumented
97 1 my ($sims) = @_;
98 # Convert objects into columns/values for JSON encoding.
99 1 my %flattened;
100 1 ▶ while ( my ($k, $v) = each %{$sims} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$sims;}) 1 1 2
101 1 $flattened{$k} = [ map { { $_->get_columns } } @$v ];
102 }
103  
104 1 return \%flattened;
105 }
106  
107 sub do_sims {
108 2 ▶ my $class = shift;
Subroutine do_sims: called CC=20 SCAR=35.9
Pod undocumented
109 2 my ($request) = @_;
110  
111 2 my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
112  
113 my $defaults = $merger->merge(
114 $base_defaults // {},
115 $class->get_defaults // {},
116 $request->{defaults} // {},
117 2 ▶ );
Condition50%3/6
$base_defaults // {}
l!l
20
$class->get_defaults // {}
l!l
02
$request->{"defaults"} // {}
l!l
02
MC/DC0%0/1
$base_defaults // {}
$base_defaults
MC/DC0%0/1
$class->get_defaults // {}
$class->get_defaults
MC/DC0%0/1
$request->{"defaults"} // {}
$request->{"defaults"}
Truth table50%1/2
A
A= $base_defaults
Aresult
11
00
Truth table50%1/2
A
A= $class->get_defaults
Aresult
11
00
Truth table50%1/2
A
A= $request->{"defaults"}
Aresult
11
00
118  
119 2 my $rv;
120 2 ▶ foreach my $item ( @{$request->{databases} // []} ) {
Condition100%2/2
$request->{"databases"} // []
l!l
11
MC/DC100%1/1
$request->{"databases"} // []
$request->{"databases"}
Truth table100%2/2
A
A= $request->{"databases"}
Aresult
11
00
121 1 my $schema;
122 1 ▶ if ( $item->{deploy} // $defaults->{deploy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{'deploy'} // $defaults->{'deploy'}) { } 1 0 1
Condition33%1/3
$item->{"deploy"} // $defaults->{"deploy"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$item->{"deploy"} // $defaults->{"deploy"}
$item->{"deploy"} $defaults->{"deploy"}
Truth table33%1/3
A // B
A= $item->{"deploy"}B= $defaults->{"deploy"}
ABresult
1X1
011
000
123 # Only create if we're also going to deploy.
124 1 ▶ if ( $item->{create} // $defaults->{create} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"create"} // $defaults->{"create"}) 1 0 1
Condition33%1/3
$item->{"create"} // $defaults->{"create"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$item->{"create"} // $defaults->{"create"}
$item->{"create"} $defaults->{"create"}
Truth table33%1/3
A // B
A= $item->{"create"}B= $defaults->{"create"}
ABresult
1X1
011
000
125 1 ▶ my $root_dbh = $class->get_root_connection($item, $defaults) // next;
Condition50%1/2
$class->get_root_connection($item, $defaults) // (next)
l!l
10
MC/DC0%0/1
$class->get_root_connection($item, $defaults) // (next)
$class->get_root_connection($item, $defaults)
Truth table50%1/2
A
A= $class->get_root_connection($item, $defaults)
Aresult
11
00
126 1 my @commands = $class->get_create_commands($item, $defaults);
127  
128 1 ▶ if (@commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commands) 0 1 1
129 0 $root_dbh->do($_) for @commands;
130 }
131 }
132  
133 # The database may not exist until this point.
134 1 ▶ $schema = $class->get_schema($item, $defaults) // next;
Condition50%1/2
$class->get_schema($item, $defaults) // (next)
l!l
10
MC/DC0%0/1
$class->get_schema($item, $defaults) // (next)
$class->get_schema($item, $defaults)
Truth table50%1/2
A
A= $class->get_schema($item, $defaults)
Aresult
11
00
135  
136 # XXX Need to capture the warnings and provide them back to the caller
137 1 $schema->deploy({
138 add_drop_table => 1,
139 show_warnings => 1,
140 });
141 }
142 else {
143 0 ▶ $schema = $class->get_schema($item, $defaults) // next;
Condition0%0/2
$class->get_schema($item, $defaults) // (next)
l!l
00
MC/DC0%0/1
$class->get_schema($item, $defaults) // (next)
$class->get_schema($item, $defaults)
Truth table0%0/2
A
A= $class->get_schema($item, $defaults)
Aresult
11
00
144 }
145  
146 1 $class->populate_default_data($schema, $item, $defaults);
147  
148 my $sims = $schema->load_sims(
149 $item->{spec} // {},
150 $merger->merge(
151 $class->sims_options($schema, $item, $defaults) // {},
152 $item->{options} // {}
153 1 ▶ ),
Condition50%3/6
$item->{"spec"} // {}
l!l
10
$class->sims_options($schema, $item, $defaults) // {}
l!l
01
$item->{"options"} // {}
l!l
01
MC/DC0%0/1
$item->{"spec"} // {}
$item->{"spec"}
MC/DC0%0/1
$class->sims_options($schema, $item, $defaults) // {}
$class->sims_options($schema, $item, $defaults)
MC/DC0%0/1
$item->{"options"} // {}
$item->{"options"}
Truth table50%1/2
A
A= $item->{"spec"}
Aresult
11
00
Truth table50%1/2
A
A= $class->sims_options($schema, $item, $defaults)
Aresult
11
00
Truth table50%1/2
A
A= $item->{"options"}
Aresult
11
00
154 );
155 1 ▶ push @{ $rv //= [] }, $class->flatten_for_transport($sims);
Condition50%1/2
$rv //= []
l!l
01
MC/DC0%0/1
$rv //= []
$rv
Truth table50%1/2
A
A= $rv
Aresult
11
00
156 }
157  
158 2 ▶ return $rv // { error => 'No actions taken' };
Condition100%2/2
$rv // {"error", "No actions taken"}
l!l
11
MC/DC100%1/1
$rv // {"error", "No actions taken"}
$rv
Truth table100%2/2
A
A= $rv
Aresult
11
00
159 }
160  
161 1 ▶use JSON::XS qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
162 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 1 ▶use Web::Simple; # Needed for the prototypes
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 sub dispatch_request {
166 2 ▶ my $class = shift;
Subroutine dispatch_request: called CC=1 SCAR=0.0
Pod undocumented
167 sub (/sims) {
168 sub (POST) {
169 2 ▶ my ($self, $env) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
170  
171 2 my $r = Plack::Request->new($env);
172 2 my $request = decode_json($r->content);
173  
174 2 my $rv = $class->do_sims( $request );
175  
176 2 [ 200, [ 'Content-type', 'application/json' ],
177 [ encode_json($rv) ],
178 ]
179 },
180 2 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
181 2 }
182  
183 1 1;
184 __END__
185  
186 =head1 NAME
187  
188 DBIx::Class::Sims::REST
189  
190 =head1 SYNOPSIS
191  
192 In your REST API class:
193  
194 package My::Sims::REST
195  
196 use base 'DBIx::Class::Sims::REST::MySQL';
197  
198 sub get_schema_class { return 'My::Schema::Class' }
199  
200 1;
201  
202 In a rest.cgi file (somewhere):
203  
204 use My::Sims::REST;
205 use Web::Simple 'My::Sims::REST';
206  
207 My::Sims::REST->run_if_script;
208  
209 Then later:
210  
211 plackup <path/to/your/rest.cgi> -p <PORT>
212  
213 And, finally, in your test (or some library your tests use):
214  
215 my $data = {
216 databases => [
217 {
218 database => {
219 name => 'database name',
220 username => 'some username',
221 password => 'some password',
222 },
223 spec => <DBIx::Class::Sims specification>,
224 },
225 ],
226 };
227  
228 my $ua = LWP::UserAgent->new;
229 my $req = HTTP::Request->new(POST => 'http://<URL>:<PORT>/sims');
230 $req->content(encode_json($data));
231 my $res = $ua->request($req);
232  
233 return decode_json($res->content);
234  
235 =head1 PURPOSE
236  
237 L<DBIx::Class::Sims> provides an easy way to create and populate test data. But,
238 sometimes, your application isn't built on L<DBIx::Class>. Or Perl. These issues
239 shouldn't get in the way of using good tools. (Even if your application or test
240 suite aren't in Perl.)
241  
242 Assumption: Everything can issue an HTTP request.
243  
244 Conclusion: The Sims should be available via an HTTP request.
245  
246 =head1 DESCRIPTION
247  
248 This is a skeleton base class that provides the basic functionality for a REST
249 API around L<DBIx::Class::Sims>. By itself, it only takes the request, parses it
250 out, and invokes a series of methods that have empty implementations. You are
251 supposed to subclass this class and provide the meat of these methods.
252  
253 You will have to create a L<DBIx::Class> description of your schema (or, at the
254 least, the bits you want to be able to sim in your tests). It really isn't that
255 difficult - there are examples in the test suite for this module.
256  
257 Once you have all of that, you will need to host this REST API somewhere. Since
258 its purpose is to aid in testing, a good place for it is in your developers'
259 Vagrant VMs, and also in the VM you use to run CI tests on.
260  
261 B<THIS SHOULD NEVER BE MADE AVAILABLE IN PRODUCTION.> If you do so, the problems
262 you will have are on your head and your head alone. I explicitly and
263 categorically disavow any and all responsibility for your idiocy if this ends up
264 in your production environment. Please, do not be stupid.
265  
266 =head1 REQUEST
267  
268 The full data structure to be passed via a request is as follows:
269  
270 {
271 defaults => {
272 database => {
273 username => '',
274 password => '',
275 root => {
276 username => '',
277 password => '',
278 },
279 },
280 create => 1,
281 deploy => 1,
282 },
283 databases => [
284 {
285 database => {
286 username => 'username',
287 password => 'password',
288 name => 'name',
289 root => {
290 username => 'root_username',
291 password => 'root_password',
292 },
293 },
294 create => <0|1>,
295 deploy => <0|1>,
296 spec => < First parameter to load_sims() >
297 options => < Second parameter to load_sims() >
298 },
299 ...
300 ],
301 }
302  
303 =over 4
304  
305 =item * databases
306  
307 Each entry in this array represents the setting up of a single database. You may
308 want to set up multiple databases at the same time, hence the ability to specify
309 multiple databases. Each entry will be executed in the order they are specified,
310 if that matters. Each entry can decide whether or not to create or deploy, as
311 appropriate to your purpose.
312  
313 =over 4
314  
315 =item * create
316  
317 This will drop and recreate the database. This defaults to true. If this is set,
318 then deploy will also be set.
319  
320 This requires the root username, password, and whatever create commands are
321 required to be set. Please see the appropriate sections of the documentation for
322 further information.
323  
324 =item * deploy
325  
326 This will deploy the schema per C<<$schema->deploy(add_drop_tables => 1)>>.
327  
328 =item * spec
329  
330 This is the meat and potatoes of this module - the reason why you're here.
331  
332 Please see L<DBIx::Class::Sims/load_sims> for further information.
333  
334 =item * options
335  
336 L<DBIx::Class::Sims/load_sims> has an optional second parameter of options. This
337 allows you to set them. Please see that documentation for further information.
338  
339 You can set a default set of options by overriding L</sims_options>.
340  
341 =back
342  
343 =back
344  
345 =head1 RESPONSE
346  
347 If all goes well, the response will be an array of the return values from
348 L<DBIx::Class::Sims/load_sims>. The array will be in the same order as
349 the databases element of the request.
350  
351 If nothing happens, the response will be exactly:
352  
353 { "error": "No actions taken" }
354  
355 =head1 REQUIRED METHODS
356  
357 You have to override the following methods for anything to work.
358  
359 =head2 get_schema_class( $item, $defaults )
360  
361 This method should return a string containing the package name for the schema.
362 It can use anything in the entry and the defaults.
363  
364 If it returns nothing, then this entry will be skipped.
365  
366 You B<MUST> provide an implementation of this method - the base implementation
367 throws an error.
368  
369 =head2 get_connect_string( $item, $defaults )
370  
371 This method should return the first parameter in the connect() method. It can
372 use anything in the entry and the defaults.
373  
374 If it returns nothing, then this entry will be skipped.
375  
376 This method has implementations in the SQLite and MySQL subclasses. Otherwise,
377 you B<MUST> provide an implementation of this method - the base implementation
378 throws an error.
379  
380 =head1 OPTIONAL METHODS
381  
382 =head2 get_defaults()
383  
384 This method should return a hashref that provides the defaults for all calls.
385  
386 The base implementation returns the following:
387  
388 {
389 database => {
390 username => '',
391 password => '',
392 root => {
393 username => '',
394 password => '',
395 },
396 },
397 create => 1,
398 deploy => 1,
399 }
400  
401 =head2 get_username( $item, $defaults )
402  
403 This method should return a string to be used as the username in the connect
404 string.
405  
406 The base implementation returns the username from either the C<$item> or the
407 C<$defaults>.
408  
409 =head2 get_password( $item, $defaults )
410  
411 This method should return a string to be used as the password in the connect
412 string.
413  
414 The base implementation returns the password from either the C<$item> or the
415 C<$defaults>.
416  
417 =head2 get_root_connection( $item, $defaults )
418  
419 This method should return a DBI C<$dbh> that has a root connection to the
420 database. This is what the return value of C<get_create_commands()> will be
421 passed to.
422  
423 The base implementation retrieves the root username and password from the
424 database entry in the C<$item> or C<$defaults>, then uses the value from
425 C<get_connect_string()> to connect to the database. This should be sufficient
426 for most purposes.
427  
428 =head2 get_create_commands( $item, $defaults )
429  
430 This method should return an array of SQL commands to be executed via a root
431 connection when a database is dropped and created. These commands will be
432 executed when C<<create => 1>>.
433  
434 The base implementation returns an empty array.
435  
436 =head2 populate_default_data( $schema, $item, $defaults )
437  
438 This method is called after create/deploy is processed, but before
439 C<<$schema->load_sims()>> is invoked. You can use this method to do any default
440 data population. For example, to invoke L<DBIx::Class::Fixtures> or to call
441 L<DBIx::Class::Schema/populate>.
442  
443 =head2 sims_options( $schema, $item, $defaults )
444  
445 This method should return a hashref appropriate to pass as the second parameter
446 to C<load_sims()>. It will be merged with the whatever is passed in for the
447 specific item.
448  
449 The base implementation returns an empty hashref.
450  
451 =head1 SUGGESTIONS
452  
453 =head2 Additional Keys
454  
455 The keys listed thus far in C<$item> are only those L<DBIx::Class::Sims> uses
456 internally. You are more than welcome to add additional keys as you need. Then,
457 in the various methods that are passed C<$item>, you can pull those out.
458  
459 =head2 Multiple databases
460  
461 Your tests may need multiple databases to be populated. This is why the data
462 specification requires a databases array. The C<$item>s will be processed in the
463 order they are specified, in case that matters.
464  
465 You can combine this with adding an additional key to specify which database
466 this C<$item> is dealing with. That way, you can have a different schema class,
467 different default data, etc.
468  
469 =head1 TODO
470  
471 =over 4
472  
473 =item * Chef/Puppet recipes for auto-launching the REST API
474  
475 =item * Figure out how to pass the class into plackup without an envvar.
476  
477 =back
478  
479 =head1 BUGS/SUGGESTIONS
480  
481 This module is hosted on Github at
482 L<https://github.com/robkinyon/dbix-class-sims-rest>. Pull requests are strongly
483 encouraged.
484  
485 =head1 SEE ALSO
486  
487 L<DBIx::Class::Sims>, L<Web::Simple>
488  
489 =head1 AUTHOR
490  
491 Rob Kinyon <rob.kinyon@gmail.com>
492  
493 =head1 LICENSE
494  
495 Copyright (c) 2013 Rob Kinyon. All Rights Reserved.
496 This is free software, you may use it and distribute it under the same terms
497 as Perl itself.
498  
499 =cut
↑ Summary blib/lib/DBIx/Class/Sims/REST/MySQL.pm »