From: Aaron Trevena Date: Fri, 14 Jul 2006 10:34:18 +0000 (+0000) Subject: fixed mysql tests a bit - checks for and creates test db if needed, type matching... X-Git-Tag: 2.11~13 X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=abb63001d28afe34e807dbd4449feaded558c528;p=maypole.git fixed mysql tests a bit - checks for and creates test db if needed, type matching now less pedantic git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@507 48953598-375a-da11-a14b-00016c27c3ee --- diff --git a/MANIFEST b/MANIFEST index 5453207..03fec8e 100644 --- a/MANIFEST +++ b/MANIFEST @@ -49,6 +49,7 @@ t/constants.t t/headers.t t/maypole.t t/pathtools.t +t/db_colinfo.t t/templates/custom/classdata t/templates/custom/frontpage t/templates/custom/list diff --git a/t/db_colinfo.t b/t/db_colinfo.t index 0c19556..cb1b436 100755 --- a/t/db_colinfo.t +++ b/t/db_colinfo.t @@ -3,11 +3,10 @@ use Test::More; use lib 'ex'; # Where BeerDB should live BEGIN { plan tests => 26; - } $db = 'test'; -$dbuser = 'test'; +$dbuser = 'test'; $dbpasswd = ''; $table = "beer_test"; $sql = " @@ -18,64 +17,79 @@ create table $table ( score smallint(2), price decimal(3,2), abv varchar(10), - image blob, + image blob, notes text, - tasted date NOT NULL, + tasted date NOT NULL, created timestamp default CURRENT_TIMESTAMP, - modified datetime default NULL, + modified datetime default NULL, style mediumint(8) default 1, brewery integer default NULL );"; # correct column types and the ones we test %correct_types = ( - id => 'int(11)', # mysql 4.1 stores this for 'integer' - brewery => 'int(11)', - style => 'mediumint(8)', - name => 'char(30)', - url => 'varchar(120)', - tasted => 'date', - created => 'timestamp', - modified => 'datetime', - score => 'smallint(2)', - price => 'decimal(3,2)', - abv => 'varchar(10)', - notes => 'text', - image => 'blob', + id => 'int', # mysql 4.1 stores this for 'integer' + brewery => 'int', + style => 'int', + name => 'char', + url => 'varchar', + tasted => 'date', + created => '(time|time)', + modified => '(date|time)', + score => 'smallint', + price => 'decimal', + abv => 'varchar', + notes => '(text|blob)', + image => 'blob', ); # Runs tests on column_* method of $class using %correct data hash # usage: run_method_tests ($class, $method, %correct); sub run_method_tests { - ($class, $method, %correct) = @_; - for $col (sort keys %correct) { - $val = $class->$method($col); + ($class, $method, %correct) = @_; + for $col (sort keys %correct) { + $val = $class->$method($col); - # Hacks for various val types - $val = lc $val if $method eq 'column_type'; + # Hacks for various val types + $val = lc $val if $method eq 'column_type'; - is($val, $correct{$col},"$method $col is $val"); - } + my $correct = $correct{$col}; + like($val, qr/$correct/,"$method $col is $val"); + } } - + # mysql test # Make test class package BeerDB::BeerTestmysql; use base Maypole::Model::CDBI; - package main; $DB_Class = 'BeerDB::BeerTestmysql'; -eval {$DB_Class->connection("dbi:mysql:$db", "$dbuser", "$dbpasswd"); }; -$err = $@; -$skip_msg = "Could not connect to MySQL using database 'test', username 'test', and password ''. Check privelages and try again."; + +my $drh = DBI->install_driver("mysql"); +my %databases = map { $_ => 1 } $drh->func('localhost', 3306, '_ListDBs'); + +unless ($databases{test}) { + my $rc = $drh->func("createdb", 'test', 'admin'); +} + +%databases = map { $_ => 1 } $drh->func('localhost', 3306, '_ListDBs'); + +if ($databases{test}) { + eval {$DB_Class->connection("dbi:mysql:$db", "$dbuser", "$dbpasswd"); }; + $err = $@; + $skip_msg = "Could not connect to MySQL using database 'test', username 'test', and password ''. Check privileges and try again."; +} else { + $err = 'no test db'; + $skip_msg = "Could not connect to MySQL using database 'test' as it doesn't exist, sorry"; +} + $skip_howmany = 13; SKIP: { - skip $skip_msg, $skip_howmany if $err; - + skip $skip_msg, $skip_howmany if $err; $DB_Class->db_Main->do("drop table if exists $table;"); $DB_Class->db_Main->do($sql); $DB_Class->table($table); diff --git a/t/maypole.t b/t/maypole.t index 0af1deb..7592453 100755 --- a/t/maypole.t +++ b/t/maypole.t @@ -306,10 +306,9 @@ warn "Tests 49 to 53\n\n"; # is_model_applicable() { TODO: { - local $TODO = "test needs fixing"; - + local $TODO = "test needs fixing"; + $r->config->ok_tables([qw(one two)]); $r->config->display_tables([qw(one two)]); - $r->config->ok_tables(undef); $r->model_class($table_class); $r->table('one'); $r->action('unittest'); @@ -317,18 +316,18 @@ TODO: { $mock_model->mock('is_public', sub {0}); my $true_false = $r->is_model_applicable; is($true_false, 0, - '... returns 0 unless model_class->is_public(action)'); + '... returns 0 unless model_class->is_public(action)'); $mock_model->mock('is_public', sub {$is_public = \@_; 1}); $true_false = $r->is_model_applicable; is($true_false, 1, '... returns 1 if table is in ok_tables'); is_deeply($is_public, [$r->model_class, 'unittest'], - '... calls model_class->is_public with request action'); + '... calls model_class->is_public with request action'); is_deeply($r->config->ok_tables, {one => 1, two => 1}, - '... config->ok_tables defaults to config->display_tables'); + '... config->ok_tables defaults to config->display_tables'); delete $r->config->ok_tables->{one}; $true_false = $r->is_model_applicable; is($true_false, 0, '... returns 0 unless $r->table is in ok_tables'); - } + } } # Tests 54 - 58