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, 3 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 .../.

    1: #!/usr/bin/perl -w
    2: # The LearningOnline Network with CAPA
    3: #
    4: # $Id: lonmysqltest.pl,v 1.2 2005/02/21 18:07:30 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ######################################################################
   29: use strict;
   30: use lib '/home/httpd/lib/perl/';
   31: use Apache::lonmysql();
   32: use DBI;
   33: use Test::Simple tests => 57;
   34: 
   35: ##
   36: ##  Note: The root password to my MySQL server is shown below.
   37: ##  Access is only allowed from localhost so it should be okay.
   38: ##
   39: my $supersecretpassword = '';  # shhhh
   40: ok(&create_test_db(),'database creation');
   41: 
   42: &Apache::lonmysql::disconnect_from_db();
   43: &Apache::lonmysql::set_mysql_user_and_password('root',
   44:                                                $supersecretpassword,
   45:                                                'lonmysqltest');
   46: &Apache::lonmysql::connect_to_db();
   47: my @letters = ('a'..'z');
   48: foreach my $letter (@letters) {
   49:     my $tablename = '1234'.$letter.'5678';
   50:     ok(&test_table_name_creation($tablename),'creating '.$tablename);
   51:     $tablename = '1234'.uc($letter).'5678';
   52:     ok(&test_table_name_creation($tablename),'creating '.$tablename);
   53: }
   54: foreach my $tablename (qw/1430288fd2941admsul1 
   55:                           2521505c20a41b8msul1 
   56:                           74261c618e441e8msul1/) {
   57:     ok(&test_table_name_creation($tablename),'creating '.$tablename);
   58: }
   59: 
   60: ok(&test_database_drop(),'database destruction');
   61: 
   62: exit;
   63: 
   64: #####################################################################
   65: #####################################################################
   66: ##
   67: ##                     Tests live below
   68: ##
   69: #####################################################################
   70: #####################################################################
   71: sub create_test_db {
   72:     &Apache::lonmysql::set_mysql_user_and_password('root',
   73:                                                    $supersecretpassword,
   74:                                                    'mysql');
   75:     my $dbh = &Apache::lonmysql::get_dbh();
   76:     if (! defined($dbh)) { return 0; }
   77:     my $request = 'DROP DATABASE IF EXISTS lonmysqltest';
   78:     $dbh->do($request);
   79:     $request = 'CREATE DATABASE lonmysqltest';
   80:     $dbh->do($request);
   81:     if ($dbh->err) {
   82:        return 0;
   83:     } else {
   84:         return 1;
   85:     }
   86:     &Apache::lonmysql::disconnect_from_db();
   87: }
   88: 
   89: sub test_database_drop {
   90:     &Apache::lonmysql::disconnect_from_db();
   91:     &Apache::lonmysql::set_mysql_user_and_password('root',
   92:                                                    $supersecretpassword,
   93:                                                    'mysql');
   94:     my $dbh = &Apache::lonmysql::get_dbh();
   95:     if (! defined($dbh)) { return 0; }
   96:     my $request = 'DROP DATABASE IF EXISTS lonmysqltest';
   97:     $dbh->do($request);
   98:     if ($dbh->err) {
   99:         return 0;
  100:     } else {
  101:         return 1;
  102:     }
  103:     &Apache::lonmyql::disconnect_from_db();
  104: }
  105: 
  106: sub test_table_name_creation {
  107:     my ($tablename) = @_;
  108:     my $dbh = &Apache::lonmysql::get_dbh();
  109:     if (! defined($dbh)) { return 0; }
  110:     ##
  111:     ## Silly table creation test
  112:     my $fixed_table_name = &Apache::lonmysql::fix_table_name($tablename);
  113:     my $table_def = {
  114:         id => $fixed_table_name,
  115:         permanent => 'no',
  116:         columns => [{ name => 'student_id',
  117:                       type => 'MEDIUMINT UNSIGNED',
  118:                       restrictions => 'NOT NULL',
  119:                       auto_inc     => 'yes', },
  120:                     { name => 'student',
  121:                       type => 'VARCHAR(100) BINARY',
  122:                       restrictions => 'NOT NULL UNIQUE'},
  123:                     { name => 'section',
  124:                       type => 'VARCHAR(100) BINARY',
  125:                       restrictions => 'NOT NULL'},
  126:                     { name => 'status',
  127:                       type => 'VARCHAR(15) BINARY',
  128:                       restrictions => 'NOT NULL'},
  129:                     { name => 'classification',
  130:                       type => 'VARCHAR(100) BINARY', },
  131:                     { name => 'updatetime',
  132:                       type => 'INT UNSIGNED'},
  133:                     { name => 'fullupdatetime',
  134:                       type => 'INT UNSIGNED'},
  135:                     ],
  136:         'PRIMARY KEY' => ['student_id'],
  137:         'KEY' => [{ columns => ['student (100)',
  138:                                 'section (100)',
  139:                                 'status (15)',]},],
  140:     };
  141:     my $tableid = &Apache::lonmysql::create_table($table_def);
  142:     if (! defined($tableid)) {
  143: #        warn 
  144: #            "Unable to create table '$tablename'->'$fixed_table_name'".$/.
  145: #            &Apache::lonmysql::get_error().$/;
  146:         return 0;
  147:     }
  148:     if ($dbh->err) {
  149: #        warn "Unspecified error on '$tablename'->'$fixed_table_name'";
  150:         return 0;
  151:     } else {
  152:         return 1;
  153:     }
  154: }

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