File Coverage

lib/SQL/Bibliosoph/Sims.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #
2             #===============================================================================
3             # FILE: Bibliosoph::Sims.pm
4             # CREATED: 07/05/2009 01:59:31 PM
5             #===============================================================================
6             #
7             #
8              
9             =head1 NAME
10              
11             SQL::Bibliosoph::Sims - A SQL::Bibliosoph Tester library
12              
13              
14             =head1 SYNOPSIS
15              
16             my $bs = SQL::Bibliosoph::Sims();
17              
18             my $array_of_hashes = $bs->h_ANYQUERY();
19             my $hash = $bs->rowh_ANYQUERY();
20             my $array_of_arrays = $bs->ANYQUERY();
21              
22             This is a simple class to replace SQL::Bibliosoph in unit test. This generate random data and does not need a catalog file. (Methods are handled on request with AUTOLOAD). The returned value is in concordance with the requested resulset( e.g.: If you ask for that hash (with the prefix rowh_) you will get a hashref).
23            
24              
25             =head1 DESCRIPTION
26              
27             Will generate random date when you call any subrotine on it. This module is inspired on Test::Sims.
28              
29              
30             =head1 Constructor parameters
31              
32             =head3 rows
33              
34             This controls how many rows will be returned in the resultset. Defaults to 10.
35              
36             =head3 presets
37              
38             You can costumize the return of some particular query by using preset, like this:
39              
40             my $bs = SQL::Bibliosoph::Sims(
41             presets => {
42             rowh_user => '{ name => "juan", age => "42" }',
43             rowh_costumer => '{
44             name => "rand_words( size=>10 )",
45             age => "rand_chars( size=>2 )",
46             }',
47             }
48             );
49              
50             Values in the array will be evaluated. You can use rand_ functions from Data::Random to generate your values.. presets queries have preference over presets_catalog quieres.
51              
52              
53             =head3 presets_catalog
54              
55             You can also define catalog for tests. In this case, the queries not defined in the catalog will be random generated. The defined, will be evaluated:
56              
57             my $bs = SQL::Bibliosoph::Sims->new(
58             presets_catalog => 'tests.bb',
59             );
60              
61             tests.bb:
62             --[ TITo ]
63             { a=>1, b=>2 }
64             --[ rowh_RANDy ]
65             {name => join "", rand_chars( set=> "alpha", min=>5, max=>7) }
66             --[ rowh_RAND2y ]
67             {name => join "", rand_chars( set=> "numeric", min=>5, max=>7) }
68             --[ h_RAND3 ]
69             [ { id => (join '',rand_chars(set=>"numeric")), name => join ('', rand_chars(set=>"alpha")), role_code => 1 }, ],
70             --[ h_RAND4 ]
71             [ { id =>1 }, { id => 2 }, { id => 3 } , ],
72              
73             =head1 BUGS
74            
75             If you use presets_catalog, arrays references [] rows MUST BE ended with a ',' (comma), like in:
76              
77             --[ h_RAND4 ]
78             [ { id =>1 }, { id => 2 }, { id => 3 } , ],
79              
80              
81            
82              
83             =cut
84              
85             package SQL::Bibliosoph::Sims; {
86 1     1   1046 use Moose;
  0            
  0            
87             use Carp;
88             use Data::Dumper;
89              
90             our $VERSION = "2.0";
91             our $AUTOLOAD;
92              
93             use SQL::Bibliosoph::CatalogFile;
94              
95             use Tie::Array::Random;
96             use Tie::Hash::Random;
97             use Data::Random qw(:all);
98             use feature qw/switch/;
99             use Data::Dumper;
100              
101             has rows => (is => 'rw', isa => 'Int', default=> '10');
102             has presets => (is => 'rw', isa => 'HashRef', default=> sub { return {}; } );
103             has presets_catalog => (is => 'rw', isa => 'Str');
104              
105             sub BUILD {
106             my ($self) = @_;
107             my $qs;
108              
109             # Add catalog's presets, if any.
110              
111             my $file = $self->presets_catalog();
112             if ($file) {
113              
114             die "$file: $!" if ! -e $file;
115              
116             my $qs = SQL::Bibliosoph::CatalogFile->new( file => $file )->read();
117              
118             while (my ($k, $v) = each %$qs) {
119             $self->presets()->{$k} = $v;
120              
121             }
122             }
123              
124             return $self;
125             }
126              
127              
128             sub AUTOLOAD {
129             my $self = shift;
130             (my $method_name = $AUTOLOAD ) =~ s/^.*:://;
131             my $ret;
132              
133             my $rows = $self->rows();
134              
135              
136             die "no method name @_" if ! $method_name;
137              
138             if (my $return = $self->presets()->{$method_name}) {
139             my $ret = eval $return;
140             if ($@) {
141             die "Error in \"$method_name\": $@\n $return \n";
142             }
143             return $ret;
144             }
145              
146              
147             given ($method_name) {
148             when (/\browh_/) {
149             my %hash;
150             tie %hash, 'Tie::Hash::Random';
151             $ret = \%hash;
152              
153             return $ret;
154             }
155             when (/\brow_/) {
156             my @array;
157             tie @array, 'Tie::Array::Random';
158             $ret = \@array;
159              
160             return $ret;
161             }
162             when (/\bh_/) {
163             my $ret = [];
164             foreach (1..$rows) {
165             my %hash;
166             tie %hash, 'Tie::Hash::Random';
167             push @$ret, \%hash;
168             }
169             return wantarray ? ($ret,$rows) : $ret;
170             }
171             default {
172             my $ret = [];
173             foreach (1..$rows) {
174             my @array;
175             tie @array, 'Tie::Array::Random';
176             push @$ret, \@array;
177             }
178             return wantarray ? ($ret,$rows) : $ret;
179             }
180             };
181             }
182              
183             }
184              
185              
186             1;
187              
188             __END__
189              
190