Grokbase
Topics Posts Groups | in
x
[ help ]

Adam Herzog (a...@herzogdesigns.com)

Profile | Posts (25)

User Information

Display Name:Adam Herzog
Partial Email Address:a...@herzogdesigns.com
Posts:
25 total
13 in Catalyst Framework
11 in DBIx::Class
1 in HTML::FormFu

5 Most Recent

All Posts
1) Adam Herzog [html-formfu] AutoSet with a single value
| +1 vote
--Apple-Mail-3-861823930 Content-Transfer-Encoding: 7bit Content-Type: text/plain;...
HTML::FormFu
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
--Apple-Mail-3-861823930
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

I ran into a problem with the AutoSet constraint, where it would  
always fail if there is only a single option, even if that option is  
selected.

I've attached a patch, with tests (svn diff against r304.)

-A


--Apple-Mail-3-861823930
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-mac-type=54455854; x-unix-mode=0644;
name=autoset.diff
Content-Disposition: attachment;
filename=autoset.diff

Index: t/constraints/autoset.t
===================================================================
--- t/constraints/autoset.t (revision 304)
+++ t/constraints/autoset.t (working copy)
@@ -1,12 +1,15 @@
use strict;
use warnings;

-use Test::More tests => 2;
+use Test::More tests => 4;

use HTML::FormFu;

my $form = HTML::FormFu->new;

+##############################################################################
+### Autoset with multiple values
+##############################################################################
$form->element('Select')
     ->name('foo')
     ->values([qw/ one two three /])
@@ -29,3 +32,30 @@

     ok( $form->has_errors('foo') );
}
+
+##############################################################################
+### Autoset with a single value
+##############################################################################
+$form->element('Select')
+    ->name('bar')
+    ->values([qw/ one /])
+    ->constraint('AutoSet');
+
+# Valid
+{
+    $form->process( {
+            bar => 'one',
+        } );
+
+    ok( $form->valid('bar') );
+}
+
+# Invalid
+{
+    $form->process( {
+            bar => 'yes',
+        } );
+
+    ok( $form->has_errors('bar') );
+}
+
Index: lib/HTML/FormFu/Constraint/AutoSet.pm
===================================================================
--- lib/HTML/FormFu/Constraint/AutoSet.pm (revision 304)
+++ lib/HTML/FormFu/Constraint/AutoSet.pm (working copy)
@@ -8,7 +8,8 @@
sub process {
     my $self = shift;
     
- $self->set( map { $_->{value} } @{ $self->parent->_options } );
+    my @set = map { $_->{value} } @{ $self->parent->_options };
+    $self->set( \@set );
     
     return $self->next::method(@_);
}

--Apple-Mail-3-861823930
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
HTML-FormFu mailing list
[email protected: HTML-F...@lists.scsys.co.uk]
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

--Apple-Mail-3-861823930--
2) Adam Herzog Re: [Dbix-class] Reconnecting Behavior
| +1 vote
--Apple-Mail-6--780610601 Content-Transfer-Encoding: 7bit Content-Type: text/plain;...
DBIx::Class
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
--Apple-Mail-6--780610601
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

