Sims/Types.pm

filter: Statementstmt 100% 104 / 104 Branchbran 97.7% 43 / 44 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 16 / 16 Podpod 100% 12 / 12 totaltotal 95.5% 194 / 203 CC 30 SCAR 17.2
CC
30
Cov
97%
us_phone
9.0
email_address
9.0
us_zipcode
7.0
CRAP 5.6
« Sims.pm ↑ Summary
1 package DBIx::Class::Sims::Types;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use DBIx::Class::Sims;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 DBIx::Class::Sims->set_sim_types({
8 23 map { $_ => __PACKAGE__->can($_) } qw(
9 email_address ip_address
10 us_firstname us_lastname us_name
11 us_address us_city us_county us_phone us_ssntin us_state us_zipcode
12 )
13 });
14  
15 23 ▶use String::Random qw( random_regex random_string );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 {
18 23 my @tlds = qw(
19 com net org gov mil co.uk co.es
20 );
21  
22 sub email_address {
23 102 ▶ my ($info) = @_;
Subroutine email_address: called CC=9 SCAR=22.0
Pod documented
24  
25 102 ▶ my $size = $info->{size} || 7;
Condition50%1/2
$info->{'size'} || 7
l!l
1020
MC/DC0%0/1
$info->{'size'} || 7
$info->{'size'}
Truth table50%1/2
A
A= $info->{'size'}
Aresult
11
00
26 102 ▶ if ( $size < 7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < 7) 6 96 102
27 6 return '';
28 }
29  
30 96 my $tld = $tlds[rand @tlds];
31 96 ▶ while ( $size - length($tld) < 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size - length($tld) < 4) 1 96 97
32 1 $tld = $tlds[rand @tlds];
33 }
34  
35 # Don't always create an address to fill the full amount.
36 96 ▶ if ( $size > 20 && rand() < .5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 20 and rand() < "0.5") 41 55 96
Condition100%3/3
$size > 20 and rand() < "0.5"
!ll&&!rl&&r
144141
MC/DC100%2/2
$size > 20 and rand() < "0.5"
$size > 20 rand() < "0.5"
Truth table100%3/3
A and B
A= $size > 20B= rand() < "0.5"
ABresult
0X0
100
111
37 41 $size -= int(rand($size-20));
38 }
39  
40 96 $size -= length($tld) + 1 + 1; # + . for $tld + @
41  
42 # Split size evenly-ish, but with randomness
43 96 my $acct_size = int($size/2);
44 96 $size -= $acct_size;
45  
46 96 my $acct = random_string( "0"x$acct_size, ['a'..'z','A'..'Z','0'..'9'] );
47 96 ▶ if ( $acct_size > 5 && rand() < 0.1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acct_size > 5 and rand() < "0.1") 11 85 96
Condition100%3/3
$acct_size > 5 and rand() < "0.1"
!ll&&!rl&&r
117411
MC/DC100%2/2
$acct_size > 5 and rand() < "0.1"
$acct_size > 5 rand() < "0.1"
Truth table100%3/3
A and B
A= $acct_size > 5B= rand() < "0.1"
ABresult
0X0
100
111
48 11 my $n = int(rand($acct_size - 4)) + 2;
49 11 substr($acct, $n, 1) = '+';
50 }
51  
52 96 my $domain = random_string( "c"x$size );
53 96 ▶ if ( $size > 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 5) 87 9 96
54 87 my $n = int(rand($size - 4)) + 2;
55 87 substr($domain, $n, 1) = '.';
56 }
57  
58 96 return "${acct}\@${domain}.${tld}";
59 }
60 }
61  
62 23 sub ip_address {
63 2000 ▶ return join '.', map { int(rand(255)) + 1 } 1 .. 4;
Subroutine ip_address: called CC=1 SCAR=0.0
Pod documented
64 }
65  
66 {
67 23 my @street_names = qw(
68 Main Court House Mill Wood Millwood
69 First Second Third Fourth Fifth Sixth Seventh Eight Ninth
70 Magnolia Acacia Poppy Cherry Rose Daisy Daffodil
71 );
72  
73 23 my @street_types = qw(
74 Street Drive Place Avenue Boulevard Lane
75 St Dr Pl Av Ave Blvd Ln
76 St. Dr. Pl. Av. Ave. Blvd. Ln.
77 );
78  
79 sub us_address {
80 # Assume a varchar-like column type with enough space.
81  
82 1000 ▶ if ( rand() < .7 ) {
Subroutine us_address: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < 0.7) { } 707 293 1000
83 # We want to change this so that distribution is by number of digits, then
84 # randomly within the numbers.
85 707 my $number = int(rand(99999));
86  
87 707 my $street_name = $street_names[rand @street_names];
88 707 my $street_type = $street_types[rand @street_types];
89  
90 707 return "$number $street_name $street_type";
91 }
92 else {
93 293 ▶ my $po = rand() < .5 ? 'PO' : 'P.O.';
Branch: 1 branch
BranchTrueFalseTotal
rand() < 0.5 ? : 150 143 293
94 293 return "$po Box " . int(rand(9999));
95 }
96 }
97 }
98  
99 {
100 23 my @city_names = qw(
101 Ithaca Jonestown Marysville Ripon Minneapolis Miami Paris London Columbus
102 );
103 23 push @city_names, (
104 'New York', 'Los Angeles', 'Montego By The Bay',
105 );
106  
107 sub us_city {
108 # Assume a varchar-like column type with enough space.
109 1 ▶ return $city_names[rand @city_names];
Subroutine us_city: called CC=1 SCAR=0.0
Pod documented
110 }
111 }
112  
113 {
114 23 my @county_names = qw(
115 Adams Madison Washinton Union Clark
116 );
117  
118 sub us_county {
119 # Assume a varchar-like column type with enough space.
120 1 ▶ return $county_names[rand @county_names];
Subroutine us_county: called CC=1 SCAR=0.0
Pod documented
121 }
122 }
123  
124 {
125 23 my @first_names = qw(
126 Aidan Bill Charles Doug Evan Frank George Hunter Ilya Jeff Kilgore
127 Liam Michael Nathan Oscar Perry Robert Shawn Thomas Urkul Victor Xavier
128  
129 Alexandra Betty Camille Debra Ellen Fatima Georgette Hettie Imay Jaime
130 Kathrine Leticia Margaret Nellie Ophelia Patsy Regina Sybil Tricia Valerie
131 );
132  
133 sub us_firstname {
134 # Assume a varchar-like column type with enough space.
135 2000 ▶ return $first_names[rand @first_names],
Subroutine us_firstname: called CC=1 SCAR=0.0
Pod documented
136 }
137 }
138  
139 {
140 23 my @last_names = qw(
141 Jones Smith Taylor Kinyon Williams Shaner Perry Raymond Moore O'Malley
142 );
143 # Some last names are two words.
144 23 push @last_names, (
145 "Von Trapp", "Van Kirk",
146 );
147  
148 sub us_lastname {
149 # Assume a varchar-like column type with enough space.
150 2000 ▶ return $last_names[rand @last_names],
Subroutine us_lastname: called CC=1 SCAR=0.0
Pod documented
151 }
152 }
153  
154 {
155 23 my @letters = ( 'A' .. 'Z' );
156  
157 23 my @suffixes = (
158 'Jr', 'Sr', 'II', 'III', 'IV', 'Esq.',
159 );
160  
161 sub us_name {
162 # Assume a varchar-like column type with enough space.
163  
164 1000 ▶ my @name = us_firstname(@_);
Subroutine us_name: called CC=3 SCAR=11.0
Pod documented
165  
166 # 10% chance of adding a middle initial
167 1000 ▶ if ( rand() < 0.1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.1") 107 893 1000
168 107 push @name, $letters[rand @letters] . '.';
169 }
170  
171 1000 push @name, us_lastname(@_);
172  
173 # 10% chance of adding a suffix
174 1000 ▶ if ( rand() < 0.1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.1") 116 884 1000
175 116 push @name, $suffixes[rand @suffixes];
176 }
177  
178 1000 return join ' ', @name;
179 }
180 }
181  
182 23 sub us_phone {
183 10 ▶ my ($info) = @_;
Subroutine us_phone: called CC=9 SCAR=22.0
Pod documented
184  
185 # Assume a varchar-like column type.
186 10 ▶ my $length = $info->{size} || 8;
Condition50%1/2
$info->{'size'} || 8
l!l
100
MC/DC0%0/1
$info->{'size'} || 8
$info->{'size'}
Truth table50%1/2
A
A= $info->{'size'}
Aresult
11
00
187 10 ▶ if ( $length < 7 ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($length < 7) { } 1 9 10
elsif ($length == 7) { } 1 8 9
elsif ($length < 10) { } 2 6 8
elsif ($length < 12) { } 2 4 6
elsif ($length == 12) { } 1 3 4
elsif ($length == 13) { } 1 2 3
elsif ($length >= 14) { } 2 0 2
188 1 return '';
189 }
190 elsif ( $length == 7 ) {
191 1 return random_regex('\d{7}');
192 }
193 elsif ( $length < 10 ) {
194 2 return random_regex('\d{3}-\d{4}');
195 }
196 elsif ( $length < 12 ) {
197 2 return random_regex('\d{10}');
198 }
199 elsif ( $length == 12 ) {
200 1 return random_regex('\d{3}-\d{3}-\d{4}');
201 }
202 # random_regex() throws a warning no matter how I try to specify the parens.
203 # It does the right thing, but noisily. So, just concatenate them.
204 elsif ( $length == 13 ) {
205 1 return '(' . random_regex('\d{3}') . ')' . random_regex('\d{3}-\d{4}');
206 }
207 elsif ( $length >= 14 ) {
208 2 return '(' . random_regex('\d{3}') . ') ' . random_regex('\d{3}-\d{4}');
209 }
210 }
211  
212 sub us_ssntin {
213 # Give strong preference to a SSN
214 1000 ▶ if ( rand() < .8 ) {
Subroutine us_ssntin: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < 0.8) { } 822 178 1000
215 822 return random_regex('\d{3}-\d{2}-\d{4}');
216 }
217 # But still generate employer TINs to mix it up.
218 else {
219 178 return random_regex('\d{2}-\d{7}');
220 }
221 }
222  
223 {
224 23 my @states = (
225 [ AL => 'Alabama' ],
226 [ AK => 'Alaska' ],
227 [ AZ => 'Arizona' ],
228 [ AR => 'Arkansas' ],
229 [ CA => 'California' ],
230 [ CO => 'Colorado' ],
231 [ CT => 'Connecticut' ],
232 [ DE => 'Delaware' ],
233 [ FL => 'Florida' ],
234 [ GA => 'Georgia' ],
235 [ HI => 'Hawaii' ],
236 [ ID => 'Idaho' ],
237 [ IL => 'Illinois' ],
238 [ IN => 'Indiana' ],
239 [ IA => 'Iowa' ],
240 [ KS => 'Kansas' ],
241 [ KY => 'Kentucky' ],
242 [ LA => 'Louisiana' ],
243 [ ME => 'Maine' ],
244 [ MD => 'Maryland' ],
245 [ MA => 'Massachusetts' ],
246 [ MI => 'Michigan' ],
247 [ MN => 'Minnesota' ],
248 [ MS => 'Mississippi' ],
249 [ MO => 'Missouri' ],
250 [ MT => 'Montana' ],
251 [ NE => 'Nebraska' ],
252 [ NJ => 'New Jersey' ],
253 [ NH => 'New Hampshire' ],
254 [ NV => 'Nevada' ],
255 [ NM => 'New Mexico' ],
256 [ NY => 'New York' ],
257 [ NC => 'North Carolina' ],
258 [ ND => 'North Dakota' ],
259 [ OH => 'Ohio' ],
260 [ OK => 'Oklahoma' ],
261 [ OR => 'Oregon' ],
262 [ PA => 'Pennsylvania' ],
263 [ RI => 'Rhode Island' ],
264 [ SC => 'South Carolina' ],
265 [ SD => 'South Dakota' ],
266 [ TN => 'Tennessee' ],
267 [ TX => 'Texas' ],
268 [ UT => 'Utah' ],
269 [ VT => 'Vermont' ],
270 [ VA => 'Virginia' ],
271 [ WA => 'Washington' ],
272 [ WV => 'West Virginia' ],
273 [ WI => 'Wisconsin' ],
274 [ WY => 'Wyoming' ],
275 # These are territories, not states, but that's okay.
276 [ AS => 'American Samoa' ],
277 [ DC => 'District Of Columbia' ],
278 [ GU => 'Guam' ],
279 [ MD => 'Midway Islands' ],
280 [ NI => 'Northern Mariana Islands' ],
281 [ PR => 'Puerto Rico' ],
282 [ VI => 'Virgin Islands' ],
283 );
284 sub us_state {
285 3 ▶ my ($info) = @_;
Subroutine us_state: called CC=3 SCAR=11.0
Pod documented
286  
287 # Assume a varchar-like column type.
288 3 ▶ my $length = $info->{size} || 2;
Condition50%1/2
$info->{'size'} || 2
l!l
30
MC/DC0%0/1
$info->{'size'} || 2
$info->{'size'}
Truth table50%1/2
A
A= $info->{'size'}
Aresult
11
00
289 3 ▶ if ( $length == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 2) 1 2 3
290 1 return $states[rand @states][0];
291 }
292 2 return substr($states[rand @states][1], 0, $length);
293 }
294 }
295  
296 23 sub us_zipcode {
297 13 ▶ my ($info) = @_;
Subroutine us_zipcode: called CC=7 SCAR=19.5
Pod documented
298  
299 13 my $datatype = $info->{data_type};
300 13 ▶ if ( $datatype eq 'varchar' || $datatype eq 'char' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($datatype eq 'varchar' or $datatype eq 'char') { } 12 1 13
Condition66%2/3
$datatype eq "varchar" or $datatype eq "char"
l!l&&r!l&&!r
1201
MC/DC50%1/2
$datatype eq "varchar" or $datatype eq "char"
$datatype eq "varchar" $datatype eq "char"
Truth table66%2/3
A or B
A= $datatype eq "varchar"B= $datatype eq "char"
ABresult
1X1
011
000
301 12 ▶ my $length = $info->{size} || 9;
Condition100%2/2
$info->{'size'} || 9
l!l
111
MC/DC100%1/1
$info->{'size'} || 9
$info->{'size'}
Truth table100%2/2
A
A= $info->{'size'}
Aresult
11
00
302 12 ▶ if ( $length < 5 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length < 5) { } 4 8 12
elsif ($length < 9) { } 4 4 8
elsif ($length == 9) { } 2 2 4
303 4 return '';
304 }
305 elsif ( $length < 9 ) {
306 4 return random_regex('\d{5}');
307 }
308 elsif ( $length == 9 ) {
309 2 return random_regex('\d{9}');
310 }
311 else {
312 2 return random_regex('\d{5}-\d{4}');
313 }
314 }
315 # Treat it as an int.
316 else {
317 1 return int(rand(99999));
318 }
319 }
320  
321 23 1;
322 __END__
323  
324 =head1 NAME
325  
326 DBIx::Class::Sims::Types
327  
328 =head1 PURPOSE
329  
330 These are pre-defined sim types for using with L<DBIx::Class::Sims>.
331  
332 =head2 TYPES
333  
334 The following sim types are pre-defined:
335  
336 =over 4
337  
338 =item * email_address
339  
340 This generates a reasonable-looking email address. The account and server names
341 are randomly generated. The TLD is selected from a list of TLDs, including
342 'co.uk' (so be warned). If the server name is large enough, a '.' will be added
343 to create a 2-level name.
344  
345 There is a small chance that a more complex email address will be used. These
346 email addresses are ones that are more likely to break poorly-written validator
347 checks. Some real-life (completely legal) examples are:
348  
349 =over 4
350  
351 =item * rob.kinyon+lists@gmail.com
352  
353 =back
354  
355 =item * ip_address
356  
357 This generates a reasonable-looking IP address.
358  
359 =item * us_address
360  
361 This generates a reasonable-looking US street address. The address will be one
362 of these forms:
363  
364 =over 4
365  
366 =item * "#### Name Type", so something like "123 Main Street"
367  
368 =item * "PO Box ####", so something like "PO Box 13579"
369  
370 =item * "P.O. Box ####", so something like "P.O. Box 97531"
371  
372 =back
373  
374 =item * us_city
375  
376 This generates a reasonable-looking US city name.
377  
378 =item * us_county
379  
380 This generates a reasonable-looking US county name.
381  
382 =item * us_firstname
383  
384 This generates a reasonable-looking US person first name. It will be randomized
385 as to gender.
386  
387 =item * us_lastname
388  
389 This generates a reasonable-looking US person last name. It may contain one
390 word, two words, or an apostrophized word.
391  
392 =item * us_name
393  
394 This generates a reasonable-looking US person name. The first and last names
395 will be generated from us_firstname and us_lastname, respectively. There is a
396 small chance a suffix will be appended.
397  
398 =item * us_phone
399  
400 This generates a reasonable-looking US phone-number, based on the size of the
401 column being filled. The column is assumed to be a character-type column
402 (varchar, etc). If the size of the column is less than 10, there will be no area
403 code. If there is space, hyphens and parentheses will be added in the right
404 places. If the column is long enough, the value will look like "(###) ###-####"
405  
406 Phone extensions are not supported at this time.
407  
408 =item * us_ssntin
409  
410 This generates a reasonable-looking US Social Security Number (SSN) or Tax
411 Identification Number (TIN). These are government identifiers that are often
412 usable as unique personal IDs. An SSN is a personal ID number and a TIN is a
413 corporate ID number.
414  
415 =item * us_state
416  
417 This generates a random US state or territory (so 57 choices). The column is
418 assumed to be able to take a US state as a value. If the size of the column is 2
419 (the default), then the abbreviation will be returned. Otherwise, the first N
420 characters of the name (where N is the size) will be returned.
421  
422 =item * us_zipcode
423  
424 This generates a reasonable-looking US zipcode. If the column is numeric, it
425 generates a number between 1 and 99999. Otherwise, it generates a legal string
426 of numbers (with a possible dash for a 5+4) that will fit within the column's
427 width.
428  
429 =back
430  
431 The reason why several of the pre-defined sim types have the country prefixed is
432 because different countries do things differently. (Shocker, I know!)
433  
434 =head1 AUTHOR
435  
436 Rob Kinyon <rob.kinyon@gmail.com>
437  
438 =head1 LICENSE
439  
440 Copyright (c) 2013 Rob Kinyon. All Rights Reserved.
441 This is free software, you may use it and distribute it under the same terms
442 as Perl itself.
443  
444 =cut
« Sims.pm ↑ Summary