Annotation of loncom/interface/test/lonmysqltest.pl, revision 1.2

1.1       matthew     1: #!/usr/bin/perl -w
                      2: # The LearningOnline Network with CAPA
                      3: #
1.2     ! matthew     4: # $Id: lonmysqltest.pl,v 1.1 2005/02/21 17:25:34 matthew Exp $
1.1       matthew     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();
1.2     ! matthew    47: my @letters = ('a'..'z');
1.1       matthew    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>