[Rt-devel] [PATCH] DBIx-SB: new tests, fixes and cleanups

Ruslan U. Zakirov Ruslan.Zakirov at miet.ru
Mon May 23 20:35:38 EDT 2005


	Heya!
Changes:
* 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

Tests:
* coverage grows from 75.2% to 84.7% for Record.pm

--
Best regards, Ruslan.
-------------- next part --------------
==== Patch <tests> level 2
Source: fac90757-c5f0-0310-a953-bfb799f65e4e:/DBIx-SearchBuilder/local:1668
Target: e417ac7c-1bcc-0310-8ffa-8f5827389a85:/DBIx-SearchBuilder/trunk:2931
        (svn://svn.bestpractical.com/DBIx-SearchBuilder)
Log:
 r233 at colinux:  cubic | 2005-02-25 15:44:38 +0000
 local copy
 r242 at colinux:  cubic | 2005-04-04 20:17:35 +0000
 M::I path update
 r1621 at colinux:  cubic | 2005-04-07 13:53:26 +0000
 nocap API test
 r1622 at colinux:  cubic | 2005-04-07 14:00:13 +0000
 add inc/ file for auto install feature
 r1623 at colinux:  cubic | 2005-04-07 16:11:05 +0000
 test suit changes:
   @AvailableDrivers array of the drivers that has DBD:: module installed
   connect_handle, connect_sqlite functions
 r1629 at colinux:  cubic | 2005-04-06 13:28:07 +0000
 cross DB test suite framework
 r1632 at colinux:  cubic | 2005-05-20 19:56:24 +0000
 cleanup_schema function integration
 r1655 at colinux:  cubic | 2005-05-21 12:26:07 +0000
 AUTOLOAD cleanup, goto &$AUTOLOAD syntax
 r1656 at colinux:  cubic | 2005-05-21 23:02:37 +0000
 fix&cleanup _LoadFromSQL behaviour:
 when PK fields missed
 when fetchrow_hashref returns undef
 clean fetched hash after fetchrow and fill it only before successful exit
 right error reporting on SimpleQuery failure
 r1657 at colinux:  cubic | 2005-05-22 09:36:59 +0000
 [TESTS] check that we really delete record
 r1666 at colinux:  cubic | 2005-05-24 00:13:54 +0000
 rewrite LoadByPrimaryKeys args handling to consistent with other Load* methods
 report error when PK filed is missing in LoadByPrimaryKeys
 fix wanrning in __Set methods when newvalue is undef
 small code cleanups
 r1667 at colinux:  cubic | 2005-05-24 00:24:47 +0000
 new tests, coverage grows from 75.2% to 84.7%
 r1668 at colinux:  cubic | 2005-05-24 00:29:47 +0000
 revert inc/* to trunk state

=== t/01records.t
==================================================================
--- t/01records.t  (revision 2931)
+++ t/01records.t  (patch tests level 2)
@@ -8,7 +8,7 @@
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 44;
+use constant TESTS_PER_DRIVER => 65;
 
 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,60 @@
 	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 => '' );
+	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 +215,13 @@
     $self->_Handle($handle);
 }
 
+sub ValidateName
+{
+	my ($self, $value) = @_;
+	return 0 if $value =~ /invalid/i;
+	return 1;
+}
+
 sub _ClassAccessible {
 
     {   
@@ -176,7 +231,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 => ''},
 
=== SearchBuilder/Record.pm
==================================================================
--- SearchBuilder/Record.pm  (revision 2931)
+++ SearchBuilder/Record.pm  (patch tests level 2)
@@ -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,7 +691,16 @@
         return ( $ret->return_value );
     }
     my $column = lc $args{'Column'};
-    if (    ( defined $self->__Value($column) )
+    if ( !defined( $args{'Value'} ) ) {
+        $ret->as_array( 0, "No value passed to _Set" );
+        $ret->as_error(
+            errno        => 2,
+            do_backtrace => 0,
+            message      => "No value passed to _Set"
+        );
+        return ( $ret->return_value );
+    }
+    elsif (    ( defined $self->__Value($column) )
         and ( $args{'Value'} eq $self->__Value($column) ) )
     {
         $ret->as_array( 0, "That is already the current value" );
@@ -706,15 +711,6 @@
         );
         return ( $ret->return_value );
     }
-    elsif ( !defined( $args{'Value'} ) ) {
-        $ret->as_array( 0, "No value passed to _Set" );
-        $ret->as_error(
-            errno        => 2,
-            do_backtrace => 0,
-            message      => "No value passed to _Set"
-        );
-        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));
 }
 
 # }}}

==== BEGIN SVK PATCH BLOCK ====
Version: svk 1.0_01 (linux)

eJyVV2tzG9UZVsNKtmyXhAmBmsDMgchx4viivUpawMiJLcexEzuxw5gyGXG0e1ZavNoVe7GtRBSO
bOVCYkKaS0m4hU5iB08xk/A1Hwn9Wv5BZ9qZ9gf0M/TdlWxCS0xra+zVOe/7vPfn7MnYUy+m2Up/
f7wSY+OVyVdHZXkCu0qhgxUqMalCVN217JhYMcgsMWJ8xbDyMaFi4iKBXcfybMV/cLGdJ67/oCsz
xO3vZwEuWYcbCiDWYQPUHHYt04mlAvisaxMSYytiWqqkef+TjbGpikNgJ4DN2mRWd3TLBMOsJCVB
AsRZULdKxMzaluXWd4Q05+vGK4phOSTrg/uAoi/OxSCgQF7VbaKAR2VYdQOghnIgJ/ycHF+ZJNhW
Cvs93VCJ/bBS4K3YsPgwNAvO1CH5dU813SA+mNsXZ0HQslWn1w18+yUwrg4W7OJSyShnXTLvqsRw
cWCA5yo4lRK5pKQl1CQvCRgTnEhoiirwyUQiIapSLJkSoSBHQqGv3vxmR20LLYXo3lA1dHkH3bf1
ysD2pam9VwhlPrai57uar8xFLvW2nB977cqe6MfG5NU485kYvTZ5ruW97ZFrQv/VJ/irE7uWBkO0
9dqeZ+H/hdwfjoY/wMyFbWdfupV67nY5cm4n8wnffN5bjCz/uvni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cNj6YI1iOkWfMxRLJevNWB8+adNEB5ngBFnY6DqADubO8Yca6ADnCcpDLzgwglYRJcRersNP
Q1LoTXTULSR/2ULqIQtw8yCQG5/5unwk1/bMGQTkAkwMl6LAeH8/V4lxXP2yMxXcWGT5uBnwEzY6
pAocwz6Dw+UJ4OCL5+lqjBMrfYP7R+Z7fnp+B6dF454T46WKhpVUPCEmehRRi/fEeTbeg1Mi35PT
colUSpNEIpD+vVzl/zQTxAEyXIpnfTNEYBNYSSg9bE5R6maSmobhj5jkEnwyhZNiTEr8T+7Ij4xL
9qP6N5fErQ4=
==== END SVK PATCH BLOCK ====


More information about the Rt-devel mailing list