| 1 | package Test::Fixture::Teng; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | our $VERSION = '0.01'; | |||||||||
| 5 | 2 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | our @EXPORT = qw/construct_fixture/; | |||||||||
| 7 | 2 | ▶ | use Params::Validate ':all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Kwalify (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | sub construct_fixture { | ||||||||
|
Subroutine
construct_fixture: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 12 | 2 | my %args = validate( | |||||||||
| 13 | @_ => +{ | ||||||||||
| 14 | db => 1, | ||||||||||
| 15 | fixture => 1, | ||||||||||
| 16 | } | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 2 | my $fixture = _validate_fixture(_load_fixture($args{fixture})); | |||||||||
| 20 | 2 | _delete_all($args{db}); | |||||||||
| 21 | 2 | return _insert($args{db}, $fixture); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | sub _load_fixture { | ||||||||
|
Subroutine
_load_fixture: called CC=3 SCAR=11.2
| |||||||||||
| 25 | 2 | my $stuff = shift; | |||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | if (ref $stuff) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 1 | ▶ | if (ref $stuff eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 1 | return $stuff; | |||||||||
| 30 | } else { | ||||||||||
| 31 | 0 | Carp::croak "invalid fixture stuff. should be ARRAY: $stuff"; | |||||||||
| 32 | } | ||||||||||
| 33 | } else { | ||||||||||
| 34 | 1 | require YAML::Syck; | |||||||||
| 35 | 1 | return YAML::Syck::LoadFile($stuff); | |||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | sub _validate_fixture { | ||||||||
|
Subroutine
_validate_fixture: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | my $stuff = shift; | |||||||||
| 41 | |||||||||||
| 42 | 2 | Kwalify::validate( | |||||||||
| 43 | { | ||||||||||
| 44 | type => 'seq', | ||||||||||
| 45 | sequence => [ | ||||||||||
| 46 | { | ||||||||||
| 47 | type => 'map', | ||||||||||
| 48 | mapping => { | ||||||||||
| 49 | table => { type => 'str', required => 1 }, | ||||||||||
| 50 | name => { type => 'str', required => 1 }, | ||||||||||
| 51 | data => { type => 'any', required => 1 }, | ||||||||||
| 52 | }, | ||||||||||
| 53 | } | ||||||||||
| 54 | ] | ||||||||||
| 55 | }, | ||||||||||
| 56 | $stuff | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | 2 | $stuff; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | sub _delete_all { | ||||||||
|
Subroutine
_delete_all: called CC=2 SCAR=6.9
| |||||||||||
| 63 | 2 | my $db = shift; | |||||||||
| 64 | 2 | $db->delete($_) for | |||||||||
| 65 | 2 | keys %{$db->schema->tables}; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 2 | ▶ | sub _insert { | ||||||||
|
Subroutine
_insert: called CC=2 SCAR=6.9
| |||||||||||
| 69 | 2 | my ($db, $fixture) = @_; | |||||||||
| 70 | |||||||||||
| 71 | 2 | my $result = {}; | |||||||||
| 72 | 2 | for my $row ( @{ $fixture } ) { | |||||||||
| 73 | 6 | $result->{ $row->{name} } = $db->insert($row->{table}, $row->{data}); | |||||||||
| 74 | } | ||||||||||
| 75 | 2 | return $result; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 2 | 1; | |||||||||
| 79 | __END__ | ||||||||||