[Jifty-commit] r1106 - in Jifty-DBI/trunk: . lib/Jifty/DBI

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue May 23 14:21:31 EDT 2006


Author: alexmv
Date: Tue May 23 14:21:30 2006
New Revision: 1106

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
   Jifty-DBI/trunk/t/01records.t
   Jifty-DBI/trunk/t/01searches.t

Log:
 r13251 at zoq-fot-pik:  chmrr | 2006-05-23 14:17:26 -0400
  * Enforce "default is ''" on columns


Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm	Tue May 23 14:21:30 2006
@@ -848,6 +848,14 @@
             $attribs{$column_name} = $bhash;
         }
     }
+
+    for my $column ($self->columns) {
+        if (not defined $attribs{$column->name} and defined $column->default and not ref $column->default) {
+            $attribs{$column->name} = $column->default;
+        }
+    }
+
+    warn YAML::Dump("Creting $self is ".YAML::Dump(\%attribs)) if ref $self eq "BTDT::Model::Task";
     my $ret = $self->_handle->insert( $self->table, %attribs );
     $self->after_create( \$ret ) if $self->can('after_create');
     return ($ret);

Modified: Jifty-DBI/trunk/t/01records.t
==============================================================================
--- Jifty-DBI/trunk/t/01records.t	(original)
+++ Jifty-DBI/trunk/t/01records.t	Tue May 23 14:21:30 2006
@@ -8,7 +8,7 @@
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 61;
+use constant TESTS_PER_DRIVER => 64;
 
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -39,8 +39,8 @@
 	is( $rec->_accessible('id'), undef, "any column is not accessible in undefined mode" );
 	is( $rec->_accessible('unexpected_column' => 'read'), undef, "column doesn't exist and can't be accessible for read" );
 
-	is_deeply( [sort($rec->readable_attributes)], [sort qw(employee_id id name phone)], 'readable attributes' );
-	is_deeply( [sort($rec->writable_attributes)], [sort qw(employee_id name phone)], 'writable attributes' );
+	is_deeply( [sort($rec->readable_attributes)], [sort qw(address employee_id id name phone)], 'readable attributes' );
+	is_deeply( [sort($rec->writable_attributes)], [sort qw(address employee_id name phone)], 'writable attributes' );
 
 	can_ok($rec,'create');
 
@@ -157,13 +157,22 @@
 	ok( !$val, "couldn't load, missing PK column");
 	is( $msg, "Missing PK column: 'id'", "right error message" );
 
+# Defaults kick in
+	$rec = TestApp::Address->new($handle);
+	$id = $rec->create( name => 'Chmrr' );
+	ok( $id, "new record");
+	$rec = TestApp::Address->new($handle);
+	$rec->load_by_cols( name => 'Chmrr' );
+        is( $rec->id, $id, "loaded record by empty value" );
+        is( $rec->address, '', "Got default on create" );
+
 # load_by_cols 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->load_by_cols( name => 'Obra', phone => undef, employee_id => '' );
-    is( $rec->id, $id, "loaded record by empty value" );
+        is( $rec->id, $id, "loaded record by empty value" );
 
 # __set error paths
 	$rec = TestApp::Address->new($handle);
@@ -214,6 +223,7 @@
         id integer AUTO_INCREMENT,
         name varchar(36),
         phone varchar(18),
+        address varchar(50),
         employee_id int(8),
   	PRIMARY KEY (id))
 EOF
@@ -226,6 +236,7 @@
         id serial PRIMARY KEY,
         name varchar,
         phone varchar,
+        address varchar,
         employee_id integer
 )
 EOF
@@ -239,6 +250,7 @@
         id  integer primary key,
         name varchar(36),
         phone varchar(18),
+        address varchar(50),
         employee_id int(8))
 EOF
 
@@ -251,17 +263,17 @@
 use Jifty::DBI::Schema;
 
 column name =>
-  type is 'varchar(14)',
-  default is '';
+  type is 'varchar(14)';
 
 column phone =>
   type is 'varchar(18)',
-  length => 18,
+
+column address =>
+  type is 'varchar(50)',
   default is '';
 
 column employee_id =>
-  type is 'int(8)',
-  default is '';
+  type is 'int(8)';
 }
 1;
 

