File:  [LON-CAPA] / loncom / interface / test / lonmysqltest.pl
Revision 1.2: download - view: text, annotated - select for diffs
Mon Feb 21 18:07:30 2005 UTC (19 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Use 'a'..'z' instead of qw/a b c .../.

#!/usr/bin/perl -w
# The LearningOnline Network with CAPA
#
# $Id: lonmysqltest.pl,v 1.2 2005/02/21 18:07:30 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 = ('a'..'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>