--- bin/ldap2model.pl 2008/04/30 23:44:43 99 +++ bin/ldap2model.pl 2008/05/01 13:41:44 102 @@ -32,6 +32,8 @@ /; my $methods; +my $create; +my $columns; sub entry { my ( $e, $add ) = @_; @@ -42,6 +44,7 @@ $out .= qq/,\n\t\tmax_length is $_->{max_length}/ if $_->{'max_length'}; $out .= qq/,\n\t\t$add/ if $add; $out .= qq/;\n\n/; + $columns->{$name}++; return $out; } @@ -49,9 +52,11 @@ $model .= qq/\t# $objectClass must:\n\n/; + map { warn "# $objectClass must: ",dump( $_ ); $model .= entry( $_, 'is mandatory' ); + $create->{$_->{name}} = $_->{name}; } $schema->must( $objectClass ); $model .= qq/\t# $objectClass may:\n\n/; @@ -61,6 +66,8 @@ $model .= entry( $_ ); } $schema->may( $objectClass ); +$methods .= qq/sub name { \$_[0]->id }\n/ unless $columns->{name}; + $model .= qq/ }; @@ -75,3 +82,70 @@ my $model_path = "lib/A3C/Model/$objectClass.pm"; write_file( $model_path, $model ); warn "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 _objectClass_ model. + +=cut + +use Jifty::Test tests => 11; + +# Make sure we can load the model +use_ok('A3C::Model::_objectClass_'); + +# 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::_objectClass_->new(current_user => $system_user); +my ($id) = $o->create( +_create_1_); +ok($id, "_objectClass_ create returned success"); +ok($o->id, "New _objectClass_ has valid id set"); +is($o->id, $id, "Create returned the right id"); + +# And another +$o->create( +_create_2_); +ok($o->id, "_objectClass_ create returned another value"); +isnt($o->id, $id, "And it is different from the previous one"); + +# Searches in general +my $collection = A3C::Model::_objectClass_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/_objectClass_/$objectClass/gs; + +foreach my $round ( 1 .. 2 ) { + my $data; + $data .= qq/\t\t'$_' => '$_ $round',\n/ foreach keys %$create; + warn "data = $data\n"; + $test =~ s/_create_${round}_/$data/gs; +} + +my $test_path = "t/00-model-$objectClass.t"; +write_file( $test_path, $test ); +warn "Created $test_path\n"; +chmod 0755, $test_path;