Modified: Jifty-DBI/trunk/t/01searches.t
==============================================================================
--- Jifty-DBI/trunk/t/01searches.t	(original)
+++ Jifty-DBI/trunk/t/01searches.t	Tue May 23 14:21:30 2006
@@ -8,7 +8,7 @@
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 59;
+use constant TESTS_PER_DRIVER => 63;
 
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -129,7 +129,7 @@
 	is( $users_obj->count, 1, "found one user who name ends with 'Tang'" );
 	$first_rec = $users_obj->first;
 	isa_ok( $first_rec, 'Jifty::DBI::Record', 'First returns record object' );
-	is( $first_rec->login, 'autrijus', 'login is correct' );
+	is( $first_rec->login, 'audreyt', 'login is correct' );
 
 	# IS NULL
 	# XXX TODO FIXME: column => undef should be handled as NULL
@@ -137,12 +137,20 @@
 	is_deeply( $users_obj, $clean_obj, 'after clean_slate looks like new object');
 	$users_obj->limit( column => 'phone', operator => 'IS', value => 'NULL' );
 	is( $users_obj->count, 2, "found 2 users who has unknown phone number" );
+	$users_obj->clean_slate;
+	is_deeply( $users_obj, $clean_obj, 'after clean_slate looks like new object');
+	$users_obj->limit( column => 'address', operator => 'IS', value => 'NULL' );
+	is( $users_obj->count, 0, "found 0 users who has unknown address" );
 	
 	# IS NOT NULL
 	$users_obj->clean_slate;
 	is_deeply( $users_obj, $clean_obj, 'after clean_slate looks like new object');
 	$users_obj->limit( column => 'phone', operator => 'IS NOT', value => 'NULL', QOUTEvalue => 0 );
-	is( $users_obj->count, $count_all - 2, "found users who has phone number filled" );
+	is( $users_obj->count, $count_all - 2, "found users who have phone number filled" );
+	$users_obj->clean_slate;
+	is_deeply( $users_obj, $clean_obj, 'after clean_slate looks like new object');
+	$users_obj->limit( column => 'address', operator => 'IS NOT', value => 'NULL', QOUTEvalue => 0 );
+	is( $users_obj->count, $count_all, "found users who have address filled" );
 	
 	# ORDER BY / GROUP BY
 	$users_obj->clean_slate;
@@ -171,6 +179,7 @@
         login varchar(18) NOT NULL,
         name varchar(36),
 	phone varchar(18),
+	address varchar(18),
   	PRIMARY KEY (id))
 EOF
 
@@ -182,7 +191,8 @@
         id serial PRIMARY KEY,
         login varchar(18) NOT NULL,
         name varchar(36),
-        phone varchar(18)
+        phone varchar(18),
+	address varchar(18)
 )
 EOF
 
@@ -195,7 +205,8 @@
 	id integer primary key,
 	login varchar(18) NOT NULL,
 	name varchar(36),
-	phone varchar(18))
+	phone varchar(18),
+	address varchar(18))
 EOF
 
 }
@@ -216,11 +227,11 @@
 
 sub init_data {
     return (
-	[ 'login',	'name',			'phone' ],
-	[ 'cubic',	'Ruslan U. Zakirov',	'+7-903-264-XX-XX' ],
-	[ 'obra',	'Jesse Vincent',	undef ],
-	[ 'glasser',	'David Glasser',	undef ],
-	[ 'autrijus',	'Autrijus Tang',	'+X-XXX-XXX-XX-XX' ],
+        [ 'login',      'name',                 'phone',            'address' ],
+        [ 'cubic',      'Ruslan U. Zakirov',    '+7-903-264-XX-XX', undef ],
+        [ 'obra',       'Jesse Vincent',        undef,              undef ],
+        [ 'glasser',    'David Glasser',        undef,              'somewhere' ],
+        [ 'audreyt',    'Audrey Tang',          '+X-XXX-XXX-XX-XX', 'someplace' ],
     );
 }
 
@@ -230,9 +241,10 @@
 BEGIN {
     use Jifty::DBI::Schema;
 
-    column login => type is 'varchar(18)';
-    column name  => type is 'varchar(36)';
-    column phone => type is 'varchar(18)', default is '';
+    column login   => type is 'varchar(18)';
+    column name    => type is 'varchar(36)';
+    column phone   => type is 'varchar(18)', default is undef;
+    column address => type is 'varchar(18)', default is '';
 }
 
 1;


More information about the Jifty-commit mailing list