Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / 01-columns.t
CommitLineData
ea2e61bf 1use strict;
4a233f30 2use warnings;
ea2e61bf 3
289ba852 4use Test::More;
97d61088 5use lib 't/cdbi/testlib';
289ba852 6
ea2e61bf 7
8#-----------------------------------------------------------------------
9# Make sure that we can set up columns properly
10#-----------------------------------------------------------------------
11package State;
12
97d61088 13use base 'DBIC::Test::SQLite';
ea2e61bf 14
15State->table('State');
16State->columns(Essential => qw/Abbreviation Name/);
17State->columns(Primary => 'Name');
18State->columns(Weather => qw/Rain Snowfall/);
19State->columns(Other => qw/Capital Population/);
20#State->has_many(cities => "City");
21
05ccf064 22sub accessor_name_for {
6a3bf251 23 my ($class, $column) = @_;
24 my $return = $column eq "Rain" ? "Rainfall" : $column;
25 return $return;
ea2e61bf 26}
27
05ccf064 28sub mutator_name_for {
6a3bf251 29 my ($class, $column) = @_;
30 my $return = $column eq "Rain" ? "set_Rainfall" : "set_$column";
31 return $return;
ea2e61bf 32}
33
34sub Snowfall { 1 }
35
36
37package City;
38
97d61088 39use base 'DBIC::Test::SQLite';
ea2e61bf 40
41City->table('City');
42City->columns(All => qw/Name State Population/);
ea2e61bf 43
97ac49db 44{
45 # Disable the `no such table' warning
46 local $SIG{__WARN__} = sub {
47 my $warning = shift;
48 warn $warning unless ($warning =~ /\Qno such table: City(1)\E/);
49 };
50
51 City->has_a(State => 'State');
52}
ea2e61bf 53
54#-------------------------------------------------------------------------
55package CD;
97d61088 56use base 'DBIC::Test::SQLite';
ea2e61bf 57
58CD->table('CD');
59CD->columns('All' => qw/artist title length/);
60
61#-------------------------------------------------------------------------
62
63package main;
64
65is(State->table, 'State', 'State table()');
66is(State->primary_column, 'name', 'State primary()');
67is_deeply [ State->columns('Primary') ] => [qw/name/],
6a3bf251 68 'State Primary:' . join ", ", State->columns('Primary');
ea2e61bf 69is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/],
6a3bf251 70 'State Essential:' . join ", ", State->columns('Essential');
ea2e61bf 71is_deeply [ sort State->columns('All') ] =>
6a3bf251 72 [ sort qw/name abbreviation rain snowfall capital population/ ],
73 'State All:' . join ", ", State->columns('All');
ea2e61bf 74
75is(CD->primary_column, 'artist', 'CD primary()');
76is_deeply [ CD->columns('Primary') ] => [qw/artist/],
6a3bf251 77 'CD primary:' . join ", ", CD->columns('Primary');
ea2e61bf 78is_deeply [ sort CD->columns('All') ] => [qw/artist length title/],
6a3bf251 79 'CD all:' . join ", ", CD->columns('All');
ea2e61bf 80is_deeply [ sort CD->columns('Essential') ] => [qw/artist/],
6a3bf251 81 'CD essential:' . join ", ", CD->columns('Essential');
ea2e61bf 82
83ok(State->find_column('Rain'), 'find_column Rain');
84ok(State->find_column('rain'), 'find_column rain');
85ok(!State->find_column('HGLAGAGlAG'), '!find_column HGLAGAGlAG');
86
87{
6a3bf251 88
ea2e61bf 89 can_ok +State => qw/Rainfall _Rainfall_accessor set_Rainfall
6a3bf251 90 _set_Rainfall_accessor Snowfall _Snowfall_accessor set_Snowfall
91 _set_Snowfall_accessor/;
92
93 foreach my $method (qw/Rain _Rain_accessor rain snowfall/) {
94 ok !State->can($method), "State can't $method";
ea2e61bf 95 }
96
97}
98
99{
6a3bf251 100 SKIP: {
101 skip "No column objects", 1;
ea2e61bf 102
6a3bf251 103 eval { my @grps = State->__grouper->groups_for("Huh"); };
104 ok $@, "Huh not in groups";
105 }
ea2e61bf 106
6a3bf251 107 my @grps = sort State->__grouper->groups_for(State->_find_columns(qw/rain capital/));
108 is @grps, 2, "Rain and Capital = 2 groups";
9bc6db13 109 @grps = sort @grps; # Because the underlying API is hash-based
6a3bf251 110 is $grps[0], 'Other', " - Other";
111 is $grps[1], 'Weather', " - Weather";
ea2e61bf 112}
113
d7156e50 114#{
6a3bf251 115#
d7156e50 116# package DieTest;
117# @DieTest::ISA = qw(DBIx::Class);
118# DieTest->load_components(qw/CDBICompat::Retrieve Core/);
119# package main;
6a3bf251 120# local $SIG{__WARN__} = sub { };
121# eval { DieTest->retrieve(1) };
122# like $@, qr/unless primary columns are defined/, "Need primary key for retrieve";
d7156e50 123#}
ea2e61bf 124
125#-----------------------------------------------------------------------
126# Make sure that columns inherit properly
127#-----------------------------------------------------------------------
128package State;
129
130package A;
131@A::ISA = qw(DBIx::Class);
126042ee 132__PACKAGE__->load_components(qw/CDBICompat Core/);
ec77fadc 133__PACKAGE__->table('dummy');
ea2e61bf 134__PACKAGE__->columns(Primary => 'id');
135
136package A::B;
137@A::B::ISA = 'A';
ec77fadc 138__PACKAGE__->table('dummy2');
ea2e61bf 139__PACKAGE__->columns(All => qw(id b1));
140
141package A::C;
142@A::C::ISA = 'A';
ec77fadc 143__PACKAGE__->table('dummy3');
ea2e61bf 144__PACKAGE__->columns(All => qw(id c1 c2 c3));
145
146package main;
147is join (' ', sort A->columns), 'id', "A columns";
148is join (' ', sort A::B->columns), 'b1 id', "A::B columns";
149is join (' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns";
150
d9bd5195 151done_testing;