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