On Jul 30, 2007, at 5:28 PM, Brandon Black wrote:
> On 7/30/07, Adam Herzog <adam@herzogdesigns.com> wrote:
>> Okay; I think I have this fixed.
>>
>> I ended up having to create a new, dummy db and chmoding it in order
>> to prevent SQLite from connecting. The fix ended up being fairly
>> straightforward once I had a sane test. No exception was being thrown
>> if the connection failed, because it was checking $dbh (which was
>> always true) instead of $DBI::errstr, which had the actual failure
>> message.
>>
>> Can somebody put their eyes on this and let me know if I've
>> overlooked anything (and commit if I haven't.)
>
> Ugh, that's cute.  Here's what the DBI docs say:
>
> 'If the connect fails (see below), it returns "undef" and sets both
> $DBI::err and $DBI::errstr. (It does not explicitly set $!.) You
> should generally test the return status of "connect" and "print
> $DBI::errstr" if it has failed.'
>
> How about we check all three, as in: "if !$dbh || $@ || $DBI::errstr"
> on the appropriate line?

I saw that in the docs, but I guess I was too focused on my own use  
case, where $dbh was becoming a hashref (with RaiseError, PrintError,  
et al), and thus evaluating as true. I didn't think that checking  
$dbh would buy us anything.

But, I guess in a situation where an empty $dbh is coming from a  
coderef (and not setting $@ or $DBI::errstr) AND 'unsafe' is set (so  
it isn't getting turned into a hashref), we'll need to check.

Updated patch is attached.

-A


--Apple-Mail-6--780610601
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-unix-mode=0644; name=reconnect_2.diff
Content-Disposition: attachment;
filename=reconnect_2.diff

Index: t/33storage_reconnect.t
===================================================================
--- t/33storage_reconnect.t (revision 3637)
+++ t/33storage_reconnect.t (working copy)
@@ -1,12 +1,17 @@
use strict;
 use warnings;  

+use FindBin;
+use File::Copy;
use Test::More;
use lib qw(t/lib);
use DBICTest;

-plan tests => 2;
+plan tests => 5;

+my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
+my $db_tmp  = "$db_orig.tmp";
+
# Set up the "usual" sqlite for DBICTest
my $schema = DBICTest->init_schema;

@@ -24,3 +29,31 @@
 #   4. Success!
my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
cmp_ok(@art_two, '==', 3, "Three artists returned");
+
+### Now, disconnect the dbh, and move the db file;
+# create a new one and chmod 000 to prevent SQLite from connecting.
+$schema->storage->_dbh->disconnect;
+move( $db_orig, $db_tmp );
+open DBFILE, '>', $db_orig;
+print DBFILE 'THIS IS NOT A REAL DATABASE';
+close DBFILE;
+chmod 0000, $db_orig;
+
+### Try the operation again... it should fail, since there's no db
+eval {
+ my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( $@, 'The operation failed' );
+
+### Now, move the db file back to the correct name
+unlink($db_orig);
+move( $db_tmp, $db_orig );
+
+### Try the operation again... this time, it should succeed
+my @art_four;
+eval {
+ @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( !$@, 'The operation succedded' );
+cmp_ok( @art_four, '==', 3, "Three artists returned" );
+
Index: lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- lib/DBIx/Class/Storage/DBI.pm (revision 3637)
+++ lib/DBIx/Class/Storage/DBI.pm (working copy)
@@ -785,7 +785,7 @@
   $DBI::connect_via = $old_connect_via if $old_connect_via;

   $self->throw_exception("DBI Connection failed: " . ($@||$DBI::errstr))
-    if !$dbh || $@;
+    if !$dbh || $@ || $DBI::errstr;

   $self->_dbh_autocommit($dbh->{AutoCommit});


--Apple-Mail-6--780610601
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed




--Apple-Mail-6--780610601
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
3) Adam Herzog Re: [Dbix-class] Reconnecting Behavior
| +1 vote
--Apple-Mail-2--786368803 Content-Transfer-Encoding: 7bit Content-Type: text/plain;...
DBIx::Class
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
--Apple-Mail-2--786368803
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

On Jul 30, 2007, at 2:17 PM, Matt S Trout wrote:
> On Mon, Jul 30, 2007 at 11:03:44AM -0400, Adam Herzog wrote:
>> On Jul 26, 2007, at 2:17 PM, Adam Herzog wrote:
>>> On Jul 26, 2007, at 12:19 PM, Brandon Black wrote:
>>>> As I understand the code, it *should* be attempting reconnection.
>>>> Any
>>>> chance you can make a test file that reproduces the behavior?
>>>
>>> I've added a test to 33storage_reconnect.t; attached is the output
>>> of an svn diff against trunk.
>>>
>>> Essentially, there was already a test to make sure that it would
>>> reconnect if it lost its connection. I had to make that
>>> reconnection fail, so I rename the sqlite file, try to fetch some
>>> data, rename the file back, and then try to fetch some data again.
>>> The last fetch fails, when it should succeed.
>>
>> After trying to look into the problem, I discovered that my test is
>> never going to work. After moving the db file and trying to
>> reconnect, SQLite just creates a new file, so it IS connected to a db
>> and so it doesn't try to reconnect again.
>
> How about if you write "THIS DATABASE IS FULL OF FAIL\n" to the
> original
> location of the file and -then- try to reconnect?

Okay; I think I have this fixed.

I ended up having to create a new, dummy db and chmoding it in order  
to prevent SQLite from connecting. The fix ended up being fairly  
straightforward once I had a sane test. No exception was being thrown  
if the connection failed, because it was checking $dbh (which was  
always true) instead of $DBI::errstr, which had the actual failure  
message.

Can somebody put their eyes on this and let me know if I've  
overlooked anything (and commit if I haven't.)

Thanks!

-A


--Apple-Mail-2--786368803
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-unix-mode=0644; name=reconnect.diff
Content-Disposition: attachment;
filename=reconnect.diff

Index: t/33storage_reconnect.t
===================================================================
--- t/33storage_reconnect.t (revision 3637)
+++ t/33storage_reconnect.t (working copy)
@@ -1,12 +1,17 @@
use strict;
 use warnings;  

+use FindBin;
+use File::Copy;
use Test::More;
use lib qw(t/lib);
use DBICTest;

-plan tests => 2;
+plan tests => 5;

+my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
+my $db_tmp  = "$db_orig.tmp";
+
# Set up the "usual" sqlite for DBICTest
my $schema = DBICTest->init_schema;

@@ -24,3 +29,31 @@
 #   4. Success!
my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
cmp_ok(@art_two, '==', 3, "Three artists returned");
+
+### Now, disconnect the dbh, and move the db file;
+# create a new one and chmod 000 to prevent SQLite from connecting.
+$schema->storage->_dbh->disconnect;
+move( $db_orig, $db_tmp );
+open DBFILE, '>', $db_orig;
+print DBFILE 'THIS IS NOT A REAL DATABASE';
+close DBFILE;
+chmod 0000, $db_orig;
+
+### Try the operation again... it should fail, since there's no db
+eval {
+ my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( $@, 'The operation failed' );
+
+### Now, move the db file back to the correct name
+unlink($db_orig);
+move( $db_tmp, $db_orig );
+
+### Try the operation again... this time, it should succeed
+my @art_four;
+eval {
+ @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( !$@, 'The operation succedded' );
+cmp_ok( @art_four, '==', 3, "Three artists returned" );
+
Index: lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- lib/DBIx/Class/Storage/DBI.pm (revision 3637)
+++ lib/DBIx/Class/Storage/DBI.pm (working copy)
@@ -785,7 +785,7 @@
   $DBI::connect_via = $old_connect_via if $old_connect_via;

   $self->throw_exception("DBI Connection failed: " . ($@||$DBI::errstr))
-    if !$dbh || $@;
+    if $@ || $DBI::errstr;

   $self->_dbh_autocommit($dbh->{AutoCommit});


--Apple-Mail-2--786368803
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
4) Adam Herzog Re: [Dbix-class] Reconnecting Behavior
| +1 vote
After trying to look into the problem, I discovered that my test is never going to work. After...
DBIx::Class
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
On Jul 26, 2007, at 2:17 PM, Adam Herzog wrote:
> On Jul 26, 2007, at 12:19 PM, Brandon Black wrote:
>> As I understand the code, it *should* be attempting reconnection.
>> Any
>> chance you can make a test file that reproduces the behavior?
>
> I've added a test to 33storage_reconnect.t; attached is the output
> of an svn diff against trunk.
>
> Essentially, there was already a test to make sure that it would
> reconnect if it lost its connection. I had to make that
> reconnection fail, so I rename the sqlite file, try to fetch some
> data, rename the file back, and then try to fetch some data again.
> The last fetch fails, when it should succeed.

After trying to look into the problem, I discovered that my test is  
never going to work. After moving the db file and trying to  
reconnect, SQLite just creates a new file, so it IS connected to a db  
and so it doesn't try to reconnect again.

I'll dig around some more and see what I can come up with; maybe add  
tests to the live mysql test file, I guess.

-A
5) Adam Herzog Re: [Dbix-class] Reconnecting Behavior
| +1 vote
--Apple-Mail-4-1008246573 Content-Transfer-Encoding: 7bit Content-Type: text/plain;...
DBIx::Class
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
--Apple-Mail-4-1008246573
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

On Jul 26, 2007, at 12:19 PM, Brandon Black wrote:
> As I understand the code, it *should* be attempting reconnection. Any
> chance you can make a test file that reproduces the behavior?

I've added a test to 33storage_reconnect.t; attached is the output of  
an svn diff against trunk.

Essentially, there was already a test to make sure that it would  
reconnect if it lost its connection. I had to make that reconnection  
fail, so I rename the sqlite file, try to fetch some data, rename the  
file back, and then try to fetch some data again. The last fetch  
fails, when it should succeed.

Let me know if there's anything else I can do.

Thanks,

-A


--Apple-Mail-4-1008246573
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-unix-mode=0644;
name=reconnect_test.diff
Content-Disposition: attachment;
filename=reconnect_test.diff

Index: t/33storage_reconnect.t
===================================================================
--- t/33storage_reconnect.t (revision 3603)
+++ t/33storage_reconnect.t (working copy)
@@ -1,12 +1,17 @@
use strict;
 use warnings;  

+use FindBin;
+use File::Copy;
use Test::More;
use lib qw(t/lib);
use DBICTest;

-plan tests => 2;
+plan tests => 4;

+my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
+my $db_tmp  = "$db_orig.tmp";
+
# Set up the "usual" sqlite for DBICTest
my $schema = DBICTest->init_schema;

@@ -24,3 +29,21 @@
 #   4. Success!
my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
cmp_ok(@art_two, '==', 3, "Three artists returned");
+
+### Now, disconnect the dbh, and move the db file so it can't be reconnected
+$schema->storage->_dbh->disconnect;
+move( $db_orig, $db_tmp );
+
+### Try the operation again... it should fail, since there's no db
+eval {
+ my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( $@, 'The operation failed' );
+
+### Now, move the db file back to the correct name
+move( $db_tmp, $db_orig );
+
+### Try the operation again... this time, it should succeed
+my @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+cmp_ok( @art_two, '==', 3, "Three artists returned" );
+

--Apple-Mail-4-1008246573
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed



--Apple-Mail-4-1008246573
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

spacer
Profile | Posts (25)
Home > People > Adam Herzog