X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=t%2Fdb_colinfo.t;fp=t%2Fdb_colinfo.t;h=cb1b4361e1f91c0b6629a6a517b2c7390097cba8;hb=abb63001d28afe34e807dbd4449feaded558c528;hp=0c19556b6c732ad360a58556e82722a06051bb1b;hpb=5c776a61a4b51aa2dc97c08fcb5feffd20f14063;p=maypole.git 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);