From: Aaron Trevena Date: Mon, 31 Jul 2006 11:26:59 +0000 (+0000) Subject: added tests for and fixed new column_required and _column_info methods in CDBI.pm X-Git-Tag: 2.11~2 X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=695634a55e7a5293c9b5197d9d6b93245ff3ff14;p=maypole.git added tests for and fixed new column_required and _column_info methods in CDBI.pm git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@518 48953598-375a-da11-a14b-00016c27c3ee --- diff --git a/lib/Maypole/Model/CDBI.pm b/lib/Maypole/Model/CDBI.pm index 5433beb..ce93690 100644 --- a/lib/Maypole/Model/CDBI.pm +++ b/lib/Maypole/Model/CDBI.pm @@ -1,6 +1,8 @@ package Maypole::Model::CDBI; use strict; +use Data::Dumper; + =head1 NAME Maypole::Model::CDBI - Model class based on Class::DBI @@ -503,7 +505,6 @@ sub _isa_class { # sub _column_info { sub _column_info { my $self = shift; - warn "__column_info called by ", join (', ', caller), "\n"; my $dbh = $self->db_Main; my $meta; # The info we are after @@ -531,8 +532,7 @@ sub _hash_type_meta { # required / nullable $meta->{$colname}{nullable} = $row->{NULLABLE}; - - $meta->{$colname}{required} = ($row->{NULLABLE} && $row->{NULLABLE} == 0) ? 1 : 0; + $meta->{$colname}{required} = ( $meta->{$colname}{nullable} == 0 ) ? 1 : 0; # default if (defined $row->{COLUMN_DEF}) { @@ -648,6 +648,7 @@ sub required_columns { my ($class, $columns) = @_; $class->_column_info() unless ref $class->COLUMN_INFO; my $column_info = $class->COLUMN_INFO; + if ($columns) { foreach my $colname ( @$columns ) { if ($class->_isa_class($colname)) { @@ -659,12 +660,12 @@ sub required_columns { warn "$colname is not a recognised column in this class ", ref $class || $class, "\n"; next; } - $column_info->{required} = 1; + $column_info->{$colname}{required} = 1; } $class->COLUMN_INFO($column_info); } - return [ grep ($column_info->{$_}{required}, keys %{$column_info}) ] ; + return [ grep ($column_info->{$_}{required}, keys %$column_info) ] ; } =head2 column_required diff --git a/t/db_colinfo.t b/t/db_colinfo.t index b387769..2b341eb 100755 --- a/t/db_colinfo.t +++ b/t/db_colinfo.t @@ -3,7 +3,7 @@ use Test::More; use Data::Dumper; use lib 'ex'; # Where BeerDB should live BEGIN { - plan tests => 35; + plan tests => 65; } $db = 'test'; @@ -67,8 +67,7 @@ create table $table ( sub run_method_tests { ($class, $method, %correct) = @_; for $col (sort keys %correct) { -# warn "class : $class\n"; -# warn "ISA : ", join(', ',@BeerDB::BeerTestmysql::ISA); + $val = $class->$method($col); # Hacks for various val types @@ -124,6 +123,29 @@ SKIP: { run_method_tests($DB_Class,'column_type', %correct_types); run_method_tests($DB_Class,'column_default', %correct_defaults); run_method_tests($DB_Class,'column_nullable', %correct_nullables); + + + foreach my $colname ( @{$DB_Class->required_columns()} ) { + ok($correct_nullables{$colname} == 0,"nullable column $colname is required (via required_columns)"); + } + + foreach my $colname (keys %correct_nullables) { + ok( $DB_Class->column_required($colname) == !$correct_nullables{$colname}, "nullable column $colname is required (via column_required)" ) + } + + ok($DB_Class->required_columns([qw/score/]), 'set required column(s)'); + + foreach my $colname ( @{$DB_Class->required_columns()} ) { + ok($correct_nullables{$colname} == 0 || $colname eq 'score',"nullable or required column $colname is required (via required_columns)" ); + } + + foreach my $colname (keys %correct_nullables) { + if ($colname eq 'score') { + ok( $DB_Class->column_required($colname) == 0, "nullable column $colname is required (via column_required)"); + } else { + ok( $DB_Class->column_required($colname) == !$correct_nullables{$colname}, "nullable column $colname is required (via column_required)"); + } + } }; # SQLite test @@ -165,6 +187,21 @@ SKIP: { # I think sqlite driver allows everything to be nullable. #run_method_tests($DB_Class,'column_nullable', %correct_nullables); + ok($DB_Class->required_columns([qw/score style name tasted/]), 'set required column(s)'); + + + foreach my $colname ( @{$DB_Class->required_columns()} ) { + ok($correct_nullables{$colname} == 0 || $colname eq 'score',"nullable or required column $colname is required (via required_columns)" ); + } + + foreach my $colname (keys %correct_nullables) { + if ($colname eq 'score') { + ok( $DB_Class->column_required($colname) == 0, "nullable column $colname is required (via column_required)"); + } else { + ok( $DB_Class->column_required($colname) == !$correct_nullables{$colname}, "nullable column $colname is required (via column_required)"); + } + } + };