use lib 'ex'; # Where BeerDB should live
BEGIN {
plan tests => 26;
-
}
$db = 'test';
-$dbuser = 'test';
+$dbuser = 'test';
$dbpasswd = '';
$table = "beer_test";
$sql = "
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);
# 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');
$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