--- loncom/metadata_database/lonmetadata_test.pl 2004/01/12 21:33:19 1.1 +++ loncom/metadata_database/lonmetadata_test.pl 2004/04/08 14:51:19 1.4 @@ -1,7 +1,7 @@ #!/usr/bin/perl -w # The LearningOnline Network with CAPA # -# $Id: lonmetadata_test.pl,v 1.1 2004/01/12 21:33:19 matthew Exp $ +# $Id: lonmetadata_test.pl,v 1.4 2004/04/08 14:51:19 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -30,11 +30,18 @@ use strict; use DBI; use LONCAPA::lonmetadata(); -use Test::Simple tests => 3; +use Test::Simple tests => 4; +## +## Note: The root password to my MySQL server is shown below. +## Access is only allowed from localhost so it should be okay. +## Now if you will excuse me I have to change the password on my luggage. +## +my $supersecretpassword = '123'; # shhhh ok(&create_test_db(),'database creation'); ok(&test_creation(),'table creation'); +ok(&test_named_creation(),'named table creation'); ok(&test_inserts(),'insert test'); exit; @@ -47,7 +54,7 @@ exit; ##################################################################### ##################################################################### sub create_test_db { - my $dbh = DBI->connect("DBI:mysql:test","root","123", + my $dbh = DBI->connect("DBI:mysql:test","root",$supersecretpassword, { RaiseError =>0,PrintError=>0}); if (! defined($dbh)) { return 0; @@ -65,7 +72,7 @@ sub create_test_db { } sub test_creation { - my $dbh = DBI->connect("DBI:mysql:lonmetatest","root","123", + my $dbh = DBI->connect("DBI:mysql:lonmetatest","root",$supersecretpassword, { RaiseError =>0,PrintError=>0}); my $request = &LONCAPA::lonmetadata::create_metadata_storage(); $dbh->do($request); @@ -78,8 +85,30 @@ sub test_creation { } } +sub test_named_creation { + my $request = + &LONCAPA::lonmetadata::create_metadata_storage('nonmetadata'); + my $dbh = DBI->connect("DBI:mysql:lonmetatest","root",$supersecretpassword, + { RaiseError =>0,PrintError=>0}); + $dbh->do($request); # Create the table, only return 0 if we cannot. + if ($dbh->err) { + $dbh->disconnect(); + return 0; + } + $dbh->do('DROP TABLE nonmetadata'); # This will generate an error if the + # table does not exist + if ($dbh->err) { + $dbh->disconnect(); + return 0; + } else { + $dbh->disconnect(); + return 1; + } +} + sub test_inserts { - my $dbh = DBI->connect("DBI:mysql:lonmetatest","root","123", + my $tablename = 'metadatatest'; + my $dbh = DBI->connect("DBI:mysql:lonmetatest","root",$supersecretpassword, { RaiseError =>0,PrintError=>0}); my @TestRecords = ( { url => 'm/b/h/test1' }, @@ -126,8 +155,22 @@ sub test_inserts { hostname =>'6', }, ); + # Create the table + my $request = &LONCAPA::lonmetadata::create_metadata_storage($tablename); + $dbh->do($request); + if ($dbh->err) { + $dbh->disconnect(); + warn "Unable to create table for test"; + return 0; + } else { + $dbh->disconnect(); + return 1; + } + # Store the sample records foreach my $data (@TestRecords) { - my ($count,$error) = &LONCAPA::lonmetadata::store_metadata($dbh,$data); + my ($count,$error) = &LONCAPA::lonmetadata::store_metadata($dbh, + $tablename, + $data); if (! $count) { warn $error; return 0;