File Coverage

blib/lib/Test/Fixture/DBIC/Schema.pm
Criterion Covered Total %
statement 43 43 100.0
branch 4 4 100.0
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 59 59 100.0


line stmt bran cond sub pod time code
1             package Test::Fixture::DBIC::Schema;
2             # git description: 568db46
3              
4             our $AUTHORITY = 'cpan:SCHWIGON';
5             # ABSTRACT: fixture data loader for DBIx::Class::Schema
6 4     4   1414297 use strict;
  4         11  
  4         207  
7 4     4   25 use warnings;
  4         8  
  4         356  
8             our $VERSION = '0.04';
9 4     4   26 use base 'Exporter';
  4         8  
  4         758  
10             our @EXPORT = qw/construct_fixture/;
11 4     4   3965 use Params::Validate ':all';
  4         30678  
  4         961  
12 4     4   3562 use Kwalify ();
  4         19059  
  4         134  
13 4     4   32 use Carp;
  4         7  
  4         2017  
14              
15             sub construct_fixture {
16 6     6 1 860646 validate(
17             @_ => +{
18             schema => +{ isa => 'DBIx::Class::Schema' },
19             fixture => 1,
20             }
21             );
22 4         30 my %args = @_;
23              
24 4         19 my $fixture = _validate_fixture(_load_fixture($args{fixture}));
25 2         8 _delete_all($args{schema});
26 2         5299 return _insert($args{schema}, $fixture);
27             }
28              
29             sub _load_fixture {
30 4     4   9 my $stuff = shift;
31              
32 4 100       13 if (ref $stuff) {
33 3 100       12 if (ref $stuff eq 'ARRAY') {
34 2         24 return $stuff;
35             } else {
36 1         304 croak "invalid fixture stuff. should be ARRAY: $stuff";
37             }
38             } else {
39 1         474 require YAML::Syck;
40 1         1612 return YAML::Syck::LoadFile($stuff);
41             }
42             }
43              
44             sub _validate_fixture {
45 3     3   272 my $stuff = shift;
46              
47 3         46 Kwalify::validate(
48             {
49             type => 'seq',
50             sequence => [
51             {
52             type => 'map',
53             mapping => {
54             schema => { type => 'str', required => 1 },
55             name => { type => 'str', required => 1 },
56             data => { type => 'any', required => 1 },
57             },
58             }
59             ]
60             },
61             $stuff
62             );
63              
64 2         922 $stuff;
65             }
66              
67             sub _delete_all {
68 2     2   3 my $schema = shift;
69              
70 2         14 $schema->resultset($_)->delete for
71 6         224 grep { $schema->source_registrations->{$_}->isa('DBIx::Class::ResultSource::Table') }
72             $schema->sources;
73             }
74              
75             sub _insert {
76 2     2   5 my ($schema, $fixture) = @_;
77              
78 2         5 my $result = {};
79 2         2 for my $row ( @{ $fixture } ) {
  2         5  
80 4         5777 $schema->resultset( $row->{schema} )->create( $row->{data} );
81 4         6552 $result->{ $row->{name} } = $schema->resultset( $row->{schema} )->find( $row->{data} );
82             }
83 2         4385 return $result;
84             }
85              
86             1;
87              
88             __END__