line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::MoCo::Fixture; |
2
|
1
|
|
|
1
|
|
62476
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
872
|
use Exporter::Lite; |
|
1
|
|
|
|
|
830
|
|
|
1
|
|
|
|
|
7
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
54
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
7
|
1
|
|
|
1
|
|
6
|
use FindBin; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
8
|
1
|
|
|
1
|
|
886
|
use Kwalify (); |
|
1
|
|
|
|
|
6301
|
|
|
1
|
|
|
|
|
22
|
|
9
|
1
|
|
|
1
|
|
775
|
use Path::Class; |
|
1
|
|
|
|
|
51891
|
|
|
1
|
|
|
|
|
70
|
|
10
|
1
|
|
|
1
|
|
894
|
use UNIVERSAL::require; |
|
1
|
|
|
|
|
1646
|
|
|
1
|
|
|
|
|
8
|
|
11
|
1
|
|
|
1
|
|
791
|
use YAML::Syck; |
|
1
|
|
|
|
|
2133
|
|
|
1
|
|
|
|
|
614
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = 0.01; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw/fixtures/; |
16
|
|
|
|
|
|
|
our @EXPORT = @EXPORT_OK; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub fixtures (@) { |
19
|
1
|
50
|
33
|
1
|
1
|
931
|
my $option = pop if ref $_[-1] and ref $_[-1] eq 'HASH'; |
20
|
1
|
|
50
|
|
|
6
|
$option ||= {}; |
21
|
1
|
50
|
|
|
|
5
|
croak 'usage: fixtures(@models, { yaml_dir => '...' })' unless @_; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
33
|
|
|
4
|
my $dir = $option->{yaml_dir} || dir($FindBin::Bin)->parent->subdir('fixtures'); |
24
|
1
|
|
|
|
|
1
|
my $res; |
25
|
1
|
|
|
|
|
3
|
for (@_) { |
26
|
2
|
|
|
|
|
23
|
my $config = validate_fixture( |
27
|
|
|
|
|
|
|
load_fixture( file($dir, sprintf("%s.yml", $_)) ) |
28
|
|
|
|
|
|
|
); |
29
|
2
|
50
|
|
|
|
38
|
$config->{model}->require or die $@; |
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
99
|
delete_all( $config->{model} ); |
32
|
2
|
|
|
|
|
77
|
$res->{$_} = insert( $config->{model}, $config->{records} ); |
33
|
|
|
|
|
|
|
} |
34
|
1
|
|
|
|
|
7
|
$res; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub load_fixture { |
38
|
2
|
|
|
2
|
0
|
487
|
my $file = shift; |
39
|
2
|
50
|
|
|
|
10
|
my $yaml = $file->slurp or die $!; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
1190
|
YAML::Syck::Load($yaml); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub validate_fixture { |
45
|
2
|
|
|
2
|
0
|
203
|
my $stuff = shift; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
35
|
Kwalify::validate({ |
48
|
|
|
|
|
|
|
type => 'map', |
49
|
|
|
|
|
|
|
mapping => { |
50
|
|
|
|
|
|
|
model => { type => 'str', required => 1 }, |
51
|
|
|
|
|
|
|
records => { type => 'any', required => 1 }, |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
}, $stuff); |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
1653
|
$stuff; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub delete_all { |
59
|
2
|
|
|
2
|
0
|
7
|
my $model = shift; |
60
|
2
|
50
|
|
|
|
27
|
if ($model->db->vendor eq 'SQLite') { |
61
|
2
|
|
|
|
|
700
|
$_->delete for $model->search; |
62
|
|
|
|
|
|
|
} else { |
63
|
0
|
|
|
|
|
0
|
$model->delete_all(where => { 1 => '1' }); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub insert { |
68
|
2
|
|
|
2
|
0
|
10
|
my ($model, $records) = @_; |
69
|
|
|
|
|
|
|
|
70
|
2
|
|
|
|
|
8
|
my $res = {}; |
71
|
2
|
|
|
|
|
16
|
for my $name (keys %$records) { |
72
|
3
|
|
|
|
|
7
|
my $obj = $model->create( %{ $records->{$name} } ); |
|
3
|
|
|
|
|
40
|
|
73
|
3
|
|
|
|
|
30
|
$obj->save; |
74
|
|
|
|
|
|
|
|
75
|
3
|
|
|
|
|
6
|
my %query; |
76
|
3
|
|
|
|
|
5
|
for (@{ $obj->primary_keys }) { |
|
3
|
|
|
|
|
11
|
|
77
|
3
|
|
|
|
|
64
|
$query{$_} = $obj->$_; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
|
|
24
|
$res->{$name} = $model->retrieve(%query); |
81
|
|
|
|
|
|
|
} |
82
|
2
|
|
|
|
|
20
|
$res; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |