From: <chr...@us...> - 2006-06-21 01:11:13
|
Revision: 892 Author: chromatic Date: 2006-06-20 18:10:58 -0700 (Tue, 20 Jun 2006) ViewCVS: http://svn.sourceforge.net/everydevel/?rev=892&view=rev Log Message: ----------- r17930@windwheel: chromatic | 2006-06-20 18:10:43 -0700 Run the insert() tests for node against a SQLite database. Minor fixes to Everything::NodeBase and Everything::DB::sqlite. Added t/lib/build_test_db.pm to build the test SQLite database. Modified Paths: -------------- trunk/ebase/MANIFEST trunk/ebase/lib/Everything/DB/sqlite.pm trunk/ebase/lib/Everything/Node/Test/htmlpage.pm trunk/ebase/lib/Everything/Node/Test/node.pm trunk/ebase/lib/Everything/NodeBase.pm Added Paths: ----------- trunk/ebase/t/lib/build_test_db.pm Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17847 + a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 Modified: trunk/ebase/MANIFEST =================================================================== --- trunk/ebase/MANIFEST 2006-06-14 22:51:20 UTC (rev 891) +++ trunk/ebase/MANIFEST 2006-06-21 01:10:58 UTC (rev 892) @@ -157,6 +157,7 @@ t/HTML/FormObject/TextArea.t t/HTML/FormObject/TextField.t t/HTML/FormObject/TypeMenu.t +t/lib/build_test_db.pm t/lib/FakeDBI.pm t/lib/FakeNode.pm t/lib/FakeNodeBase.pm Modified: trunk/ebase/lib/Everything/DB/sqlite.pm =================================================================== --- trunk/ebase/lib/Everything/DB/sqlite.pm 2006-06-14 22:51:20 UTC (rev 891) +++ trunk/ebase/lib/Everything/DB/sqlite.pm 2006-06-21 01:10:58 UTC (rev 892) @@ -79,7 +79,7 @@ $getHash = 1 unless defined $getHash; $table ||= "node"; - my $DBTABLE = $this->getNode( $table, 'dbtable' ) || {}; + my $DBTABLE = $this->{nb}->getNode( $table, 'dbtable' ) || {}; unless ( exists $DBTABLE->{Fields} ) { @@ -96,6 +96,12 @@ return map { $_->{Field} } @{ $DBTABLE->{Fields} }; } +sub lastValue +{ + my $self = shift; + return $self->{dbh}->func( 'last_insert_rowid' ); +} + =head2 C<tableExists> Check to see if a table of the given name exists in this database. Returns 1 Modified: trunk/ebase/lib/Everything/Node/Test/htmlpage.pm =================================================================== --- trunk/ebase/lib/Everything/Node/Test/htmlpage.pm 2006-06-14 22:51:20 UTC (rev 891) +++ trunk/ebase/lib/Everything/Node/Test/htmlpage.pm 2006-06-21 01:10:58 UTC (rev 892) @@ -30,7 +30,8 @@ $node->{DB} = $db; delete $node->{parent_container}; - $node->set_true( 'SUPER' ); + $node->set_true( 'SUPER' ) + ->clear(); $db->set_series( -getNode => undef, 'gnc' ); $node->insert( 'user' ); Modified: trunk/ebase/lib/Everything/Node/Test/node.pm =================================================================== --- trunk/ebase/lib/Everything/Node/Test/node.pm 2006-06-14 22:51:20 UTC (rev 891) +++ trunk/ebase/lib/Everything/Node/Test/node.pm 2006-06-21 01:10:58 UTC (rev 892) @@ -5,12 +5,19 @@ use base 'Test::Class'; +use DBI; use Test::More; use Test::MockObject; use Test::MockObject::Extends; +use File::Copy; +use File::Temp; +use File::Spec::Functions; use Scalar::Util qw( reftype blessed ); +use Everything::NodeBase; +use Everything::DB::sqlite; + sub node_class { my $self = shift; @@ -24,6 +31,8 @@ my $self = shift; $self->{errors} = []; + $self->make_base_test_db(); + my $mock = Test::MockObject->new(); $mock->fake_module( 'Everything', logErrors => sub { @@ -32,12 +41,12 @@ ); *Everything::Node::node::DB = \$mock; - my $module = $self->node_class(); + my $module = $self->node_class(); my %import; my $mockimport = sub { $import{ +shift }++ }; - for my $mod (qw( DBI Everything Everything::NodeBase Everything::XML)) + for my $mod (qw( DBI Everything Everything::XML)) { $mock->fake_module( $mod, import => $mockimport ); } @@ -49,6 +58,28 @@ isa_ok( $module->new(), $module ); } +sub make_base_test_db +{ + my $self = shift; + + my $blank_db = catfile(qw( t ebase.db )); + require 't/lib/build_test_db.pm' unless -e $blank_db; + + my $tempdir = File::Temp::tempdir( DIR => 't', CLEANUP => 1 ); + my $module = $self->node_class(); + my $module_db = catfile( $tempdir, $module . '_base.db' ); + + copy( $blank_db, $module_db ) + or die "No test database for $module $!"; + + $self->{base_test_db} = $module_db; + $self->{tempdir} = $tempdir; + $self->populate_base_database( $module_db ); +} + +# override if necessary +sub populate_base_database {} + sub test_extends :Test( 1 ) { my $self = shift; @@ -70,7 +101,10 @@ sub make_fixture :Test(setup) { my $self = shift; - my $db = Test::MockObject->new(); + $self->make_test_db(); + + my $nb = Everything::NodeBase->new( $self->{test_db}, 1, 'sqlite' ); + my $db = Test::MockObject::Extends->new( $nb ); $self->reset_mock_node(); *Everything::Node::node::DB = \$db; @@ -79,6 +113,21 @@ $self->{errors} = []; } +sub make_test_db +{ + my $self = shift; + my $method_name = $self->current_method(); + my $base_db = $self->{base_test_db}; + my $tempdir = $self->{tempdir}; + my $test_db = catfile( $tempdir, $method_name . '.db' ); + + copy( $base_db, $test_db ) + or die "Cannot create test db for $method_name: $!\n"; + + $self->{test_db} = $test_db; + $self->{test_dbh} = DBI->connect( "dbi:SQLite:dbname=$test_db", '', '' ); +} + sub reset_mock_node { my $self = shift; @@ -155,58 +204,48 @@ '... or should return the inserted node_id otherwise' ); } -sub test_insert :Test( 10 ) +sub test_insert :Test( 3 ) { my $self = shift; my $node = $self->{node}; my $db = $self->{mock_db}; + my $type = $db->getType( 'nodetype' ); + $node->{node_id} = 0; - $node->{type} = $node; - $node->{restrictdupes} = 1; + $node->{type} = $type; + $node->{type_nodetype} = 1; $node->set_true(qw( -hasAccess -restrictTitle -getId )); - $db->set_always( getNode => { key => 'value' } ) - ->set_always( -lastValue => 101 ); $node->{foo} = 11; delete $node->{type}{restrictdupes}; - $db->set_list( -getFields => 'foo' ) - ->set_series( getNode => 0, {} ) - ->set_true( 'sqlInsert' ) - ->set_always( -now => 'now' ) - ->clear(); - $node->set_always( -getTableArray => [ 'table' ] ) - ->set_true( 'cache' ); + my $time = time(); + $db->set_always( -now => $time ); + + $node->set_true( 'cache' ); $node->{node_id} = 0; - ok( defined $node->insert( 'user' ), - '... but should return node_id if no dupes exist' ); - - my ( $method, $args ) = $db->next_call( 2 ); - is( $method, 'sqlInsert', '... inserting base node' ); + my $result = $node->insert( 'user' ); - is( $args->[1], 'node', '... into the node table' ); - is_deeply( $args->[2], + ok( defined $result, 'insert() should return a node_id if no dupes exist' ); + is( $result, 4, '... with the proper sequence' ); + + my $dbh = $db->{storage}->getDatabaseHandle(); + my $sth = $dbh->prepare( + 'SELECT createtime, author_user, hits FROM node WHERE node_id=?' + ); + $sth->execute( $result ); + my $node_ref = $sth->fetchrow_hashref(); + is_deeply( $node_ref, { - -createtime => 'now', + createtime => $time, author_user => 'user', hits => 0, - foo => 11, }, '... with the proper fields' ); - - ( $method, $args ) = $db->next_call(); - is( $method, 'sqlInsert', '... inserting node' ); - is( $args->[1], 'table', '... into proper table' ); - is_deeply( $args->[2], { foo => 11, table_id => 101 }, - '... proper fields' ); - - ( $method, $args ) = $db->next_call(); - is( $method, 'getNode', '... fetching node' ); - is( join( '-', @$args ), "$db-101-force", '... forcing refresh' ); - is( $node->next_call(), 'cache', '... and caching node' ); + $sth->finish(); } sub test_update_access :Test( 3 ) Modified: trunk/ebase/lib/Everything/NodeBase.pm =================================================================== --- trunk/ebase/lib/Everything/NodeBase.pm 2006-06-14 22:51:20 UTC (rev 891) +++ trunk/ebase/lib/Everything/NodeBase.pm 2006-06-21 01:10:58 UTC (rev 892) @@ -23,10 +23,10 @@ BEGIN { my @methlist = qw( - getDatabaseHandle sqlDelete sqlSelect sqlSelectJoined + getDatabaseHandle sqlDelete sqlSelect sqlSelectJoined getFieldsHash sqlSelectMany sqlSelectHashref sqlUpdate sqlInsert _quoteData sqlExecute getNodeByIdNew getNodeByName constructNode selectNodeWhere getNodeCursor - countNodeMatches getAllTypes dropNodeTable quote genWhereString + countNodeMatches getAllTypes dropNodeTable quote genWhereString lastValue ); for my $method (@methlist) Added: trunk/ebase/t/lib/build_test_db.pm =================================================================== --- trunk/ebase/t/lib/build_test_db.pm (rev 0) +++ trunk/ebase/t/lib/build_test_db.pm 2006-06-21 01:10:58 UTC (rev 892) @@ -0,0 +1,90 @@ +#! perl + +# XXX: this file depends on the format of tables/*.sql +# run it through SQL::Translator if and when it changes! + +use strict; +use warnings; + +use DBI; +use File::Spec::Functions 'catfile'; + +my $db_file = catfile(qw( t ebase.db )); +unlink $db_file; + +my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", '', '' ); + +my @tables = split /\n--\n/, <<END_TABLES; +CREATE TABLE node ( + node_id INTEGER PRIMARY KEY NOT NULL, + type_nodetype int(11) NOT NULL DEFAULT '0', + title char(240) NOT NULL DEFAULT '', + author_user int(11) NOT NULL DEFAULT '0', + createtime datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + modified datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + hits int(11) DEFAULT '0', + loc_location int(11) DEFAULT '0', + reputation int(11) NOT NULL DEFAULT '0', + lockedby_user int(11) NOT NULL DEFAULT '0', + locktime datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + authoraccess char(4) NOT NULL DEFAULT 'iiii', + groupaccess char(5) NOT NULL DEFAULT 'iiiii', + otheraccess char(5) NOT NULL DEFAULT 'iiiii', + guestaccess char(5) NOT NULL DEFAULT 'iiiii', + dynamicauthor_permission int(11) NOT NULL DEFAULT '-1', + dynamicgroup_permission int(11) NOT NULL DEFAULT '-1', + dynamicother_permission int(11) NOT NULL DEFAULT '-1', + dynamicguest_permission int(11) NOT NULL DEFAULT '-1', + group_usergroup int(11) NOT NULL DEFAULT '-1' +); +-- +CREATE TABLE nodetype ( + nodetype_id INTEGER PRIMARY KEY NOT NULL DEFAULT '0', + restrict_nodetype int(11) DEFAULT '0', + extends_nodetype int(11) DEFAULT '0', + restrictdupes int(11) DEFAULT '0', + sqltable char(255), + grouptable char(40) DEFAULT '', + defaultauthoraccess char(4) NOT NULL DEFAULT 'iiii', + defaultgroupaccess char(5) NOT NULL DEFAULT 'iiiii', + defaultotheraccess char(5) NOT NULL DEFAULT 'iiiii', + defaultguestaccess char(5) NOT NULL DEFAULT 'iiiii', + defaultgroup_usergroup int(11) NOT NULL DEFAULT '-1', + defaultauthor_permission int(11) NOT NULL DEFAULT '-1', + defaultgroup_permission int(11) NOT NULL DEFAULT '-1', + defaultother_permission int(11) NOT NULL DEFAULT '-1', + defaultguest_permission int(11) NOT NULL DEFAULT '-1', + maxrevisions int(11) NOT NULL DEFAULT '-1', + canworkspace int(11) NOT NULL DEFAULT '-1' +); +-- +CREATE TABLE setting ( + setting_id INTEGER PRIMARY KEY NOT NULL DEFAULT '0', + vars text(65535) NOT NULL +); +-- +CREATE TABLE version ( + version_id INTEGER PRIMARY KEY NOT NULL DEFAULT '0', + version int(11) NOT NULL DEFAULT '1' +); +-- +CREATE INDEX title_node on node (title, type_nodetype); +-- +CREATE INDEX author_node on node (author_user); +-- +CREATE INDEX type_node on node (type_nodetype); +END_TABLES + +my $nodes = do { + local $/; local @ARGV = catfile(qw( tables basenodes.in )); <> +}; + +for my $statement (@tables, split /\n/, $nodes ) +{ + next unless $statement =~ /\S/; + $dbh->do( $statement ); +} + +$dbh->disconnect(); + +1; Property changes on: trunk/ebase/t/lib/build_test_db.pm ___________________________________________________________________ Name: svn:mime-type + text/plain; charset=UTF-8 Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |