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 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.000011';
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 Module::Runtime qw(use_module);
205 my $app = use_module('My::Sims::REST')->to_psgi_app;
206  
207 use Plack::Builder;
208  
209 builder {
210 # Or whatever other middleware you want.
211 enable "Runtime"; # Adds the X-Runtime header
212 $app
213 }
214  
215 Then later:
216  
217 plackup <path/to/your/rest.cgi> -p <PORT>
218  
219 And, finally, in your test (or some library your tests use):
220  
221 my $data = {
222 databases => [
223 {
224 database => {
225 name => 'database name',
226 username => 'some username',
227 password => 'some password',
228 },
229 spec => <DBIx::Class::Sims specification>,
230 },
231 ],
232 };
233  
234 my $ua = LWP::UserAgent->new;
235 my $req = HTTP::Request->new(POST => 'http://<URL>:<PORT>/sims');
236 $req->content(encode_json($data));
237 my $res = $ua->request($req);
238  
239 return decode_json($res->content);
240  
241 =head1 PURPOSE
242  
243 L<DBIx::Class::Sims> provides an easy way to create and populate test data. But,
244 sometimes, your application isn't built on L<DBIx::Class>. Or Perl. These issues
245 shouldn't get in the way of using good tools. (Even if your application or test
246 suite aren't in Perl.)
247  
248 Assumption: Everything can issue an HTTP request.
249  
250 Conclusion: The Sims should be available via an HTTP request.
251  
252 =head1 DESCRIPTION
253  
254 This is a skeleton base class that provides the basic functionality for a REST
255 API around L<DBIx::Class::Sims>. By itself, it only takes the request, parses it
256 out, and invokes a series of methods that have empty implementations. You are
257 supposed to subclass this class and provide the meat of these methods.
258  
259 You will have to create a L<DBIx::Class> description of your schema (or, at the
260 least, the bits you want to be able to sim in your tests). It really isn't that
261 difficult - there are examples in the test suite for this module.
262  
263 Once you have all of that, you will need to host this REST API somewhere. Since
264 its purpose is to aid in testing, a good place for it is in your developers'
265 Vagrant VMs, and also in the VM you use to run CI tests on.
266  
267 B<THIS SHOULD NEVER BE MADE AVAILABLE IN PRODUCTION.> If you do so, the problems
268 you will have are on your head and your head alone. I explicitly and
269 categorically disavow any and all responsibility for your idiocy if this ends up
270 in your production environment. Please, do not be stupid.
271  
272 =head1 REQUEST
273  
274 The full data structure to be passed via a request is as follows:
275  
276 {
277 defaults => {
278 database => {
279 username => '',
280 password => '',
281 root => {
282 username => '',
283 password => '',
284 },
285 },
286 create => 1,
287 deploy => 1,
288 },
289 databases => [
290 {
291 database => {
292 username => 'username',
293 password => 'password',
294 name => 'name',
295 root => {
296 username => 'root_username',
297 password => 'root_password',
298 },
299 },
300 create => <0|1>,
301 deploy => <0|1>,
302 spec => < First parameter to load_sims() >
303 options => < Second parameter to load_sims() >
304 },
305 ...
306 ],
307 }
308  
309 =over 4
310  
311 =item * databases
312  
313 Each entry in this array represents the setting up of a single database. You may
314 want to set up multiple databases at the same time, hence the ability to specify
315 multiple databases. Each entry will be executed in the order they are specified,
316 if that matters. Each entry can decide whether or not to create or deploy, as
317 appropriate to your purpose.
318  
319 =over 4
320  
321 =item * create
322  
323 This will drop and recreate the database. This defaults to true. If this is set,
324 then deploy will also be set.
325  
326 This requires the root username, password, and whatever create commands are
327 required to be set. Please see the appropriate sections of the documentation for
328 further information.
329  
330 =item * deploy
331  
332 This will deploy the schema per C<<$schema->deploy(add_drop_tables => 1)>>.
333  
334 =item * spec
335  
336 This is the meat and potatoes of this module - the reason why you're here.
337  
338 Please see L<DBIx::Class::Sims/load_sims> for further information.
339  
340 =item * options
341  
342 L<DBIx::Class::Sims/load_sims> has an optional second parameter of options. This
343 allows you to set them. Please see that documentation for further information.
344  
345 You can set a default set of options by overriding L</sims_options>.
346  
347 =back
348  
349 =back
350  
351 =head1 RESPONSE
352  
353 If all goes well, the response will be an array of the return values from
354 L<DBIx::Class::Sims/load_sims>. The array will be in the same order as
355 the databases element of the request.
356  
357 If nothing happens, the response will be exactly:
358  
359 { "error": "No actions taken" }
360  
361 =head1 REQUIRED METHODS
362  
363 You have to override the following methods for anything to work.
364  
365 =head2 get_schema_class( $item, $defaults )
366  
367 This method should return a string containing the package name for the schema.
368 It can use anything in the entry and the defaults.
369  
370 If it returns nothing, then this entry will be skipped.
371  
372 You B<MUST> provide an implementation of this method - the base implementation
373 throws an error.
374  
375 =head2 get_connect_string( $item, $defaults )
376  
377 This method should return the first parameter in the connect() method. It can
378 use anything in the entry and the defaults.
379  
380 If it returns nothing, then this entry will be skipped.
381  
382 This method has implementations in the SQLite and MySQL subclasses. Otherwise,
383 you B<MUST> provide an implementation of this method - the base implementation
384 throws an error.
385  
386 =head1 OPTIONAL METHODS
387  
388 =head2 get_defaults()
389  
390 This method should return a hashref that provides the defaults for all calls.
391  
392 The base implementation returns the following:
393  
394 {
395 database => {
396 username => '',
397 password => '',
398 root => {
399 username => '',
400 password => '',
401 },
402 },
403 create => 1,
404 deploy => 1,
405 }
406  
407 =head2 get_username( $item, $defaults )
408  
409 This method should return a string to be used as the username in the connect
410 string.
411  
412 The base implementation returns the username from either the C<$item> or the
413 C<$defaults>.
414  
415 =head2 get_password( $item, $defaults )
416  
417 This method should return a string to be used as the password in the connect
418 string.
419  
420 The base implementation returns the password from either the C<$item> or the
421 C<$defaults>.
422  
423 =head2 get_root_connection( $item, $defaults )
424  
425 This method should return a DBI C<$dbh> that has a root connection to the
426 database. This is what the return value of C<get_create_commands()> will be
427 passed to.
428  
429 The base implementation retrieves the root username and password from the
430 database entry in the C<$item> or C<$defaults>, then uses the value from
431 C<get_connect_string()> to connect to the database. This should be sufficient
432 for most purposes.
433  
434 =head2 get_create_commands( $item, $defaults )
435  
436 This method should return an array of SQL commands to be executed via a root
437 connection when a database is dropped and created. These commands will be
438 executed when C<<create => 1>>.
439  
440 The base implementation returns an empty array.
441  
442 =head2 populate_default_data( $schema, $item, $defaults )
443  
444 This method is called after create/deploy is processed, but before
445 C<<$schema->load_sims()>> is invoked. You can use this method to do any default
446 data population. For example, to invoke L<DBIx::Class::Fixtures> or to call
447 L<DBIx::Class::Schema/populate>.
448  
449 =head2 sims_options( $schema, $item, $defaults )
450  
451 This method should return a hashref appropriate to pass as the second parameter
452 to C<load_sims()>. It will be merged with the whatever is passed in for the
453 specific item.
454  
455 The base implementation returns an empty hashref.
456  
457 =head1 SUGGESTIONS
458  
459 =head2 Additional Keys
460  
461 The keys listed thus far in C<$item> are only those L<DBIx::Class::Sims> uses
462 internally. You are more than welcome to add additional keys as you need. Then,
463 in the various methods that are passed C<$item>, you can pull those out.
464  
465 =head2 Multiple databases
466  
467 Your tests may need multiple databases to be populated. This is why the data
468 specification requires a databases array. The C<$item>s will be processed in the
469 order they are specified, in case that matters.
470  
471 You can combine this with adding an additional key to specify which database
472 this C<$item> is dealing with. That way, you can have a different schema class,
473 different default data, etc.
474  
475 =head1 TODO
476  
477 =over 4
478  
479 =item * Chef/Puppet recipes for auto-launching the REST API
480  
481 =item * Figure out how to pass the class into plackup without an envvar.
482  
483 =back
484  
485 =head1 BUGS/SUGGESTIONS
486  
487 This module is hosted on Github at
488 L<https://github.com/robkinyon/dbix-class-sims-rest>. Pull requests are strongly
489 encouraged.
490  
491 =head1 SEE ALSO
492  
493 L<DBIx::Class::Sims>, L<Web::Simple>
494  
495 =head1 AUTHOR
496  
497 Rob Kinyon <rob.kinyon@gmail.com>
498  
499 =head1 LICENSE
500  
501 Copyright (c) 2013 Rob Kinyon. All Rights Reserved.
502 This is free software, you may use it and distribute it under the same terms
503 as Perl itself.
504  
505 =cut
↑ Summary REST/MySQL.pm »