1use strict; 2use Test::More tests => 26; 3 4#----------------------------------------------------------------------- 5# Make sure that we can set up columns properly 6#----------------------------------------------------------------------- 7package State; 8 9use base 'Class::DBI'; 10use Class::DBI::Column; 11 12State->table('State'); 13State->columns(Essential => qw/Abbreviation Name/); 14State->columns(Primary => 'Name'); 15State->columns(Weather => qw/Snowfall/, 16 Class::DBI::Column->new('Rain', { accessor => 'Rainfall' }) 17); 18State->columns(Other => qw/Capital Population/); 19State->has_many(cities => "City"); 20 21sub mutator_name_for { 22 my ($class, $column) = @_; 23 return "set_" . $column->accessor; 24} 25 26sub Snowfall { 1 } 27 28package City; 29 30use base 'Class::DBI'; 31 32City->table('City'); 33City->columns(All => qw/Name State Population/); 34City->has_a(State => 'State'); 35 36#------------------------------------------------------------------------- 37package CD; 38use base 'Class::DBI'; 39 40CD->table('CD'); 41CD->columns('All' => qw/artist title length/); 42 43#------------------------------------------------------------------------- 44 45package main; 46 47is(State->table, 'State', 'State table()'); 48is(State->primary_column, 'name', 'State primary()'); 49is_deeply [ State->columns('Primary') ] => [qw/name/], 50 'State Primary:' . join ", ", State->columns('Primary'); 51is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/], 52 'State Essential:' . join ", ", State->columns('Essential'); 53is_deeply [ sort State->columns('All') ] => 54 [ sort qw/name abbreviation rain snowfall capital population/ ], 55 'State All:' . join ", ", State->columns('All'); 56 57is(CD->primary_column, 'artist', 'CD primary()'); 58is_deeply [ CD->columns('Primary') ] => [qw/artist/], 59 'CD primary:' . join ", ", CD->columns('Primary'); 60is_deeply [ sort CD->columns('All') ] => [qw/artist length title/], 61 'CD all:' . join ", ", CD->columns('All'); 62is_deeply [ sort CD->columns('Essential') ] => [qw/artist/], 63 'CD essential:' . join ", ", CD->columns('Essential'); 64 65{ 66 local $SIG{__WARN__} = sub { ok 1, "Error thrown" }; 67 ok(!State->columns('Nonsense'), "No Nonsense group"); 68} 69ok(State->find_column('Rain'), 'find_column Rain'); 70ok(State->find_column('rain'), 'find_column rain'); 71ok(!State->find_column('HGLAGAGlAG'), '!find_column HGLAGAGlAG'); 72 73can_ok +State => qw/Rainfall _Rainfall_accessor set_Rainfall 74 _set_Rainfall_accessor Snowfall _Snowfall_accessor set_Snowfall 75 _set_Snowfall_accessor/; 76 77foreach my $method (qw/Rain _Rain_accessor rain snowfall/) { 78 ok !State->can($method), "State can't $method"; 79} 80 81{ 82 eval { my @grps = State->__grouper->groups_for("Huh"); }; 83 ok $@, "Huh not in groups"; 84 85 my @grps = 86 sort State->__grouper->groups_for(State->_find_columns(qw/rain capital/)); 87 is @grps, 2, "Rain and Capital = 2 groups"; 88 is $grps[0], 'Other', " - Other"; 89 is $grps[1], 'Weather', " - Weather"; 90} 91 92{ 93 local $SIG{__WARN__} = sub { }; 94 eval { Class::DBI->retrieve(1) }; 95 like $@, qr/Can't retrieve unless primary columns are defined/, 96 "Need primary key for retrieve"; 97} 98 99#----------------------------------------------------------------------- 100# Make sure that columns inherit properly 101#----------------------------------------------------------------------- 102package State; 103 104package A; 105@A::ISA = qw(Class::DBI); 106__PACKAGE__->columns(Primary => 'id'); 107 108package A::B; 109@A::B::ISA = 'A'; 110__PACKAGE__->columns(All => qw(id b1)); 111 112package A::C; 113@A::C::ISA = 'A'; 114__PACKAGE__->columns(All => qw(id c1 c2 c3)); 115 116package main; 117is join(' ', sort A->columns), 'id', "A columns"; 118is join(' ', sort A::B->columns), 'b1 id', "A::B columns"; 119is join(' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns"; 120