| Revision 181 (by dpavlin, 2008/06/16 20:08:28) |
make ldap2model use JiftyModelCreator and minor tweaks all over the place |
package JiftyModelCreator;
use strict;
use warnings;
use File::Slurp;
use Data::Dump qw/dump/;
sub write {
my $self = shift;
my ( $ModelName, $model, $create ) = @_;
my $model_path = "lib/A3C/Model/$ModelName.pm";
$model_path .= '.orig' if -e $model_path;
write_file( $model_path, $model );
print "Created $model_path\n";
my $test = <<'__END_OF_TEST__';
#!/usr/bin/env perl
use warnings;
use strict;
=head1 DESCRIPTION
A basic test harness for the _ModelName_ model.
=cut
use Jifty::Test tests => 11;
# Make sure we can load the model
use_ok('A3C::Model::_ModelName_');
# Grab a system user
my $system_user = A3C::CurrentUser->superuser;
ok($system_user, "Found a system user");
# Try testing a create
my $o = A3C::Model::_ModelName_->new(current_user => $system_user);
my ($id) = $o->create(
_create_1_);
ok($id, "_ModelName_ create returned success");
ok($o->id, "New _ModelName_ has valid id set");
is($o->id, $id, "Create returned the right id");
# And another
$o->create(
_create_2_);
ok($o->id, "_ModelName_ create returned another value");
isnt($o->id, $id, "And it is different from the previous one");
# Searches in general
my $collection = A3C::Model::_ModelName_Collection->new(current_user => $system_user);
$collection->unlimit;
is($collection->count, 2, "Finds two records");
# Searches in specific
$collection->limit(column => 'id', value => $o->id);
is($collection->count, 1, "Finds one record with specific id");
# Delete one of them
$o->delete;
$collection->redo_search;
is($collection->count, 0, "Deleted row is gone");
# And the other one is still there
$collection->unlimit;
is($collection->count, 1, "Still one left");
__END_OF_TEST__
$test =~ s/_ModelName_/$ModelName/gs;
#warn dump( $create );
foreach my $round ( 1 .. 2 ) {
my $data;
if ( $create ) {
$data .= qq/\t\t'$_' => / . dump( $create->{$_} ) . qq/,\n/ foreach keys %$create;
}
$test =~ s/_create_${round}_/$data/gs;
}
my $test_path = "t/00-model-$ModelName.t";
$test_path .= '.orig' if -e $test_path;
write_file( $test_path, $test );
print "Created $test_path\n";
chmod 0755, $test_path;
} # create_model
1;