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