Thread: [Lxr-commits] CVS: lxr-tools/tests PostgresTest.pm, NONE, 1.1 initdb-postgres, NONE, 1.1
Brought to you by:
ajlittoz
From: Malcolm B. <mb...@us...> - 2009-05-12 15:51:57
|
Update of /cvsroot/lxr/lxr-tools/tests In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv12683 Added Files: PostgresTest.pm initdb-postgres Log Message: Test suite for Index::Postgres.pm --- NEW FILE: PostgresTest.pm --- # Test cases for the Postgres backend # Uses the lxr.conf file in the tests directory # Assumes the use of lxr_tests as the test database package PostgresTest; use strict; use File::Spec; use Test::Unit; use LXR::Config; use LXR::Index::Postgres; use DBI; use base qw(Test::Unit::TestCase); use vars qw($config); sub new { my $self = shift()->SUPER::new(@_); return $self; } # define tests # Test fileid sub test_fileid { my $self = shift; my $pg = $self->{'pg'}; my $dbh = $self->{'dbh'}; my ($id, $id2); # Test generation and matching $id = $pg->fileid("/lxr/tests/testpath", 1); $self->assert_not_null($id, "Fileid null"); $id2 = $pg->fileid("/lxr/tests/testpath", 1); $self->assert_equals($id, $id2, "Fileid doesn't match"); $id2 = $pg->fileid("/lxr/tests/testpath", 2); $self->assert_not_equals($id, $id2, "Match even though revisions different"); $id2 = $pg->fileid("/lxr/tests/testpath/two", 1); $self->assert_not_equals($id, $id2); # Look in the database for the files my $data; $data = $dbh->selectrow_array("SELECT filename FROM lxr_files WHERE fileid = $id"); $self->assert_equals("/lxr/tests/testpath", $data); $data = $dbh->selectrow_array("SELECT revision FROM lxr_files WHERE fileid = $id"); $self->assert_equals(1, $data); $data = $dbh->selectrow_array("SELECT COUNT(filename) FROM lxr_files"); $self->assert_equals(3, $data, "Wrong number of rows in DB"); } # Tests for the fileindexed getter/setters sub test_fileindexed { my $self = shift; my $pg = $self->{'pg'}; my $dbh = $self->{'dbh'}; my $status; # Insert some files into table $dbh->do("insert into lxr_files (filename, revision, fileid) values ('temp', '1.1', 1789)"); $dbh->do("insert into lxr_files (filename, revision, fileid) values ('temp22', '2.2.', 17890)"); # Indexed $status = $pg->fileindexed(1789); $self->assert_equals(0, $status); $pg->setfileindexed(1789); $status = $pg->fileindexed(1789); $self->assert($status, "Indexed should be true"); $pg->setfileindexed(17890); $status = $pg->fileindexed(1789); $self->assert($status); $pg->setfilereferenced(17890); $status = $pg->fileindexed(17890); $self->assert($status, "Expected indexed to be true"); } sub test_filereferenced { my $self = shift; my $pg = $self->{'pg'}; my $dbh = $self->{'dbh'}; my $status; # referenced $status = $pg->filereferenced(17890); $self->assert(!$status, "Referenced should be false (fixture interference?)"); $status = $pg->filereferenced(1789); $self->assert(!$status, "Ref should be false"); $status = $dbh->do("insert into lxr_files values (\'hi\', 1, 1789)"); $self->assert_equals($status, 1); $pg->setfilereferenced(1789); $status = $pg->filereferenced(1789); $self->assert($status); $status = $pg->fileindexed(1789); $self->assert($status, "Setting referenced should set indexed"); } sub test_creation { my $self = shift; $self->assert($config->dbname =~ /^dbi:Pg:dbname/); $self->assert(defined($self->{'pg'}), "Failed to create object"); $self->assert($self->{'pg'}->isa("LXR::Index::Postgres"), "Not a Index::Postgres object"); # Check we have connected to the database $self->assert_not_null($self->{'dbh'}, $DBI::errstr); } # Test symbol insertion and retrieval sub test_symbol { my $self = shift; my ($stat, $symname, $fid1, $fid2, $line, $langid, $type, $relsym); my $dbh = $self->{'dbh'}; my $pg = $self->{'pg'}; # Set up some dummy files $fid1 = $pg->fileid("/lxr/tests/testfile1", "1.0.1"); $fid2 = $pg->fileid("/a/nother/path/to/a/file/that\'s very/long", "longversionstringhere"); $pg->setfilerelease($fid1, 1); $pg->setfilerelease($fid2, 2); # Set up dummy declarations my ($id1, $id2); $id1 = $pg->decid(1, "struct"); $id2 = $pg->decid(1, "class"); # Now insert some symbols into the first release $pg->setsymdeclaration("symbol1", $fid1, 1000, 1, $id1, undef); $pg->setsymdeclaration("aVeryLongSymbolNameWithWeirdCapsGoesHere", $fid2, 101, 1, $id2, undef); # Check that the declarations are in the database my $data; $data = $dbh->selectall_arrayref("SELECT s.symname, i.line, i.type FROM lxr_indexes as i, lxr_symbols as s WHERE i.symid = s.symid AND i.fileid = $fid1"); $self->assert_equals( "symbol1", $data->[0]->[0]); $self->assert_equals(1000, $data->[0]->[1]); $self->assert_equals($id1, $data->[0]->[2]); $data = $dbh->selectall_arrayref("SELECT s.symname, i.line, i.type FROM lxr_indexes as i, lxr_symbols as s WHERE i.symid=s.symid AND i.fileid = $fid2"); $self->assert_equals("aVeryLongSymbolNameWithWeirdCapsGoesHere", $data->[0]->[0]); $self->assert_equals(101, $data->[0]->[1]); $self->assert_equals($id2, $data->[0]->[2]); # Now use the accessors my @syms; @syms = $pg->symdeclarations("symbol1", 1); # returns filename, line, declaration, relsym $self->assert_equals("/lxr/tests/testfile1", $syms[0]->[0]); $self->assert_equals(1000, $syms[0]->[1]); $self->assert_equals("struct", $syms[0]->[2]); $self->assert_null($syms[0]->[3]); } # Test reference insertion and retrival sub test_references { my $self = shift; my $pg = $self->{'pg'}; my $dbh = $self->{'dbh'}; my ($fid1, $fid2); # Set up some dummy files $fid1 = $pg->fileid("/lxr/tests/testfile1", "1.0.1"); $fid2 = $pg->fileid("/a/nother/path/to/a/file/that\'s very/long", "longversionstringhere"); $pg->setfilerelease($fid1, 1); $pg->setfilerelease($fid2, 2); # Set up dummy declarations my ($id1, $id2); $id1 = $pg->decid(1, "struct"); $id2 = $pg->decid(1, "class"); # Now insert some symbols into the first release $pg->setsymdeclaration("symbol1", $fid1, 1000, 1, $id1, undef); $pg->setsymdeclaration("aVeryLongSymbolNameWithWeirdCapsGoesHere", $fid2, 101, 1, $id2, undef); # Set references to symbols $pg->setsymreference("symbol1", $fid1, 2000); $pg->setsymreference("symbol1", $fid2, 3000); $pg->setsymreference("symbol1", $fid1, 2001); # Check directly in database my $data; $data = $dbh->selectall_arrayref("SELECT * from lxr_usage WHERE symid=".$pg->symid("symbol1")." ORDER BY line"); $self->assert_equals(2000,$data->[0]->[1]); $self->assert_equals(2001, $data->[1]->[1]); # And via accessor my @data; @data = $pg->symreferences("symbol1", 1); # returns filename, line $self->assert_equals("/lxr/tests/testfile1", $data[0]->[0]); $self->assert_equals(2000, $data[0]->[1]); $self->assert_equals(1, $#data); $self->assert_equals("/lxr/tests/testfile1", $data[1]->[0]); $self->assert_equals(2001, $data[1]->[1]); @data = $pg->symreferences("symbol1", 2); $self->assert_equals(0, $#data); $self->assert_equals("/a/nother/path/to/a/file/that\'s very/long", $data[0]->[0]); $self->assert_equals(3000, $data[0]->[1]); } # Test the declaration id creation logic sub test_decid { my $self = shift; my $pg = $self->{'pg'}; my ($id1, $id2); $id1 = $pg->decid(1, "struct"); $id2 = $pg->decid(1, "class"); $self->assert($id1 != $id2); } # set_up and tear_down are used to # prepare and release resources need for testing # Prepare the database for the run sub set_up { my $self = shift; # Create config $config = new LXR::Config("http://test/pg/lxr", "./lxr.conf"); # Clear out all data in the database system("psql lxr_tests < initdb-postgres >".File::Spec->devnull()." 2>&1"); # Create Pg object $self->{'pg'} = new LXR::Index($config->dbname); # Create direct connection to DB $self->{'dbh'} = DBI->connect($config->dbname); } sub tear_down { my $self = shift; $self->{'pg'} = undef; $config = undef; $self->{'dbh'}->disconnect(); } 1; --- NEW FILE: initdb-postgres --- drop sequence lxr_filenum; drop sequence lxr_symnum; drop sequence lxr_declnum; drop table lxr_files cascade; drop table lxr_symbols cascade; drop table lxr_indexes cascade; drop table lxr_releases cascade; drop table lxr_usage cascade; drop table lxr_status cascade; drop table lxr_declarations cascade; create sequence lxr_filenum cache 50; create sequence lxr_symnum cache 50; create sequence lxr_declnum cache 10; create table lxr_files ( filename varchar, revision varchar, fileid int, primary key (fileid), unique (filename, revision) ); create table lxr_symbols ( symname varchar, symid int, primary key (symid), unique (symname) ); create table lxr_declarations ( declid smallint not null, langid smallint not null, declaration char(255) not null, primary key (declid, langid) ); create table lxr_indexes ( symid int references lxr_symbols, fileid int references lxr_files, line int, langid smallint not null, type smallint not null, relsym int references lxr_symbols, foreign key (langid, type) references lxr_declarations (langid, declid) ); create table lxr_releases (fileid int references lxr_files, releaseid varchar, primary key (fileid,releaseid) ); create table lxr_usage (fileid int references lxr_files, line int, symid int references lxr_symbols ); create table lxr_status (fileid int references lxr_files, status smallint, primary key (fileid) ); create index lxr_indexindex on lxr_indexes using btree (symid); create index lxr_symbolindex on lxr_symbols using btree (symname); create index lxr_usageindex on lxr_usage using btree (symid); create index lxr_filelookup on lxr_files using btree (filename); grant select on lxr_files to public; grant select on lxr_symbols to public; grant select on lxr_indexes to public; grant select on lxr_releases to public; grant select on lxr_usage to public; grant select on lxr_status to public; grant select on lxr_declarations to public; |