Jifty/Script/Model.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 63 CC 4 SCAR 18.3
CC
4
Cov
0%
_write
12.0
run
6.0
options
2.0
CRAP 6.2
1 use warnings;
2 use strict;
3  
4 package Jifty::Script::Model;
5 use base qw/Jifty::Script/;
6  
7 =head1 NAME
8  
9 Jifty::Script::Model - Add a model class to your Jifty application
10  
11 =head1 SYNOPSIS
12  
13 jifty model --name MyFirstModel
14 jifty model --name MyFirstModel --force
15  
16 Options:
17 --name <name> name of the model
18 --force overwrite files
19  
20 --help brief help message
21 --man full documentation
22  
23 =head1 DESCRIPTION
24  
25 Creates a skeleton model file.
26  
27 =head2 options
28  
29 =over 8
30  
31 =item B<--name> NAME (required)
32  
33 Name of the model class.
34  
35 =item B<--force>
36  
37 By default, this will stop and warn you if any of the files it is
38 going to write already exist. Passing the --force flag will make it
39 overwrite the files.
40  
41 =item B<--help>
42  
43 Print a brief help message and exits.
44  
45 =item B<--man>
46  
47 Prints the manual page and exits.
48  
49 =back
50  
51 =cut
52  
53 sub options {
54 my $self = shift;
55 return (
56 $self->SUPER::options,
57 'n|name=s' => 'name',
58 'force' => 'force',
59 )
60 }
61  
62 =head1 DESCRIPTION
63  
64 This creates a skeleton of a new model class for your jifty
65 application, complete with a skeleton of a test suite for it, as well.
66  
67 =head1 METHODS
68  
69 =head2 run
70  
71 Creates a skeleton file under C<lib/I<ApplicationClass>/Model/I<Model>>, as
72 well as a skeleton tests file.
73  
74 =cut
75  
76 sub run {
77 my $self = shift;
78
79 $self->print_help;
80  
81 my $model = $self->{name} || '';
82 $self->print_help("You need to give your new model a --name")
83 unless $model =~ /\w+/;
84  
85 Jifty->new( no_handle => 1 );
86 my $root = Jifty::Util->app_root;
87 my $appclass = Jifty->config->framework("ApplicationClass");
88 my $appclass_path = File::Spec->catfile(split (/::/, $appclass));
89  
90 my $modelFile = <<"EOT";
91 use strict;
92 use warnings;
93  
94 package @{[$appclass]}::Model::@{[$model]};
95 use Jifty::DBI::Schema;
96  
97 use @{[$appclass]}::Record schema {
98  
99 };
100  
101 # Your model-specific methods go here.
102  
103 1;
104  
105 EOT
106  
107  
108 my $testFile = <<"EOT";
109 #!/usr/bin/env perl
110 use warnings;
111 use strict;
112  
113 =head1 DESCRIPTION
114  
115 A basic test harness for the $model model.
116  
117 =cut
118  
119 use Jifty::Test tests => 11;
120  
121 # Make sure we can load the model
122 use_ok('@{[$appclass]}::Model::@{[$model]}');
123  
124 # Grab a system user
125 my \$system_user = @{[$appclass]}::CurrentUser->superuser;
126 ok(\$system_user, "Found a system user");
127  
128 # Try testing a create
129 my \$o = @{[$appclass]}::Model::@{[$model]}->new(current_user => \$system_user);
130 my (\$id) = \$o->create();
131 ok(\$id, "$model create returned success");
132 ok(\$o->id, "New $model has valid id set");
133 is(\$o->id, \$id, "Create returned the right id");
134  
135 # And another
136 \$o->create();
137 ok(\$o->id, "$model create returned another value");
138 isnt(\$o->id, \$id, "And it is different from the previous one");
139  
140 # Searches in general
141 my \$collection = @{[$appclass]}::Model::@{[$model]}Collection->new(current_user => \$system_user);
142 \$collection->unlimit;
143 is(\$collection->count, 2, "Finds two records");
144  
145 # Searches in specific
146 \$collection->limit(column => 'id', value => \$o->id);
147 is(\$collection->count, 1, "Finds one record with specific id");
148  
149 # Delete one of them
150 \$o->delete;
151 \$collection->redo_search;
152 is(\$collection->count, 0, "Deleted row is gone");
153  
154 # And the other one is still there
155 \$collection->unlimit;
156 is(\$collection->count, 1, "Still one left");
157  
158 EOT
159  
160 $self->_write("$root/lib/$appclass_path/Model/$model.pm" => $modelFile,
161 "$root/t/00-model-$model.t" => $testFile,
162 );
163 }
164  
165 sub _write {
166 my $self = shift;
167 my %files = (@_);
168 my $halt;
169 for my $path (keys %files) {
170 my ($volume, $dir, $file) = File::Spec->splitpath($path);
171  
172 # Make sure the directories we need are there
173 Jifty::Util->make_path($dir);
174  
175 # If it already exists, bail
176 if (-e $path and not $self->{force}) {
177 print "File $path exists already; Use --force to overwrite\n";
178 $halt = 11;
179 }
180 }
181 exit if $halt;
182
183 # Now that we've san-checked everything, we can write the files
184 for my $path (keys %files) {
185 print "Writing file $path\n";
186 # Actually write the file out
187 open(FILE, ">$path")
188 or die "Can't write to $path: $!";
189 print FILE $files{$path};
190 close FILE;
191 }
192 }
193  
194 1;