1 |
package JiftyModelCreator; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use File::Slurp; |
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
sub write { |
10 |
my $self = shift; |
11 |
|
12 |
my ( $ModelName, $model, $create ) = @_; |
13 |
|
14 |
my $model_path = "lib/A3C/Model/$ModelName.pm"; |
15 |
|
16 |
$model_path .= '.orig' if -e $model_path; |
17 |
|
18 |
write_file( $model_path, $model ); |
19 |
print "Created $model_path\n"; |
20 |
|
21 |
my $test = <<'__END_OF_TEST__'; |
22 |
#!/usr/bin/env perl |
23 |
use warnings; |
24 |
use strict; |
25 |
|
26 |
=head1 DESCRIPTION |
27 |
|
28 |
A basic test harness for the _ModelName_ model. |
29 |
|
30 |
=cut |
31 |
|
32 |
use Jifty::Test tests => 11; |
33 |
|
34 |
# Make sure we can load the model |
35 |
use_ok('A3C::Model::_ModelName_'); |
36 |
|
37 |
# Grab a system user |
38 |
my $system_user = A3C::CurrentUser->superuser; |
39 |
ok($system_user, "Found a system user"); |
40 |
|
41 |
# Try testing a create |
42 |
my $o = A3C::Model::_ModelName_->new(current_user => $system_user); |
43 |
my ($id) = $o->create( |
44 |
_create_1_); |
45 |
ok($id, "_ModelName_ create returned success"); |
46 |
ok($o->id, "New _ModelName_ has valid id set"); |
47 |
is($o->id, $id, "Create returned the right id"); |
48 |
|
49 |
# And another |
50 |
$o->create( |
51 |
_create_2_); |
52 |
ok($o->id, "_ModelName_ create returned another value"); |
53 |
isnt($o->id, $id, "And it is different from the previous one"); |
54 |
|
55 |
# Searches in general |
56 |
my $collection = A3C::Model::_ModelName_Collection->new(current_user => $system_user); |
57 |
$collection->unlimit; |
58 |
is($collection->count, 2, "Finds two records"); |
59 |
|
60 |
# Searches in specific |
61 |
$collection->limit(column => 'id', value => $o->id); |
62 |
is($collection->count, 1, "Finds one record with specific id"); |
63 |
|
64 |
# Delete one of them |
65 |
$o->delete; |
66 |
$collection->redo_search; |
67 |
is($collection->count, 0, "Deleted row is gone"); |
68 |
|
69 |
# And the other one is still there |
70 |
$collection->unlimit; |
71 |
is($collection->count, 1, "Still one left"); |
72 |
__END_OF_TEST__ |
73 |
|
74 |
$test =~ s/_ModelName_/$ModelName/gs; |
75 |
|
76 |
#warn dump( $create ); |
77 |
|
78 |
foreach my $round ( 1 .. 2 ) { |
79 |
my $data; |
80 |
if ( $create ) { |
81 |
$data .= qq/\t\t'$_' => / . dump( $create->{$_} ) . qq/,\n/ foreach keys %$create; |
82 |
} |
83 |
$test =~ s/_create_${round}_/$data/gs; |
84 |
} |
85 |
|
86 |
my $test_path = "t/00-model-$ModelName.t"; |
87 |
$test_path .= '.orig' if -e $test_path; |
88 |
write_file( $test_path, $test ); |
89 |
print "Created $test_path\n"; |
90 |
chmod 0755, $test_path; |
91 |
|
92 |
} # create_model |
93 |
|
94 |
1; |