From: <pau...@us...> - 2007-05-22 23:12:45
|
Revision: 958 http://svn.sourceforge.net/everydevel/?rev=958&view=rev Author: paul_the_nomad Date: 2007-05-22 16:12:39 -0700 (Tue, 22 May 2007) Log Message: ----------- Nodebase connection utility function for command line and tests Modified Paths: -------------- trunk/ebase/lib/Everything/CmdLine.pm trunk/ebase/lib/Everything/Test/CmdLine.pm Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:983 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 + 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:985 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 Modified: trunk/ebase/lib/Everything/CmdLine.pm =================================================================== --- trunk/ebase/lib/Everything/CmdLine.pm 2007-05-22 23:12:13 UTC (rev 957) +++ trunk/ebase/lib/Everything/CmdLine.pm 2007-05-22 23:12:39 UTC (rev 958) @@ -7,16 +7,18 @@ use strict; use warnings; -our @EXPORT_OK = qw(get_options abs_path); +our @EXPORT_OK = qw(get_options abs_path usage_options make_nodebase); Getopt::Long::Configure(qw/bundling/); sub get_options { - my ($usage_msg) = @_; + my ($usage_msg, $other_options) = @_; + $other_options ||= []; my %opts; + $opts{database} = $opts{port} = $opts{host} = $opts{password} = $opts{user} = ''; GetOptions( \%opts, 'user|u=s', 'password|p=s', 'host|h=s', - 'database|d=s', 'port|P=s', 'type|t=s' + 'database|d=s', 'port|P=s', 'type|t=s', @$other_options ) or usage_options($usage_msg); return \%opts; @@ -27,7 +29,7 @@ $usage_msg ||= "Usage:\n\n"; $usage_msg .= <<USAGE; -Takes the following options: +Takes the following standard options: \t -d \t --database \t\t the db name. In the case of sqlite, it will be the file name of the test db, it will not be deleted on completion. If no name is specified a temporary file will be used if possible. The temporary file will be deleted on completion. In the case of mysql or postgresql, it is the name of the database to use. @@ -75,4 +77,30 @@ } +=cut + +=head2 C<make_nodebase> + +Takes a hash reference like the one returned by get_options(). Returns a nodebase object if it can get one. + +=cut + +sub make_nodebase { + my ($opts) = @_; + + $$opts{type} ||= 'sqlite'; + $$opts{user} ||= $ENV{USER}; + $$opts{host} ||= 'localhost'; + + my $nb = + Everything::NodeBase->new( + "$$opts{database}:$$opts{user}:$$opts{password}:$$opts{host}", + 1, $$opts{type} ); + croak +"Can't connect to nodebase using database '$$opts{database}', user '$$opts{user}', password '$$opts{password}', host '$$opts{host}' and type '$$opts{type}'" + unless $nb; + + return $nb; +} + 1; Modified: trunk/ebase/lib/Everything/Test/CmdLine.pm =================================================================== --- trunk/ebase/lib/Everything/Test/CmdLine.pm 2007-05-22 23:12:13 UTC (rev 957) +++ trunk/ebase/lib/Everything/Test/CmdLine.pm 2007-05-22 23:12:39 UTC (rev 958) @@ -2,6 +2,8 @@ use Test::More; use Test::Warn; +use Test::MockObject; +use Test::Exception; use Cwd; use warnings; use strict; @@ -88,4 +90,50 @@ } + +sub test_make_nodebase : Test(5) { + my $self = shift; + can_ok( $self->{class}, 'make_nodebase' ) + || return 'abs_path not implemented.'; + + my $mock = Test::MockObject->new; + $mock->fake_module('Everything::NodeBase'); + + my @new_args; + my $new_returns = $mock; + local *Everything::NodeBase::new; + *Everything::NodeBase::new = sub { @new_args = @_; return $new_returns }; + + my $test_code = \&{ $self->{class} . '::make_nodebase' }; + my $opts = { + database => 'dbname', + user => 'dbuser', + password => 'dbpassword', + host => 'dbhost', + type => 'dbtype', + port => 'dbport' + }; + my $rv = $test_code->($opts); + is_deeply( + \@new_args, + [ + 'Everything::NodeBase', "dbname:dbuser:dbpassword:dbhost", + 1, 'dbtype' + ], + '...args are handled properly.' + ); + is( "$rv", $mock, '...returns the return value of NodeBase\'s new.' ); + + $opts = { database => 'dbname', password => '' }; + $rv = $test_code->($opts); + is_deeply( + \@new_args, + [ 'Everything::NodeBase', "dbname:$ENV{USER}::localhost", 1, 'sqlite' ], + '...defaults are set.' + ); + + undef $new_returns; + dies_ok { $test_code->($opts) } '...dies if no nodebase found.'; +} + 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |