File:  [LON-CAPA] / loncom / interface / test / lonmysqltest.pl
Revision 1.1: download - view: text, annotated - select for diffs
Mon Feb 21 17:25:34 2005 UTC (19 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Test harness for lonmysql.pm.  Must be run as root or www.  Be sure to
fill in the root mysql password before running.
Currently tests creation of tables which have names that MySQL may find
confusing.

#!/usr/bin/perl -w
# The LearningOnline Network with CAPA
#
# $Id: lonmysqltest.pl,v 1.1 2005/02/21 17:25:34 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
######################################################################
use strict;
use lib '/home/httpd/lib/perl/';
use Apache::lonmysql();
use DBI;
use Test::Simple tests => 57;

##
##  Note: The root password to my MySQL server is shown below.
##  Access is only allowed from localhost so it should be okay.
##
my $supersecretpassword = '';  # shhhh
ok(&create_test_db(),'database creation');

&Apache::lonmysql::disconnect_from_db();
&Apache::lonmysql::set_mysql_user_and_password('root',
                                               $supersecretpassword,
                                               'lonmysqltest');
&Apache::lonmysql::connect_to_db();
my @letters = qw/a b c d e f g h i j k l m n o p q r s t u v w x y z/;
foreach my $letter (@letters) {
    my $tablename = '1234'.$letter.'5678';
    ok(&test_table_name_creation($tablename),'creating '.$tablename);
    $tablename = '1234'.uc($letter).'5678';
    ok(&test_table_name_creation($tablename),'creating '.$tablename);
}
foreach my $tablename (qw/1430288fd2941admsul1 
                          2521505c20a41b8msul1 
                          74261c618e441e8msul1/) {
    ok(&test_table_name_creation($tablename),'creating '.$tablename);
}

ok(&test_database_drop(),'database destruction');

exit;

#####################################################################
#####################################################################
##
##                     Tests live below
##
#####################################################################
#####################################################################
sub create_test_db {
    &Apache::lonmysql::set_mysql_user_and_password('root',
                                                   $supersecretpassword,
                                                   'mysql');
    my $dbh = &Apache::lonmysql::get_dbh();
    if (! defined($dbh)) { return 0; }
    my $request = 'DROP DATABASE IF EXISTS lonmysqltest';
    $dbh->do($request);
    $request = 'CREATE DATABASE lonmysqltest';
    $dbh->do($request);
    if ($dbh->err) {
       return 0;
    } else {
        return 1;
    }
    &Apache::lonmysql::disconnect_from_db();
}

sub test_database_drop {
    &Apache::lonmysql::disconnect_from_db();
    &Apache::lonmysql::set_mysql_user_and_password('root',
                                                   $supersecretpassword,
                                                   'mysql');
    my $dbh = &Apache::lonmysql::get_dbh();
    if (! defined($dbh)) { return 0; }
    my $request = 'DROP DATABASE IF EXISTS lonmysqltest';
    $dbh->do($request);
    if ($dbh->err) {
        return 0;
    } else {
        return 1;
    }
    &Apache::lonmyql::disconnect_from_db();
}

sub test_table_name_creation {
    my ($tablename) = @_;
    my $dbh = &Apache::lonmysql::get_dbh();
    if (! defined($dbh)) { return 0; }
    ##
    ## Silly table creation test
    my $fixed_table_name = &Apache::lonmysql::fix_table_name($tablename);
    my $table_def = {
        id => $fixed_table_name,
        permanent => 'no',
        columns => [{ name => 'student_id',
                      type => 'MEDIUMINT UNSIGNED',
                      restrictions => 'NOT NULL',
                      auto_inc     => 'yes', },
                    { name => 'student',
                      type => 'VARCHAR(100) BINARY',
                      restrictions => 'NOT NULL UNIQUE'},
                    { name => 'section',
                      type => 'VARCHAR(100) BINARY',
                      restrictions => 'NOT NULL'},
                    { name => 'status',
                      type => 'VARCHAR(15) BINARY',
                      restrictions => 'NOT NULL'},
                    { name => 'classification',
                      type => 'VARCHAR(100) BINARY', },
                    { name => 'updatetime',
                      type => 'INT UNSIGNED'},
                    { name => 'fullupdatetime',
                      type => 'INT UNSIGNED'},
                    ],
        'PRIMARY KEY' => ['student_id'],
        'KEY' => [{ columns => ['student (100)',
                                'section (100)',
                                'status (15)',]},],
    };
    my $tableid = &Apache::lonmysql::create_table($table_def);
    if (! defined($tableid)) {
#        warn 
#            "Unable to create table '$tablename'->'$fixed_table_name'".$/.
#            &Apache::lonmysql::get_error().$/;
        return 0;
    }
    if ($dbh->err) {
#        warn "Unspecified error on '$tablename'->'$fixed_table_name'";
        return 0;
    } else {
        return 1;
    }
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>