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