[Rt-commit] r2939 - in DBIx-SearchBuilder/trunk: . SearchBuilder t

glasser at bestpractical.com glasser at bestpractical.com
Tue May 24 00:12:25 EDT 2005


Author: glasser
Date: Tue May 24 00:12:25 2005
New Revision: 2939

Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm
   DBIx-SearchBuilder/trunk/t/01records.t
Log:
 r32877 at tin-foil:  glasser | 2005-05-24 00:10:08 -0400
 Patch from Ruslan:
   * rewrite LoadByPrimaryKeys args handling to consistent with other Load*
   methods
   * report error when PK filed is missing in LoadByPrimaryKeys
   * fix warning in __Set methods when newvalue is undef
   * small code cleanups
   * test coverage grows from 75.2% to 84.7% for Record.pm


Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm	Tue May 24 00:12:25 2005
@@ -676,16 +676,12 @@
         @_
     );
 
-    $args{'Column'}        = $args{'Field'};
-    $args{'IsSQLFunction'} = $args{'IsSQL'};
+    $args{'Column'}        = delete $args{'Field'};
+    $args{'IsSQLFunction'} = delete $args{'IsSQL'};
 
     my $ret = Class::ReturnValue->new();
 
-    ## Cleanup the hash.
-    delete $args{'Field'};
-    delete $args{'IsSQL'};
-
-    unless ( defined( $args{'Column'} ) && $args{'Column'} ) {
+    unless ( $args{'Column'} ) {
         $ret->as_array( 0, 'No column specified' );
         $ret->as_error(
             errno        => 5,
@@ -695,23 +691,23 @@
         return ( $ret->return_value );
     }
     my $column = lc $args{'Column'};
-    if (    ( defined $self->__Value($column) )
-        and ( $args{'Value'} eq $self->__Value($column) ) )
-    {
-        $ret->as_array( 0, "That is already the current value" );
+    if ( !defined( $args{'Value'} ) ) {
+        $ret->as_array( 0, "No value passed to _Set" );
         $ret->as_error(
-            errno        => 1,
+            errno        => 2,
             do_backtrace => 0,
-            message      => "That is already the current value"
+            message      => "No value passed to _Set"
         );
         return ( $ret->return_value );
     }
-    elsif ( !defined( $args{'Value'} ) ) {
-        $ret->as_array( 0, "No value passed to _Set" );
+    elsif (    ( defined $self->__Value($column) )
+        and ( $args{'Value'} eq $self->__Value($column) ) )
+    {
+        $ret->as_array( 0, "That is already the current value" );
         $ret->as_error(
-            errno        => 2,
+            errno        => 1,
             do_backtrace => 0,
-            message      => "No value passed to _Set"
+            message      => "That is already the current value"
         );
         return ( $ret->return_value );
     }
@@ -1034,18 +1030,15 @@
 
 
 sub LoadByPrimaryKeys {
-    my ($self, $data) = @_;
+    my $self = shift;
+    my $data = (ref $_[0] eq 'HASH')? $_[0]: {@_};
 
-    if (ref($data) eq 'HASH') {
-       my %cols=();
-       foreach (@{$self->_PrimaryKeys}) {
-         $cols{$_}=$data->{$_} if (exists($data->{$_}));
-       }
-       return ($self->LoadByCols(%cols));
-    } 
-    else { 
-      return (0, "Invalid data");
+    my %cols=();
+    foreach (@{$self->_PrimaryKeys}) {
+	return (0, "Missing PK field: '$_'") unless defined $data->{$_};
+	$cols{$_}=$data->{$_};
     }
+    return ($self->LoadByCols(%cols));
 }
 
 # }}}

Modified: DBIx-SearchBuilder/trunk/t/01records.t
==============================================================================
--- DBIx-SearchBuilder/trunk/t/01records.t	(original)
+++ DBIx-SearchBuilder/trunk/t/01records.t	Tue May 24 00:12:25 2005
@@ -8,7 +8,7 @@
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 44;
+use constant TESTS_PER_DRIVER => 64;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -77,29 +77,23 @@
 # Validate truncation on update
 
 	($val,$msg) = $rec->SetName('1234567890123456789012345678901234567890');
-
-	ok($val, $msg) ;
-
+	ok($val, $msg);
 	is($rec->Name, '12345678901234', "Truncated on update");
-
+	$val = $rec->TruncateValue(Phone => '12345678901234567890');
+	is($val, '123456789012345678', 'truncate by length attribute');
 
 
 # Test unicode truncation:
 	my $univalue = "這是個測試";
-
 	($val,$msg) = $rec->SetName($univalue.$univalue);
-
 	ok($val, $msg) ;
-
 	is($rec->Name, '這是個測');
 
 
 
 # make sure we do _not_ truncate things which should not be truncated
 	($val,$msg) = $rec->SetEmployeeId('1234567890');
-
 	ok($val, $msg) ;
-
 	is($rec->EmployeeId, '1234567890', "Did not truncate id on create");
 
 # make sure we do truncation on create
@@ -141,6 +135,61 @@
 	is($val, 0, "didn't find object");
 	is($msg, "Couldn't execute query", "reason is bad SQL");
 
+# test Load* methods
+	$newrec = TestApp::Address->new($handle);
+	$newrec->Load();
+	is( $newrec->id, undef, "can't load record with undef id");
+
+	$newrec = TestApp::Address->new($handle);
+	$newrec->LoadByCol( Name => '12345678901234' );
+	is( $newrec->id, $newid, "load record by 'Name' column value");
+
+# LoadByCol with operator
+	$newrec = TestApp::Address->new($handle);
+	$newrec->LoadByCol( Name => { value => '%45678%',
+				      operator => 'LIKE' } );
+	is( $newrec->id, $newid, "load record by 'Name' with LIKE");
+
+# LoadByPrimaryKeys
+	$newrec = TestApp::Address->new($handle);
+	($val, $msg) = $newrec->LoadByPrimaryKeys( id => $newid );
+	ok( $val, "load record by PK");
+	is( $newrec->id, $newid, "loaded correct record");
+	$newrec = TestApp::Address->new($handle);
+	($val, $msg) = $newrec->LoadByPrimaryKeys( {id => $newid} );
+	ok( $val, "load record by PK");
+	is( $newrec->id, $newid, "loaded correct record" );
+	$newrec = TestApp::Address->new($handle);
+	($val, $msg) = $newrec->LoadByPrimaryKeys( Phone => 'some' );
+	ok( !$val, "couldn't load, missing PK field");
+	is( $msg, "Missing PK field: 'id'", "right error message" );
+
+# LoadByCols and empty or NULL values
+	$rec = TestApp::Address->new($handle);
+	$id = $rec->Create( Name => 'Obra', Phone => undef );
+	ok( $id, "new record");
+	$rec = TestApp::Address->new($handle);
+#	$rec->LoadByCols( Name => 'Obra', Phone => undef, EmployeeId => '' );
+#	Fails under Pg
+#    is( $rec->id, $id, "loaded record by empty value" );
+
+# __Set error paths
+	$rec = TestApp::Address->new($handle);
+	$rec->Load( $id );
+	$val = $rec->SetName( 'Obra' );
+	isa_ok( $val, 'Class::ReturnValue', "couldn't set same value, error returned");
+	is( ($val->as_array)[1], "That is already the current value", "correct error message" );
+	is( $rec->Name, 'Obra', "old value is still there");
+	$val = $rec->SetName( 'invalid' );
+	isa_ok( $val, 'Class::ReturnValue', "couldn't set invalid value, error returned");
+	is( ($val->as_array)[1], 'Illegal value for Name', "correct error message" );
+	is( $rec->Name, 'Obra', "old value is still there");
+# XXX TODO FIXME: this test cover current implementation that is broken //RUZ
+	$val = $rec->SetName( );
+	isa_ok( $val, 'Class::ReturnValue', "couldn't set empty/undef value, error returned");
+	is( ($val->as_array)[1], "No value passed to _Set", "correct error message" );
+	is( $rec->Name, 'Obra', "old value is still there");
+
 # deletes
 	$newrec = TestApp::Address->new($handle);
 	$newrec->Load( $newid );
@@ -167,6 +216,13 @@
     $self->_Handle($handle);
 }
 
+sub ValidateName
+{
+	my ($self, $value) = @_;
+	return 0 if $value =~ /invalid/i;
+	return 1;
+}
+
 sub _ClassAccessible {
 
     {   
@@ -176,7 +232,7 @@
         Name => 
         {read => 1, write => 1, type => 'varchar(14)', default => ''},
         Phone => 
-        {read => 1, write => 1, type => 'varchar(18)', default => ''},
+        {read => 1, write => 1, type => 'varchar(18)', length => 18, default => ''},
         EmployeeId => 
         {read => 1, write => 1, type => 'int(8)', default => ''},
 


More information about the Rt-commit mailing list